如何遍历datagrid中的每个单元格和行 [英] how to loop through each cells and rows in datagrid

查看:443
本文介绍了如何遍历datagrid中的每个单元格和行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i希望循环遍历datagrid中的每个单元格和行,以便将值

插入数据库。我知道如何在数据网格视图中执行此操作但在数据网格中我不要知道

如果有人知道帮我示例代码

解决方案
对(INT I = 0; I< gridview_id.Rows .Count; i ++)

{

for(int j = 1; j< gridview_id.Columns.Count; j ++)

{

//对单元格gridview_id.Columns [j]执行任何所需的操作。

}

}



或者尝试以下方法:

  foreach (GridViewRow行 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;
}
}


首先检查行号,然后按照以下步骤迭代每一行:

WPF通过数据网格 [ ^ ]

迭代到在数据网格WPF [ ^ ]




在这种情况下,我们知道 gridview cell 的数量,但是我们不知道 gridview ,假设我们每行有三个单元格,那么来自这些单元格的值可以得到如下

  foreach (DataGr idViewRow dr  in  dataGridView.Rows)
{
string cell1 = dr.Cells [ cell1]。Value.ToString();
string cell2 = dr.Cells [ 小区2\" ] Value.ToString();
string cell3 = dr.Cells [ CELL3\" ] Value.ToString();
}

这里 cell1 cell2 cell3 是gridview单元格的ID。

希望这对你有帮助。


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.

解决方案

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:

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;
    }
}


First check out the row numbers and then iterate each row following these:
WPF iterate through datagrid[^]
Iterate to all cells in WPF Datagrid [^]


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

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.


这篇关于如何遍历datagrid中的每个单元格和行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆