Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据多个条件筛选服务器_C#_Database_Winforms_Ms Access - Fatal编程技术网

C# 根据多个条件筛选服务器

C# 根据多个条件筛选服务器,c#,database,winforms,ms-access,C#,Database,Winforms,Ms Access,我有一个包含游戏及其服务器的数据库。我想让我的程序根据游戏和服务器位置以及游戏类型过滤数据库,但我似乎没有达到我的目标:(。 例如,我希望它显示位于英国的Minecraft服务器(在数据库中) 我正在构建一个Windows窗体应用程序 下面是一段代码: private void filter(string cOULMN,TextBox box) { DataView dvtble = tbl1.DefaultView; dvtble.RowFilter = (cOULMN+ " li

我有一个包含游戏及其服务器的数据库。我想让我的程序根据游戏和服务器位置以及游戏类型过滤数据库,但我似乎没有达到我的目标:(。 例如,我希望它显示位于英国的Minecraft服务器(在数据库中)

我正在构建一个
Windows窗体
应用程序

下面是一段代码:

private void filter(string cOULMN,TextBox box)
{
   DataView dvtble = tbl1.DefaultView;
   dvtble.RowFilter = (cOULMN+ " like '%" + box.Text + "%'");
}

private void button1_Click(object sender, EventArgs e)
{
     filter("Game", textBox4);
     filter("Location", textBox3);
     filter("Game Type", textBox2);
}

首先,我将创建一个类来保存所有数据库信息

public class Games
{
    public int ID { get; set; }
    public string GameName { get; set; }
    public string ServerLocation { get; set; }
    public string IP { get; set; }
}
接下来,我将制作一个
游戏列表
,以保存所有数据库项目

private List<Games> Game = new List<Games>();

如果您将数据转储到某种类型的类中,您可以使用Linq非常轻松地完成此操作。另一方面,像这样创建dataview可能也会有所帮助。
dataview dv=new-dataview(dataTable);
@Timmy我尝试过,但没有任何区别:(您的数据库包含哪些列?@Timmy thanx为此,我不知道LINQ功能:D
foreach (DataRow d in DataTableGames.Rows)
{
    Games g = new Games();
    g.ID = d.Field<int>("ID");
    g.GameName = d.Field<string>("Game");
    g.ServerLocation = d.Field<string>("ServerLocat");
    g.IP = d.Field<string>("iP");
    Game.Add(g);
}
private void btnFilter_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = Game.Where(l => l.ServerLocation.ToUpper() == txtLocationFlt.Text.ToUpper() 
                                            && l.GameName.ToUpper() == txtGameFlt.Text.ToUpper()).ToList();
}