question

JamieHayashi-3240 avatar image
0 Votes"
JamieHayashi-3240 asked JamieHayashi-3240 commented

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

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@gmail.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



dotnet-csharpoffice-access-dev
untitled.png (14.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered JamieHayashi-3240 commented

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 = "";
 }


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Attention to karenpayneoregon,

Ma'am Good day in this line i have a error.

sender.DataView().RowFilter = $"{field1} = '{value1.EscapeApostrophe()}' AND {pField2} = '{value2.EscapeApostrophe()}'";

0 Votes 0 ·

My guess is you need this

 public static class StringExtensions
 {
     public static string EscapeApostrophe(this string sender)
         => sender.Replace("'", "''");
 }
0 Votes 0 ·

Ma'am good day, Can I ask you a favor. a simple application on this. Because it's so hard to understand your code ma'am. I am a beginner only ma'am.
I hope you understand my felling's right now ma'am.

Advance thank you so much ma'am.

0 Votes 0 ·