Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want to loop through each cells and rows in the datagrid to insert the value
to the database .i know how to do it in datagrid view but it in datagrid i don't know
if anyone knows help me with sample code.
Posted
Updated 26-Feb-20 15:49pm

for (int i = 0; i < gridview_id.Rows.Count; i++)
{
for (int j = 1; j < gridview_id.Columns.Count; j++)
{
// perform any required operation on cell gridview_id.Columns[j].
}
}

Or try the following:
C#
foreach(GridViewRow row in gridview_id.Rows)
{
    for(int i = 0; i < gridview_id.Columns.Count, i++)
    {
        String header = gridview_id.Columns[i].HeaderText;
        String cellText = row.Cells[i].Text;
    }
}
 
Share this answer
 
v2
Comments
Charles Clayton 16-Jan-15 18:22pm    
Again, this is a DataGrid for WPF, not a DataGridView.
First check out the row numbers and then iterate each row following these:
WPF iterate through datagrid[^]
Iterate to all cells in WPF Datagrid [^]
 
Share this answer
 
Hi,
In such cases we know number of cell in gridview but we dont know no of rows in the gridview, assuming we have three cells in each row then values from these cells can get like below
C#
foreach (DataGridViewRow dr in dataGridView.Rows)
{
    string cell1 = dr.Cells["cell1"].Value.ToString();
    string cell2 = dr.Cells["cell2"].Value.ToString();
    string cell3 = dr.Cells["cell3"].Value.ToString();
}
Here cell1, cell2 and cell3 are the Ids of gridview cells.
hope this help you.
 
Share this answer
 
Comments
prabhakar78 3-Oct-13 3:28am    
hi i want to do it in datagrid not datagridview
tanweer 3-Oct-13 3:54am    
Hi, these two controls are not far different, just change DataGridViewRow to GridViewRow will solve your problem

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