Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read data from one column of datagridview

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.

like image 740
Sumit Goyal Avatar asked Feb 20 '13 10:02

Sumit Goyal


People also ask

How to get column value in DataGridView c#?

To get one cell: string data = (string)DataGridView1[iCol, iRow]. Value; Then you can simply loop rows and columns.

How to select a column in DataGridView c#?

Use the SelectedColumns property. To enable users to select columns, you must set the SelectionMode property to FullColumnSelect or ColumnHeaderSelect.

How to get Selected row in DataGridView c# Windows application?

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.


2 Answers

Maybe this helps too. To get one cell:

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

Then you can simply loop rows and columns.

Documentation.

like image 101
SysDragon Avatar answered Oct 22 '22 15:10

SysDragon


Try this

string data = string.Empty;
int indexOfYourColumn = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
   data = row.Cells[indexOfYourColumn].Value;
like image 23
yogi Avatar answered Oct 22 '22 16:10

yogi