怎么做才能最小化控件上if else的条件

时间:2016-12-05 11:45:34

标签: c# if-statement datagridview controls

enter image description here

我有网格视图,其中所有问题都已从访问数据库中显示。现在我想在给定的文本框中执行搜索操作,用户可以在1个文本框中输入数据,或者在所有文本框中输入数据,具体取决于用户需求。我的问题是条件已经过多少次无论用户在任何文本框中提供信息,都要进行相应的过滤。

例如:用户只提供标准和标记而不是过滤必须执行标准=“给定值”和标记=“给定值”

我已经为每个控件提供了各种条件,但它的编码太大了。现在想要最小化这个,所以任何建议都必须推荐。

我的代码:

private void txt_marks_TextChanged(object sender, EventArgs e)
        {
            string marks = Convert.ToString(txt_marks.Text);
            string q_type = Convert.ToString(cmbQType.SelectedValue);
            if (q_type.Contains("[") || q_type.Contains("]") || q_type.Contains("*") || q_type.Contains("%"))
            {
                q_type = replacestring(q_type);
            }
            if (btnlanguage.Text != "" && txt_sub.Text != "" && txt_std.Text != "" && cmbQType.SelectedIndex != -1 && txt_marks.Text != "")
            {
                DataTable dt = main_ds.Tables[0];
                dt.DefaultView.RowFilter = String.Format("Subject  Like '" + txt_sub.Text.ToString() + "%' and Standard Like '" + txt_std.Text.ToString() + "'and Chapter Like '" + btnlanguage.Text.ToString() + "%' and QuestionType Like '" + q_type + "' and Marks = '" + marks + "'");
                DGV_View.DataSource = main_ds.Tables[0].DefaultView;
            }
            else if (txt_marks.Text != "" && cmbQType.SelectedIndex != -1 && txt_sub.Text != "" && txt_std.Text != "")
            {
                DataTable dt = main_ds.Tables[0];
                dt.DefaultView.RowFilter = String.Format("QuestionType Like '" + q_type + "' and  Marks = '" + marks + "' and Subject  Like '" + txt_sub.Text.ToString() + "%' and Standard Like '"+ txt_std.Text.ToString()+ "'");
                DGV_View.DataSource = main_ds.Tables[0].DefaultView;
            }

            else if (txt_marks.Text != "" && cmbQType.SelectedIndex != -1 && txt_sub.Text != "")
            {
                DataTable dt = main_ds.Tables[0];
                dt.DefaultView.RowFilter = String.Format("QuestionType Like '" + q_type + "' and  Marks = '" + marks + "' and Subject  Like '" + txt_sub.Text.ToString() + "%'");
                DGV_View.DataSource = main_ds.Tables[0].DefaultView;
            }
            else if (txt_marks.Text != "" && cmbQType.SelectedIndex != -1)
            {
                DataTable dt = main_ds.Tables[0];
                dt.DefaultView.RowFilter = String.Format(" QuestionType Like '" + q_type + "'  and Marks = '" + marks + "'");
                DGV_View.DataSource = main_ds.Tables[0].DefaultView;
            }
            else if (txt_marks.Text != "")
            {
                DataTable dt = main_ds.Tables[0];
                dt.DefaultView.RowFilter = String.Format("Marks = '"+ marks +"'");
                DGV_View.DataSource = main_ds.Tables[0].DefaultView;
            }
            else
            {
                load_grid_view();
            }

同样,对每个给定的控制进行了编码。

谢谢。

2 个答案:

答案 0 :(得分:1)

也许你可以将字段查询存储在每个控件的Tag属性中(例如txt_marks.Tag将被设置为"Marks ='{0}'")然后你可以定义一个extensiom方法来获取查询一个TextBox和另一个用于下拉,例如:

internal static string GetQuery(this TextBox textBox)
{
    if(string.IsNullOrEmpty(textBox.Text)) return string.Empty;
    return string.Format(textBox.Tag.ToString(), textBox.Text)
}

internal static string GetQuery(this ComboBox cmbBox)
{
    if(cmbBox.SelectedIndex == -1) return string.Empty;
    return string.Format(cmbBox.Tag.ToString(), cmbBox.SelectedValue)
}

然后,您可以循环浏览控件,调用GetQuery并执行string.Join("and ", controlQueries.Where(q => !string.IsNullOrEmpty(q))

答案 1 :(得分:1)

谢谢@KMoussa和@Sid。 使用Combine Both建议我在函数中进行了动态查询,并在每个控件上调用此函数,并且还想与此站点共享。

我的功能

public void searching_query()
        {
            string grid_query = "";
            int cnt_coma = 0;
            string q_type = "";
            if (txt_marks.Text != "")
            {
                string marks = Convert.ToString(txt_marks.Text);
            }
            if (cmbQType.SelectedIndex != -1)
            {
                q_type = Convert.ToString(cmbQType.SelectedValue);
                // Removing the wild character in question type . 
                if (q_type.Contains("[") || q_type.Contains("]") || q_type.Contains("*") || q_type.Contains("%"))
                {
                    q_type = replacestring(q_type);
                }
            }
            // counting the number of fields has been enter ->(for entering "and" in between in query)
            {
                if (txt_std.Text != "")
                    cnt_coma = 1;
                if (txt_sub.Text != "")
                    cnt_coma = 2;
                if (Txt_chp.Text != "")
                    cnt_coma = 3;
                if (cmbQType.SelectedIndex != -1)
                    cnt_coma = 4;
                if (txt_marks.Text != "")
                    cnt_coma = 5; 
            }
            // making query for searching . 

            if (txt_std.Text != "")
            {
                if (cnt_coma > 1)
                    grid_query = grid_query + "Standard Like  '" + txt_std.Text.ToString() + "' and ";
                else if (cnt_coma <= 1)
                    grid_query = grid_query + "Standard Like '" + txt_std.Text.ToString() + "'";
            }
            if (txt_sub.Text != "")
            {
                if (cnt_coma > 2)
                    grid_query = grid_query + "Subject Like  '" + txt_sub.Text.ToString() + "%' and ";
                else if (cnt_coma <= 2 )
                    grid_query = grid_query + "Subject Like  '" + txt_sub.Text.ToString() + "%' ";
            }
            if (Txt_chp.Text != "")
            {
                if (cnt_coma > 3)
                    grid_query = grid_query + "Chapter Like  '" + Txt_chp.Text.ToString() + "%' and ";
                else if (cnt_coma <= 3 )
                    grid_query = grid_query + "Chapter Like  '" + Txt_chp.Text.ToString() + "%'";
            }
            if (cmbQType.SelectedIndex != -1)
            {
                if (cnt_coma > 4)
                    grid_query = grid_query + "QuestionType Like '" + q_type + "' and ";
                else if (cnt_coma <= 4 )
                    grid_query = grid_query + "QuestionType Like '" + q_type + "'";
            }
            if (txt_marks.Text != "")
            {
                grid_query = grid_query + "Marks = '" + Convert.ToString(txt_marks.Text) + "'";
            }

            //---------- Grid view Filteration 
            if (cnt_coma > 0)
            {             
                DataTable dt = main_ds.Tables[0];
                dt.DefaultView.RowFilter = String.Format(grid_query);
                DGV_View.DataSource = main_ds.Tables[0].DefaultView;
            }
            else
            {
                load_grid_view();
            }

        }
相关问题