如何在C#中按数据表的单元格值获取行索引 [英] How to get Row Index by cell value of Datatable in C#

查看:688
本文介绍了如何在C#中按数据表的单元格值获取行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,我有大约100行。我希望在Cell中为一个值的数据表选择查询以获取单元格值匹配的所有行。

I have a datatable and i have around 100 rows. i want to have a select query for datatable for one value in Cell to get all the rows where the value of the cell matches.

推荐答案

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


这篇关于如何在C#中按数据表的单元格值获取行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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