Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get row value by just giving column name in DataTable

Tags:

c#

datatable

Is it possible to get a row value by giving column name when DataTable holds a single row, without iteration.

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

Table

I need Something like below without loop when we have only one row,

String cn=row["ColumnName"].ToString()
like image 951
TestQTP Avatar asked Aug 19 '16 13:08

TestQTP


People also ask

How can I search a value in DataTable with DataTable?

I want to search that value in DataTable and get the row and column indexes of that value. How Can i accomplish this with datatable? You can use search () to search the table which would then put those matching rows into the table. table.rows ( {search:'applied'} ).indexes () would return the row index of those matching records.

How do I get a list of rows in a table?

We can use .loc [] to get rows. Note the square brackets here instead of the parenthesis (). The syntax is like this: df.loc [row, column]. column is optional, and if left blank, we can get the entire row.

How to search whole DataTable and get row and column indexes?

I just want to search whole datatable (under whole i mean whole table data, ignoring currently applied by search val value) and get row and column indexes. How can i accomplish that? You can just get the data in an array with something like, table.rows ().data (), and iterate through the array to see if it matches.

How to get out_DT value from column name?

out_value = out_dt.Rows (0) (“column_name”).ToString Cheers… You can get it two ways either using column name or column index. row (“ColumnName”).Tostring Please let me know if you face any issue.


1 Answers

This is the way:

string Text = dataTable.Rows[0]["ColumnName"].ToString();
like image 133
Paul Pérez Avatar answered Oct 13 '22 06:10

Paul Pérez