Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Sqllite database and filtering a datagridview using five comboboxes and its working fine but my problem is when I dont select value in any of the combobox it does'nt work coz I am using AND and if I'll use OR it'll not give me desired result coz I want to filter using all the selected values in the combobox. My code is as follows:
DataView dv = new DataView();
        string voterlist = "select * from voters";
        DataTable dt = Database.myAdapter(voterlist);
        dv = dt.DefaultView;
        dataGridView1.DataSource = dv;
        dv.RowFilter = string.Format("consname= '{0}' AND area = '{1}' AND pollingstn = '{2}' AND wardno = '{3}' AND policestn = '{4}'", combo_consname.SelectedItem, combo_Area.SelectedItem, combo_Polling.SelectedItem, combo_Ward.SelectedItem, combo_Police.SelectedItem);
Posted

1 solution

Just be a little more thoughtful about it:
C#
    StringBuilder sb = new StringBuilder();
    bool join = false;
    join = AddFilterData(sb, "cosname", combo_cosname.SelectedItem, join);
    ...
    dv.RowFilter = sb.ToString();
    ...

private bool AddFilterData(StringBuilder sb, string field, object value, bool join)
    {
    if (value != null)
        {
        string s = value.ToString();
        if (!string.IsNullOrEmpty(s))
            {
            if (join)
                {
                sb.Append(" AND ");
                }
            sb.AppendFormat("{0}='{1}' ", field, s);
            join = true;
            }
        }
    return join;
    }
 
Share this answer
 
Comments
lucky050 20-Apr-13 10:48am    
Wow., Thank you so much . You helped me a lot. It worked excellent .
OriginalGriff 20-Apr-13 10:55am    
:laugh: You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900