C# 如何从datagridview的一列中读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14977697/
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
how to read data from one column of datagridview
提问by Sumit Goyal
I want to read data from one column of datagridview. My datagridview contains many columns but I want to read all cells but only from one column. I read all columns using this code:
我想从 datagridview 的一列中读取数据。我的 datagridview 包含许多列,但我想读取所有单元格,但只能从一列中读取。我使用此代码阅读了所有列:
foreach (DataGridViewColumn col in dataGridView1.Columns)
col.Name.ToString();
But I want to read all cell from particular column.
但我想读取特定列中的所有单元格。
采纳答案by SysDragon
Maybe this helps too. To get one cell:
也许这也有帮助。获取一个单元格:
string data = (string)DataGridView1[iCol, iRow].Value;
Then you can simply loop rows and columns.
然后您可以简单地循环行和列。
文档。
回答by yogi
Try this
尝试这个
string data = string.Empty;
int indexOfYourColumn = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
data = row.Cells[indexOfYourColumn].Value;
回答by Rohit
try this
尝试这个
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex == 0) //Set your Column Index
{
//DO your Stuff here..
}
}
}
or the other way
或者其他方式
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
if (col.Name == "MyColName")
{
//DO your Stuff here..
}
}
回答by user2673536
To get the value of the clicked cell:
要获取单击的单元格的值:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}