VoVb
VoVb

Reputation: 89

Filtering server according to multiple conditions

I have a database containing games and their servers. I want to make my program filter the database according to game and server location, and game type, but I don't seem to reach my goal :(. for example i want it to show Minecraft servers which are located in UK(within the database)

I'm building a Windows Forms application.

Here's chunk of the code:

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

Upvotes: 1

Views: 46

Answers (1)

Timmy
Timmy

Reputation: 553

To start, I would make a class to hold all your Database information

public class Games
{
    public int ID { get; set; }
    public string GameName { get; set; }
    public string ServerLocation { get; set; }
    public string IP { get; set; }
}

Next I would make a List of Games to hold all your database items

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

From here populate the List using that information from the database. There are tons of ways to do this. This is just one option.

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

After you have your list populated with all the fields from the database, you can use LINQ to essentially filter. In my example, I'm using a button like you originally did, but I'm putting mine into a datagridview for visibility. You can do whatever you please with it.

I'm also only filtering using a ServerLocation textbox and a GameName textbox. You can decide what and how you would like to filter using LINQ queries. LINQ is much more powerful than this example shows.

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

Upvotes: 1

Related Questions