I am trying to code a sort routine against a dynamicaly populated gridview names Users. I have a syntax error in the C# code where I am trying to populate a column in a row of a the datatable.
for (int j = 0; j <= Users.Rows.Count - 1; j++)
{
DataRow dr = default(DataRow);
GridViewRow row = Users.Rows[j];
dr = dt.NewRow();
DataView dvUsers = new DataView(dt);
for (int i = 0; i <= row.Cells.Count - 1; i++)
{
dr.Item(i) = row.Cells[i].Text;
}
dt.Rows.Add(dr);
}
Error message:
System.Data.DataRow does not contain a definition for “Item”
Can someone help with the correct syntax to assign this value to the datarow?
I am new to C#
thank you,
You just need to change the line
to
Assuming the columns in the data table match the ordering of the cells in the table.
()are used to call methods, while[]indicates an indexer. This is the reason for the compile error. There isn’t a methodItemonDataRow, it is an indexer.EDIT
Changed
dr.Item[i]todr[i]. As explained in the comments,Itemis the default indexer, so you don’t specify the property, the index provided to the object itself will be applied to theItemindexer.