Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi
Need help selecting the value from custID column in the ListView so that I can retrieve the value from the database and display it in the TextBoxes.

Thanks

<img src='http://img713.imageshack.us/img713/133/listview.jpg' border='0'/>

Mediafire Link for image
link for database
Posted
Updated 28-Feb-11 2:44am
v4
Comments
#realJSOP 28-Feb-11 7:45am    
Not all of us can get to imageshack from work (or any other photo-sharing site for that matter).
M Bester 28-Feb-11 7:57am    
added a mediafire link, will that work?

You can expand Olivier's solution using a custom class:
private void yourListView_SelectedIndexChanged(object sender, EventArgs e)
{
    if (yourListView.SelectedIndex == -1)
        return;

    Customer customer = ((CustomerListViewItem)yourListView.Items[yourListView.SelectedIndices[0]]).Customer;

    textBoxID.Text = customer.Id.ToString();
    textBoxName.Text = customer.Name;
    textBoxPhone.Text = customer.Phone;
    textBoxLevel.Text = customer.Level;
}

internal class CustomerListViewItem
{
    internal Customer { get; private set; }

    internal CustomerListViewItem(Customer customer)
    {
        Customer = customer;
    }
}
 
Share this answer
 
Comments
M Bester 1-Mar-11 5:56am    
getting an error with "internal Customer { get; private set; }" and commenting that out gives me
-> 'CustomerListViewItem': member names cannot be the same as their enclosing type (CS0542)
M Bester 1-Mar-11 6:51am    
I get the feeling I have to do something extra but I can't see it, got rusty
Dima Popov 1-Mar-11 15:21pm    
Well, just choose another name :)
I had a look to your image and it seems you are using a ListView control.
Can't you just retrieve the info from that control using the SelectedIndexChanged event?
private void yourListView_SelectedIndexChanged(object sender, EventArgs e)
{
    if (yourListView.SelectedIndex == -1)
        return;
    //get selected row
    ListViewItem item = yourListView.Items[yourListView.SelectedIndex];
    //fill the text boxes
    textBoxID.Text = item.Text;
    textBoxName.Text = item.SubItems[0].Text;
    textBoxPhone.Text = item.SubItems[1].Text;
    textBoxLevel.Text = item.SubItems[2].Text;
}
 
Share this answer
 
Comments
M Bester 28-Feb-11 8:51am    
ListViews doesn't have a SelectedIndex, but I worked around that and it works well now, going to test a bit more. Thanks alot.
M Bester 28-Feb-11 8:57am    
Doesn't work as well as I hoped. Busy editing
Without Seeing the Name of your Table and its Column Value its very difficult to write a Program whatever you can take a help of given command to load value in textbox after selecting data in Listview

OleDbCommand cmd = new OleDbCommand("select * from TableName where custID ='"+listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text+"'",YourConnection);
OleDbDataReader dr= cmd.ExecuteReader();
if (dr.Read())
{
txtboxID = dr[Index].ToString();   // Example 
// Assign Correspondance textbox value to dr[with accurate Index No]
}
dr.Close(); 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900