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:
foreach (DataGridViewColumn col in dataGridView1.Columns)
col.Name.ToString();
But I want to read all cell from particular column.
To get one cell: string data = (string)DataGridView1[iCol, iRow]. Value; Then you can simply loop rows and columns.
Use the SelectedColumns property. To enable users to select columns, you must set the SelectionMode property to FullColumnSelect or ColumnHeaderSelect.
You can get the selected row using the DataGridView. SelectedRows Collection. If your DataGridView allows only one selected, have a look at my sample. DataGridView.
Maybe this helps too. To get one cell:
string data = (string)DataGridView1[iCol, iRow].Value;
Then you can simply loop rows and columns.
Documentation.
Try this
string data = string.Empty;
int indexOfYourColumn = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
data = row.Cells[indexOfYourColumn].Value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With