如何根据条件获取数据表的rowindex [英] How to get the rowindex of a datatable based on condition

查看:121
本文介绍了如何根据条件获取数据表的rowindex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我需要查找特定值所属的数据表的rowindex。

例如我有一个像这样的数据表

Hi Friends,

I need to find rowindex of the datatable for which a particular value falls.
For example i have a datatable like this

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
}



如果假设我有一个值说30000它落在第3层所以我应该得到结果为3如果60000结果应该是5



编辑代码

我可以使用以下代码根据条件过滤行


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

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.

推荐答案

试试这个...

你将在文本框中输入什么值,它会返回索引号。

如果断点是in orderform



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

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;
                }
            }


如果我理解你,你想获得索引。如果您的数据模型没有唯一键,那么实现这一目标的最佳方法是添加 AutoIncrement字段 [ ^ ]到 DataTable 。请参阅:创建自动增量列 [ ^ ]

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[^]
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:

//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"));
}


这篇关于如何根据条件获取数据表的rowindex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆