Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access cell value of datatable

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?

Thanks.

like image 855
el ninho Avatar asked Jan 26 '12 17:01

el ninho


2 Answers

If you need a weak reference to the cell value:

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

or

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)

like image 77
vc 74 Avatar answered Oct 07 '22 04:10

vc 74


string abc= dt.Rows[0]["column name"].ToString(); 
like image 28
ashok luhach Avatar answered Oct 07 '22 03:10

ashok luhach