使用 C# 遍历 gridview 的所有行和列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2023187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:46:31  来源:igfitidea点击:

loop through all rows and columns of a gridview using C#

c#gridviewfor-loop

提问by ACP

I have a grid view which contains 10 rows and 3 columns.. Now i want to loop through all rows and all columns of the gridview and want to add them to a datatable..

我有一个包含 10 行和 3 列的网格视图。现在我想遍历 gridview 的所有行和所有列,并希望将它们添加到数据表中。

  DataRow row;
  int rowscount = gv.Rows.Count;
    int columnscount = gv.Columns.Count;
    for (int i = 0; i < rowscount; i++)
    {
        for (int j = 0; j < columnscount; j++)
        {
            row = empTable.NewRow();
            row["a"] = gv.Rows[i][column1].Tostring();
            row["b"] = gv.Rows[i][column2].ToString();
            MynewDatatable.Rows.Add(row);
        }
    }    

gv - my gridview

gv - 我的网格视图

Now my question is , Can i get the value of all columns of all rows in gvto my new datatable.. I dont know whether my loop is correct or not... I am using this datatable for a bulkcopy function...

现在我的问题是,我能否将所有行的所有列的值都放入gv我的新数据表中......我不知道我的循环是否正确......我正在将此数据表用于批量复制功能......

采纳答案by JMD

  • Your code above is creating a new Row for every cell in the GridView.
  • within each row of the GridView your code is assigning every value in the row to the same column, Emp_Name.
  • 您上面的代码正在为 GridView 中的每个单元格创建一个新行。
  • 在 GridView 的每一行中,您的代码将行中的每个值分配给同一列 Emp_Name。

Corrected, I think:

更正,我认为:

int rowscount = gv.Rows.Count;
int columnscount = gv.Columns.Count;
for (int i = 0; i < rowscount; i++)
{
    // Create the row outside the inner loop, only want a new table row for each GridView row
    DataRow row = empTable.NewRow();
    for (int j = 1; j < columnscount; j++)
    {
        // Referencing the column in the new row by number, starting from 0.
        row[j - 1] = gv.Rows[i][j].Tostring();
    }
    MynewDatatable.Rows.Add(row);
}

EDIT: You've been editing the source in your question, so my answer may grow out-of-date. :)

编辑:您一直在编辑问题中的来源,所以我的回答可能会过时。:)

回答by Bobby

Your loop is incorrect, all lists start at 0. But you start at 1 when looping through the columns.

您的循环不正确,所有列表都从 0 开始。但是在循环列时您从 1 开始。

回答by Rafa Castaneda

The JMD answer is correct, but depending on your use-case, maybe databinding the DataSource of your grid view to your DataTable would be a better approach.

JMD 答案是正确的,但根据您的用例,也许将网格视图的 DataSource 数据绑定到 DataTable 是更好的方法。

回答by sandeep

Try this code:

试试这个代码:

        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            string selectedempid = dataGridView1.SelectedRows[0].Cells["Deptno"].Value.ToString();
            {
                SqlCommand cmd = new SqlCommand("delete from Test_dept where Deptno=" + selectedempid, con);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                MessageBox.Show("deleted");

            }
        }