How to get index/indices of a row in a DataTable
-
Hi, is there any way to get index/indices of a row where the content of a column is as specified. For example, the following table named "employee" contains 3 columns with 4 datarows.
[employee]
(emp_id)•(emp_name)•(salary)
00001 Johan 30000
00002 Joseph 35000
00003 Jack 28000
00004 Jane 32000I'd like to find the index of a row where emp_name is "Joseph". Is there any way to get it? I've tried to use IndexOf() method to get it but it asks for a datarow as its parameter which i don't even know where it is in the table -_-"
DataTable dt = ds.Tables["employee"];
int i = dt.Rows.IndexOf(); <-- don't know what to put here.Thank you so much. KiT
-
Hi, is there any way to get index/indices of a row where the content of a column is as specified. For example, the following table named "employee" contains 3 columns with 4 datarows.
[employee]
(emp_id)•(emp_name)•(salary)
00001 Johan 30000
00002 Joseph 35000
00003 Jack 28000
00004 Jane 32000I'd like to find the index of a row where emp_name is "Joseph". Is there any way to get it? I've tried to use IndexOf() method to get it but it asks for a datarow as its parameter which i don't even know where it is in the table -_-"
DataTable dt = ds.Tables["employee"];
int i = dt.Rows.IndexOf(); <-- don't know what to put here.Thank you so much. KiT
DataTable dt=ds.Tables["employee"]; DataRow[] arr=dt.Select("emp_name='Joseph'"); textBox1.Text=arr[0].ItemArray["emp_id"].ToString();
Worth noting, if you have several persons named Joseph you simply can not take the first one. To obtain uniqueness it is better to work with the id-column. HTH