Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends
i want to transfer the selected row of my datagridview to a listview. What am trying to do is to use the listview as a preview of the data in the datagridview. I was using labels to do this (i.e transfering the datas to different labels)but using labels is not a very good practice . can any one help me on this. Here is the code i used with the labels
C#
int i ;
i=dataGridView1.SelectedCells[0].RowIndex;
label1.Text = dataGridView1.Rows[i].Cells[0].Values.ToString();
label2.Text = dataGridView1.Rows[i].Cells[1].Values.ToString();
Posted
Comments
Ese ochuko 11-Mar-13 17:53pm    
Please am using winform
Sergey Alexandrovich Kryukov 11-Mar-13 18:57pm    
Tag it.
—SA
joshrduncan2012 11-Mar-13 17:57pm    
What exactly are you trying to do? Are you wanting to load data elements into a listview control? Are you familiar with how listview controls receive data elements?
Ese ochuko 11-Mar-13 18:43pm    
Thanks or your reply
The thing is, i have a datagrid that is binded to a table in sql database so onload of the form the datas are drawn to datagrid , now am trying to preview this data in an ordered maner, take for instance datagrid has two columns, id and name
so this is fixed now on selecting a row in grid the information selected should be previewed in the listview and when you move to the next, the data are also transfered to listview. example of what i want is how visual studio property window works. where when you click on a control it displays the properties of the control, i.e the Text, font, backcolor e.t.c and when you click on another control same thing happens. thanks

1 solution

You can try like that
protected void gvTest_RowCommand(object sender, GridViewCommandEventArgs e)
{
DataTable dt=new DataTable();
dt=ViewState["dt"] as DataTable;
if (e.CommandName == "Select")
{
int rowId = Convert.ToInt32( e.CommandArgument.ToString());
Label id = (Label)gvTest.Rows[rowId-1].FindControl("lblId");
var result =from row in dt.AsEnumerable() where row.Field<int32>("Id") ==Convert.ToInt32(id.Text) select row ;
if (result.Count() > 0)
{
lstTest.DataSource = result.CopyToDataTable();
lstTest.DataBind();
}
}
}
 
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