Does anyone know how to get the value of a datatable row column using the column name.
I know using VB it is:
Dim var as string
var = DT.Rows(0).Item("PricePerItem")
but I need the c# code to do the same thing.
Thanks
.NET ProgrammingC#
Last Comment
trstill
8/22/2022 - Mon
jhshukla
string s = DT.Rows[0]["PricePerItem"];
or
string s = DT.Rows[0].Items["PricePerItem"];
jhshukla
Noticed typo. Second one should be:
string s = DT.Rows[0].Item["PricePerItem"];
ihshukla,
Thanks for the answer and it does work.
I should have given more info.
This is what I'm actually trying to do by looping thru a datatable:
DT is a Datatable
foreach (DataRow row in DT.Rows)
{
String s row.item["PriceToItem"].ToString;
}
But I get system.data.datarow does not contain a definition for item
jhshukla
Can you place a breakpoint and check names of all columns in the datatable? There could be a few things wrong:
1. Typo when creating column names
2. Typo in code (PricePerItem vs PriceToItem)
2. Some columns were excluded in the query.
trstill
ASKER
jhshukla,
Thanks again for the reply.
I can do what you suggest but the column name is definitely correct.
The actual error is a compiler error that seems to be stating
the .item in row.item is not a valid definition or variable.
or
string s = DT.Rows[0].Items["PricePer