rohit singh
rohit singh

Reputation: 65

How to select a particular data from a datatable?

I have three select statements,

select 1 from tbl1;  
select 2 from tbl2; 
select 3 from tbl3;

and in my front end i use a Datatable to retrieve the value from sql.

 datatable dt = new datatable(); dt = obj.funcgetval();
 gridview.DataSource = dt; gridview.Databind();

I want to select a particular data from the datatable like in a dataset we use ds.tables[0], how can we apply the same in a datatable?

Upvotes: 0

Views: 11642

Answers (4)

coder
coder

Reputation: 2000

If you want select specific cell value from Datatable,you can use..

dt.Rows[row index]["Column name"].TOString();

like if you want to select 1st row value from column say Name then use

dt.Rows[1]["Name"].TOString();

if u want select particular row itself you can use Select with filtercondition.

 string FilterCond = ur condition;
 DataRow[] myrow = dt.Select(FilterCond);

Upvotes: 0

Adil
Adil

Reputation: 148110

You can use combination of Rows and Columns to read / write particular cell of datatable.

dataTable.Rows[rowsIndex].Column["ColumnName"] = "SomeValue";
string strValue = dataTable.Rows[rowsIndex].Column["ColumnName"].ToString();

To iterate through dataTable

foreach(DataRow row in dt.Rows)
{ 
    string name = row["name"].ToString();
    string description = row["description"].ToString();
}

Upvotes: 1

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

This should work for you:

dt.Rows[rowindex]["ColumnName"].ToString();

Upvotes: 0

Esha Garg
Esha Garg

Reputation: 144

If you want to select any row then use

DataRow dr=dt.Rows[Rowindex];

If you want to get any column value then

object obj=dt.Rows[Rowindex][columnindex];

or

object obj=dt.Rows[Rowindex]["ColumnName"];

Upvotes: 0

Related Questions