23

How can I read data from DataGridView in C#? I want to read the data appear in Table. How do I navigate through lines?

Kevin Chen
  • 994
  • 8
  • 24
sharon
  • 285
  • 1
  • 3
  • 6

5 Answers5

55

something like

for (int rows = 0; rows < dataGrid.Rows.Count; rows++)
{
     for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++)
    {
        string value = dataGrid.Rows[rows].Cells[col].Value.ToString();

    }
} 

example without using index

foreach (DataGridViewRow row in dataGrid.Rows)
{ 
    foreach (DataGridViewCell cell in row.Cells)
    {
        string value = cell.Value.ToString();

    }
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
CliffC
  • 903
  • 1
  • 10
  • 18
  • 5
    Instead of writing the type of a row or cell i.e. "DataGridViewRow" or "DataGridViewCell" respectively, you can simply write "var". – Kamran Bigdely Feb 27 '13 at 04:17
  • 1
    @kami if var is used, row.Cells throws an error because it thinks row is type object – Ravvy Aug 30 '16 at 00:58
10

If you wish, you can also use the column names instead of column numbers.

For example, if you want to read data from DataGridView on the 4. row and the "Name" column. It provides me a better understanding for which variable I am dealing with.

dataGridView.Rows[4].Cells["Name"].Value.ToString();

Hope it helps.

macrobook
  • 156
  • 3
  • 10
1

Code Example : Reading data from DataGridView and storing it in an array

int[,] n = new int[3, 19];
for (int i = 0; i < (StartDataView.Rows.Count - 1); i++)
{
    for (int j = 0; j < StartDataView.Columns.Count; j++)
    {
        if(this.StartDataView.Rows[i].Cells[j].Value.ToString() != string.Empty)
        {
            try
            {
                n[i, j] = int.Parse(this.StartDataView.Rows[i].Cells[j].Value.ToString());
            }
            catch (Exception Ee)
            { //get exception of "null"
                MessageBox.Show(Ee.ToString());
            }
        }
    }
}
Tassisto
  • 9,877
  • 28
  • 100
  • 157
Bibhu
  • 4,053
  • 4
  • 33
  • 63
  • I don't get the try-catch in your example. You should test to make sure the cell isn't null before parsing it. – carlbenson Jun 27 '11 at 03:12
1
string[,] myGridData = new string[dataGridView1.Rows.Count,3];

int i = 0;

foreach(DataRow row in dataGridView1.Rows)

{

    myGridData[i][0] = row.Cells[0].Value.ToString();
    myGridData[i][1] = row.Cells[1].Value.ToString();
    myGridData[i][2] = row.Cells[2].Value.ToString();

    i++;
}

Hope this helps....

Dulini Atapattu
  • 2,735
  • 8
  • 33
  • 47
0
 private void HighLightGridRows()
 {            
     Debugger.Launch();
     for (int i = 0; i < dtgvAppSettings.Rows.Count; i++)
     {
         String key = dtgvAppSettings.Rows[i].Cells["Key"].Value.ToString();
         if (key.ToLower().Contains("applicationpath") == true)
         {
             dtgvAppSettings.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
         }
     }
 }
Darren
  • 1,352
  • 5
  • 19
  • 49
Muhammad Mubashir
  • 1,591
  • 1
  • 21
  • 18