Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Friends,

I need to find rowindex of the datatable for which a particular value falls.
For example i have a datatable like this
C#
DataTable dt = new DataTable();
dt.Columns.Add("Breakpoint", typeof(Int32));
dt.Columns.Add("rate", typeof(double));          

dt.Rows.Add(10000,5.5);
dt.Rows.Add(20000,4);
dt.Rows.Add(35000,3);
dt.Rows.Add(50000, 2.5);
dt.Rows.Add(100000, 1);

int indexno=0;//Here i need to find the row index.

for(int i=indexno;i<dt.Rows.Count;i++)
{
    //some calculations
}

If Suppose i have a value say 30000 it falls on the 3rd tier so i should get the result as 3 and if 60000 the result should be 5

Edited Code
I can filter the row based on condition using below code
C#
DataRow[] result = dt.Select("Breakpoint >= 30000");
foreach (DataRow row in result)
{
Console.WriteLine("{0}, {1}", row[0], row[1]);
}

But my requirement is to get index no because in some cases i need to reverse the loop.
Posted
Updated 18-Apr-15 4:51am
v3
Comments
[no name] 18-Apr-15 10:38am    
I suggest you to redesign and Forget about rowindex.
jinesh sam 18-Apr-15 10:45am    
I can do something like this. But i need the index no. i some cases i need to reverse the loop

DataRow[] result = dt.Select("Breakpoint >= 30000");
foreach (DataRow row in result)
{
Console.WriteLine("{0}, {1}", row[0], row[1]);
}
Sinisa Hajnal 18-Apr-15 16:25pm    
Why not use dt.Rows.IndexOf(result[0])?
jinesh sam 19-Apr-15 13:58pm    
Hi Sinisa... Its works fine. Thanks:)

Try this...
what ever value will u enter in textbox it will return you the index number.
This code will work if Breakpoint is in orderform

C#
DataTable dt = new DataTable();
            dt.Columns.Add("Breakpoint", typeof(Int32));
            dt.Columns.Add("rate", typeof(double));

            dt.Rows.Add(10000, 5.5);
            dt.Rows.Add(20000, 4);
            dt.Rows.Add(35000, 3);
            dt.Rows.Add(50000, 2.5);
            dt.Rows.Add(100000, 1);

            foreach (DataRow row in dt.Rows)
            {
                if (int.Parse(row[0].ToString()) >= int.Parse(textBox1.Text))
                {
                    label1.Text = row[1].ToString() + " ";
                    break;
                }
          }

//if your record in datatable is not in proper order than use this code

DataTable dt = new DataTable();
            dt.Columns.Add("Breakpoint", typeof(Int32));
            dt.Columns.Add("rate", typeof(double));

            dt.Rows.Add(10000, 5.5);
            dt.Rows.Add(20000, 4);
            dt.Rows.Add(50000, 2.5);
            dt.Rows.Add(35000, 3);
            dt.Rows.Add(100000, 1);
            DataRow[] OrderedRows = dt.Select().OrderBy(u => u["Breakpoint"]).ToArray();

            foreach (DataRow row in OrderedRows)
            {
                if (int.Parse(row[0].ToString()) >= int.Parse(textBox1.Text))
                {
                    label1.Text = row[1].ToString() + " ";
                    break;
                }
            }
 
Share this answer
 
v5
If i understand you well, you want to get index. The best way to achieve that, if your data model does not have unique key, is to add AutoIncrement field[^] to the DataTable. See: Creating AutoIncrement Columns[^]
C#
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("index", typeof(Int32));
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
dt.Columns.Add(dc);
dc = new DataColumn("Breakpoint", typeof(Int32));
dt.Columns.Add(dc);
dc = new DataColumn("rate", typeof(double));          
dt.Columns.Add(dc);

dt.Rows.Add(null, 10000,5.5);
dt.Rows.Add(null, 20000,4);
dt.Rows.Add(null, 35000,3);
dt.Rows.Add(null, 50000, 2.5);
dt.Rows.Add(null, 100000, 1);


Result:
index Breakpoint rate
1     10000      5,5
2     20000      4
3     35000      3
4     50000      2,5
5     100000     1


Now, you can loop through the records (no matter of drirection) and the index is always the same ;)

Check this:
C#
//using Linq query
var qry = dt.AsEnumerable()
        .Where(r => r.Field<int>("Breakpoint")>30000);
//display rows which meet condition
foreach(var row in qry)
{
    Console.WriteLine("{0}: {1}", row.Field<int>("index"), row.Field<int>("Breakpoint"));
}
 
Share this answer
 

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