对于DataGridView,如何获取每行的值? [英] For a DataGridView, how do I get the values from each row?
本文介绍了对于DataGridView,如何获取每行的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在想什么,但是我不真的很喜欢它,因为如果我重新排列列,那么代码也将被更改。
for(int i = 0; i< dataGridView.RowCount; i ++)
{
string Value0 = dataGridView1.Rows [i] .Cells [0];
string Value1 = dataGridView1.Rows [i] .Cells [1];
string Value2 = dataGridView1.Rows [i] .Cells [2];
}
解决方案
您可以使用 foreach
遍历 DataGridView
中的 DataGridViewRow
,并使用列名称以从单元格
中检索值:
foreach(DataGridViewRow在dataGridView.Rows中)
{
string col1 = dr.Cells [Col1]。Value.ToString();
string col2 = dr.Cells [Col2]。Value.ToString();
string col3 = dr.Cells [Col3]。Value.ToString();
}
I am wondering what is the best way to go iterate through all the rows in a datagridview and get the values from the cells.
Here is what I am thinking of doing, but I don't really like it because if I rearrange the columns then the code will also have to be changed.
for (int i = 0; i < dataGridView.RowCount; i++)
{
string Value0 = dataGridView1.Rows[i].Cells[0];
string Value1 = dataGridView1.Rows[i].Cells[1];
string Value2 = dataGridView1.Rows[i].Cells[2];
}
解决方案
You can use a foreach
to iterate over the DataGridViewRow
s within the DataGridView
and use the column names to retrieve the values from the Cells
:
foreach (DataGridViewRow dr in dataGridView.Rows)
{
string col1 = dr.Cells["Col1"].Value.ToString();
string col2 = dr.Cells["Col2"].Value.ToString();
string col3 = dr.Cells["Col3"].Value.ToString();
}
这篇关于对于DataGridView,如何获取每行的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文