Datatable Row Number

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,
I am using a datatable to store some data from the xml.
I need the row number where a particular string exists
(Note: All values in datatable are unique).
Please help me in this regard.
 
GhanaShyam said:
Hi All,
I am using a datatable to store some data from the xml.
I need the row number where a particular string exists
(Note: All values in datatable are unique).
Please help me in this regard.

There is no row index property as such.
A possible workaround is using a counter, looping through the datatable,
incrementing the counter until the desired row is reached.

FUnky
 
Do you mean the index of the row in the table's row collection?

If so, and the data is unique, then you could use DataTable.Select() to find
the row matching your criteria and then get the index of this row within the
collection, as follows. Note Select() returns an array or rows matching the
specified criteria, hence the indexer:

// create a table
DataTable dt = new DataTable();
dt.Columns.Add("MyColumn", typeof(string));

// add some data
dt.Rows.Add(new object[] { "Hello" });
dt.Rows.Add(new object[] { "World" });

// find a row
int rowIndex = dt.Rows.IndexOf(dt.Select("MyColumn = 'World'")[0]);

hth

kh
 
Back
Top