根据条件过滤数据表

时间:2016-04-12 20:43:11

标签: c# asp.net datatable dropdown

我有一个包含PrinterName,Ip,description和ConnectionStringID

列表的文本文件

示例

Name,Ip,Description,connectionstringID
gkbzpl01,12.43.13,425,2nd floor,Datatable
ackzpl201,13.44.65,324,1st floor,Datatable

我将其加载到下拉列表中:

 protected void LoadPrinterList()
    {

        string CSVFilePathName = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
        string[] Lines = File.ReadAllLines(CSVFilePathName);
        string[] Fields;
        Fields = Lines[0].Split(new char[] { ',' });
        int Cols = Fields.GetLength(0);
        DataTable dt = new DataTable();
        //1st row must be column names; force lower case to ensure matching later on.
        for (int i = 0; i < Cols; i++)
            dt.Columns.Add(Fields[i].ToLower(), typeof(string));
        dt.Columns.Add("nameanddescription", typeof(string), "name +'-'+ description");
        dt.Columns.Add("ipandconnectionstring", typeof(string), "ip +'-'+ ConncetionStringID");
        DataRow Row;
        for (int i = 1; i < Lines.GetLength(0); i++)
        {
            Fields = Lines[i].Split(new char[] { ',' });
            Row = dt.NewRow();
            for (int f = 0; f < Cols; f++)
                Row[f] = Fields[f];
             dt.Rows.Add(Row);
        }

        string hostname = Request.UserHostName.Substring(0, 3);
        string[] name = Printerlist.SelectedValue.Split('-');
        //string plant = name[0].ToString();
        //string plantvalue = plant.Substring(0, 3);
        //if(hostname == plantvalue)
        //{
        Printerlist.DataTextField = "nameanddescription";
        Printerlist.DataValueField = "ipandconnectionstring";
        //}
        Printerlist.DataSource = dt;
        Printerlist.DataBind();


    }

以上工作非常精彩,但现在我只需要显示与该特定工厂相关的标签打印机,总共有4个工厂,每个工厂有5台或更多台打印机和2台测试打印机,我可以访问企业建设。

所以这就是我到目前为止所拥有的

 string servername = HttpContext.Current.Request.Url.Host;
            string firstthreeserver = servername.Substring(0, 3);
            string clientname = HttpContext.Current.Server.MachineName;
            string firstthreeclient = clientname.Substring(0, 3).ToLower();
            if (firstthreeserver.ToLower() == firstthreeclient.ToLower())
            {
                DataRow[] result = dt.Select("nameanddescription Like '%" + firstthreeclient + "%'");
                for (int i = 1; i < Lines.GetLength(0); i++)
                {
                    Fields = Lines[i].Split(new char[] { ',' });
                    Row = dt.NewRow();
                      for (int f = 0; f < Cols; f++)
                          Row[f] = Fields[f];
                      dt.Rows.Add(result);
                }
            }
                else
                {
                    string test = "tes".ToLower();
                    foreach (var row in dt.Select("nameanddescription like '%" + test  + "%'" ))
                    {
                         dt.ImportRow(row);
                    }
                }

所以我得到服务器名称(gkbserv)和从服务器访问应用程序的客户端(gkb001)我取前三个字母并比较如果上面是真的然后在下拉列表中显示gkbzpl01-2ndfloor。如果上述情况不成立,则显示两台测试打印机。 如何过滤数据表只显示满足相同条件的记录?

2 个答案:

答案 0 :(得分:1)

您可以使用DataTable.Select方法过滤满足条件的行。您可以阅读更多详细信息here。有关使用过滤器的信息,请参阅here

答案 1 :(得分:0)

我认为这可以提供帮助:DataTable Select Implementation with Multiple AND Conditions(2和3-d评论)

 IEnumerable<DataRow> rows = myDataTable.AsEnumerable()
.Where(r => r.Field<string>("Field2") == term2
        &&  r.Field<string>("Field3") == term3
        &&  r.Field<string>("Field1").Contains(term1));

 DataRow [] results = myDataTable
 .Select("Field3 = '" + term3+ "'")
 .Select("Field2 = '" + term2 + "'")
 .Select("Field1 LIKE '*" + term1 + "*'");