c# particular Access cell value of datatable

how to get first row from datatable in c# (5)

public V[] getV(DataTable dtCloned)

    V[] objV = new V[dtCloned.Rows.Count];
    MyClasses mc = new MyClasses();
    int i = 0;
    int intError = 0;
    foreach (DataRow dr in dtCloned.Rows)
            V vs = new V();
            vs.R = int.Parse(mc.ReplaceChar(dr["r"].ToString()).Trim());
            vs.S = Int64.Parse(mc.ReplaceChar(dr["s"].ToString()).Trim());
            objV[i] = vs;
        catch (Exception ex)
            DataRow row = dtError.NewRow();
            row["r"] = dr["r"].ToString();
            row["s"] = dr["s"].ToString();
    return vs;

Can anyone help me how to access for example value of first cell in 4th column?

a b c d
1 2 3 5
g n m l

for example, how to access to value d, if that would be datatable?


Answer #1

You can also try (first cell in 4th column):


Answer #2
foreach(DataRow row in dt.Rows)
    string value = row[3].ToString();

Answer #3
string abc= dt.Rows[0]["column name"].ToString();

Answer #4

If you need a weak reference to the cell value:

object field = d.Rows[0][3]


object field = d.Rows[0].ItemArray[3]

Should do it

If you need a strongly typed reference (string in your case) you can use the DataRowExtensions.Field extension method:

string field = d.Rows[0].Field<string>(3);

(make sure System.Data is in listed in the namespaces in this case)

Indexes are 0 based so we first access the first row (0) and then the 4th column in this row (3)