lundi 5 décembre 2016

What to do to minimize the condition of if else on controls

enter image description here

I have grid view in which all question has been display from access database . now I want to perform the search operation in given text boxes ,user may enter the data in 1 text box or else in all text boxes ,depends upon user needs .My question is how many times condition has been give so that whatever user give information in any text box ,filtration is performed accordingly.

for eg : user gave only standard and marks than filtration must be perform where standard = "given value" and marks = "given value" only

I have given various condition on each control but its become too huge coding. now wants to minimize this so any suggestion must recommended.

My Code:

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

Similarly above coding has been done to every given control.

Thank you .

Aucun commentaire:

Enregistrer un commentaire