Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using vb.net 2012, linq, sqlserver compact .
I have a form with a datagridview on the left and another form containing a bunch of textboxes and combos for the current row's detail .
What I want to do is display a list of people in the grid then display the selected record details on the second form

I have this linq query:
VB
Dim queryRead = (From contacts In db.Contatti _
                                Select contacts.Id, contacts.Nome, contacts.Comune, contacts.Provincia, contacts.Regione, contacts.DataNascita, contacts.DataMatrimonio)
bs.DataSource = queryRead
dgElenco.AutoGenerateColumns = False
dgElenco.DataSource = bs


then in my click event :
VB
Private Sub dgElenco_Click(sender As Object, e As EventArgs) Handles dgElenco.Click
...
i = dgElenco.CurrentRow.Index
...

I get my data in the grid. Now my problem is how to display the selected row in the detail form. Looking through the debugger I can see the data dgElenco.CurrentRow.DataBoundItem but I can’t seem to pull out the individual fields . Would someone point me to the right direction please,
thanks
Posted

Best way to do this is to create your own datatype class. I used a GridView Control and used this method to get the 'Name' property value if you are using anonymous type.

C#
void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string Name = (string)DataBinder.Eval(e.Row.DataItem, "Name");
            }

        }


Please refer this link[^] for other approaches like using reflection.
 
Share this answer
 
Comments
Claudio Trenca 11-Apr-14 13:02pm    
this project is a windows form, rowdatabound does't exist, which event can I use
Here I used DataGridView Click event

C#
private void dataGridView1_Click(object sender, EventArgs e)
        {
            //Approach -1, using column name
            {
                object name = dataGridView1.CurrentRow.Cells["NameCol"].Value;
            }
            {
                object obj = dataGridView1.CurrentRow.DataBoundItem;

                //Approach -2, using reflection
                Type t = obj.GetType();
                PropertyInfo p = t.GetProperty("Name");
                object v = p.GetValue(obj, null);

                //Approach -3, using new dynamic type
                dynamic d = obj;
                string name = d.Name;
                int age = d.Age;
            }
        }
 
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