Filtering data in datagridview using 2 combobox in c# with MS Access DB

Jamie Hayashi 121 Reputation points
2022-04-17T12:42:05.903+00:00

Good day everyone, please help me this problem.
color green represent other combo box and also color blue.

I want to filter the data in datagridview with different combo box.

fill free to message me in my gmail account jamei432294@Stuff .com
Thank you so much.

Here my code in Room Type combobox.

        con.Open();//filter  
        string query = "select * from CheckInRoom where RType = '" +cmbRtype.SelectedItem.ToString() +"'";  
        OleDbDataAdapter da = new OleDbDataAdapter(query, con);  
        OleDbCommandBuilder builder = new OleDbCommandBuilder(da);  
        var ds = new DataSet();  
        da.Fill(ds);  
        CheckIndataGridView1.DataSource = ds.Tables[0];  
        con.Close();  

193637-untitled.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,465 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
841 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,286 Reputation points
    2022-04-17T14:23:27.21+00:00

    The following works with any database as at the time used we are not connected to the database at all. You can see a Microsoft article on this code.

    The best way to filter is adding a BindingSource, set it's DataSource to ds.Tables[0] then use my language extension.

    public static void RowFilterTwoConditions(this BindingSource sender, string field1, string value1, string pField2, string value2, bool caseSensitive = false)
    {
        sender.DataTable().CaseSensitive = caseSensitive;
        sender.DataView().RowFilter = $"{field1} = '{value1.EscapeApostrophe()}' AND {pField2} = '{value2.EscapeApostrophe()}'";
    }
    

    Lets call the BindingSource _bindingSource

    public partial class Form1 : Form
    {
        private readonly BindingSource _bindingSource = new BindingSource();
    

    Mocked up call to filter

    _bindingSource.RowFilterTwoConditions(
        "RoomType", 
        "Value from ComboBox", 
        "SmokingRoom", 
        "Value from smoking room ComboBox");
    

    Another extension to clear the filter

    public static void RowFilterClear(this BindingSource sender)
    {
        sender.DataView().RowFilter = "";
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful