62

Here is a DataTable dt, which has lots of data.

I want to get the specific Cell Value from the DataTable, say Cell[i,j]. Where, i -> Rows and j -> Columns. I will iterate i,j's value with two forloops.

But I can't figure out how I can call a cell by its index.

Here's the code:

for (i = 0; i <= dt.Rows.Count - 1; i++)
{
    for (j = 0; j <= dt.Columns.Count - 1; j++)
    {
        var cell = dt.Rows[i][j];
        xlWorkSheet.Cells[i + 1, j + 1] = cell;
    }
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83

6 Answers6

142

The DataRow has also an indexer:

Object cellValue = dt.Rows[i][j];

But i would prefer the strongly typed Field extension method which also supports nullable types:

int number = dt.Rows[i].Field<int>(j);

or even more readable and less error-prone with the name of the column:

double otherNumber = dt.Rows[i].Field<double>("DoubleColumn");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Primerily, My job is done. Thanks/ But all my columns are not string. some are double and int. how could i use Field extension method? – Abdur Rahim Dec 11 '12 at 08:53
  • @decoyer: That's a generic method, you can use it with any type. Edited my answer to demonstrate it with `int` and `double`. – Tim Schmelter Dec 11 '12 at 08:56
  • Ok. Fine. But i am executing these codes in two loops(For). when j is changed, datatype may be changed. i am giving my code to make it better. I am editing my question. – Abdur Rahim Dec 11 '12 at 08:59
  • @decoyer: In that case, stay with the simple indexer since you don't know what type it is. – Tim Schmelter Dec 11 '12 at 09:07
14

You probably need to reference it from the Rowsrather than as a cell:

var cellValue = dt.Rows[i][j];
Chamila Chulatunga
  • 4,856
  • 15
  • 17
7

You can iterate DataTable like this:

private void button1_Click(object sender, EventArgs e)
{
    for(int i = 0; i< dt.Rows.Count;i++)
        for (int j = 0; j <dt.Columns.Count ; j++)
        {
            object o = dt.Rows[i].ItemArray[j];
            //if you want to get the string
            //string s = o = dt.Rows[i].ItemArray[j].ToString();
        }
}

Depending on the type of the data in the DataTable cell, you can cast the object to whatever you want.

Farhad
  • 4,119
  • 8
  • 43
  • 66
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
5

To get cell column name as well as cell value :

List<JObject> dataList = new List<JObject>();

for (int i = 0; i < dataTable.Rows.Count; i++)
{
    JObject eachRowObj = new JObject();

    for (int j = 0; j < dataTable.Columns.Count; j++)
    {
        string key = Convert.ToString(dataTable.Columns[j]);
        string value = Convert.ToString(dataTable.Rows[i].ItemArray[j]);

        eachRowObj.Add(key, value);

    }

    dataList.Add(eachRowObj);

}
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
2

You can call the indexer directly on the datatable variable as well:

var cellValue = dt[i].ColumnName
honk
  • 9,137
  • 11
  • 75
  • 83
Emre Inan
  • 21
  • 3
-2

If I have understood your question correctly you want to display one particular cell of your populated datatable? This what I used to display the given cell in my DataGrid.

var s  = dataGridView2.Rows[i].Cells[j].Value;
txt_Country.Text = s.ToString();

Hope this helps