Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi

i have datatable which contain n numer of row

i want to know the indexfof that row if that row ItemArray contains suppose "a"

please help me out..
Posted
Updated 19-Nov-16 4:44am
Comments
Salman622 17-Dec-13 8:40am    
is there any condition when it should return a Row index
rahuls1 17-Dec-13 8:41am    
ya if that constan is fouund in row ItemArray ...

DataTable Dtb1 = new DataTable();
BindingSource Bnd1= new BindingSource();
Bnd1.DataSource = Dtb1;
int RowNo = Bnd1.Find("MyFieldName", 'a');
if (RowNo != -1)
{
//RowNo is that Row
MyResult=Dtb1.Rows[RowNo]["MyFieldName"].ToString();
}
 
Share this answer
 
Try:
C#
int index = -1;
DataRow[] rows = dt.Select("MyColumnName Like '%a%'");
if (rows.Count() > 0)
    {
    index = myDataTable.Rows.IndexOf(rows[0]);
    }
 
Share this answer
 
if you have primary key column in the data table you can use
DataRow dr = DataTable1.Rows.Find([primary key value]);

which will give you the datarow object.and after can use method IndexOf available Rows

Datatable1.Rows.IndexOf(dr);
 
Share this answer
 
My Solution in vb.net
VB
Dim drow As DataRow = datatable1.Select("YourColumn='" & YourValueString & "'")(0)
Dim tempIndex as integer =datatable1.Rows.IndexOf(drow)


in C#:
C#
DataRow drow = datatable1.Select(("YourColumn=\'" 
                + (YourValueString + "\'")))[0];
int tempIndex = datatable1.Rows.IndexOf(drow);
 
Share this answer
 
Comments
Richard MacCutchan 19-Nov-16 10:55am    
This question was answered almost 3 years ago. Please do not re-open old questions, especially ones that have already been answered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900