且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中按数据表的单元格值获取行索引

更新时间:2023-01-25 21:18:53

using System;
using System.Data;

class Program
{
    static void Main()
    {
    // Create a table of five different people.
    // ... Store their size and sex.
    DataTable table = new DataTable("Players");
    table.Columns.Add(new DataColumn("Size", typeof(int)));
    table.Columns.Add(new DataColumn("Sex", typeof(char)));

    table.Rows.Add(100, 'f');
    table.Rows.Add(235, 'f');
    table.Rows.Add(250, 'm');
    table.Rows.Add(310, 'm');
    table.Rows.Add(150, 'm');

    // Search for people above a certain size.
    // ... Require certain sex.
    DataRow[] result = table.Select("Size >= 230 AND Sex = 'm'");
    foreach (DataRow row in result)
    {
        Console.WriteLine("{0}, {1}", row[0], row[1]);
    }
    }
}