How to read data from a single datagridview column

I want to read data from a single datagridview column. My datagridview contains many columns, but I want to read all cells, but only from one column. I read all the columns using this code:

foreach (DataGridViewColumn col in dataGridView1.Columns) col.Name.ToString(); 

But I want to read the whole cell from a specific column.

+7
source share
4 answers

Perhaps this also helps. To get one cell:

 string data = (string)DataGridView1[iCol, iRow].Value; 

Then you can just drill rows and columns.

Documentation

+13
source

try it

 string data = string.Empty; int indexOfYourColumn = 0; foreach (DataGridViewRow row in dataGridView1.Rows) data = row.Cells[indexOfYourColumn].Value; 
+16
source

try it

 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 other way

  foreach (DataGridViewColumn col in dataGridView1.Columns) { if (col.Name == "MyColName") { //DO your Stuff here.. } } 
+2
source

To get the value of a clicked cell:

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { textBox1.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(); } 
0
source

All Articles