2016-12-05 4 views
2

enter image description hereコントロール

私はすべての質問は、Accessデータベースから表示されたグリッドビューを持っているのであれば、他の条件を最小限にするために何をすべきか。今は、特定のテキストボックスで検索操作を実行したい、ユーザーが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(); 
      } 

同様にコーディング上記は、あらゆる与えられた制御に行われています。

ありがとうございます。

+0

イエス・キリスト確かにいくつかのコードです。この 'string marks = Convert.ToString(txt_marks.Text);'を実行すると、なぜあなたはまだどこでも 'txt_marks'を使用していますか? –

+0

異なるコントロールとのtxt_Marksの組み合わせは異なる条件を与えているので、ユーザーが標準値の値をvalueに与えてもsubjectは与えないかもしれないと仮定します。 – Mamta

答えて

1

ありがとうございます@KMoussaと@Sid。 Combine Bothの提案では、関数内で動的クエリを作成し、この関数をすべてのコントロールで呼び出し、このサイトと共有したいと思います。

My機能

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

     } 
2

Functionを使用して、n引数を使用していくつかのチェックを行うのはどうですか?

private void txt_marks_TextChanged(object sender, EventArgs e) 
{ 
    string marks = Convert.ToString(txt_marks.Text); 
    string q_type = Convert.ToString(cmbQType.SelectedValue); 
    char[] q_types = { '[', ']', '%'}; 

    if (ContainsChars(q_types, q_type)) 
    { 
     q_type = replacestring(q_type); 
    } 
    if (NoEmpty(btnlanguage.Text, txt_sub.Text, txt_std.Text, txt_marks.Text) && cmbQType.SelectedIndex != -1) 
    { 
     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 (NoEmpty(txt_marks.Text, txt_sub.Text, txt_std.Text) && cmbQType.SelectedIndex != -1) 
    { 
     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 (NoEmpty(txt_marks.Text, txt_sub.Text) && cmbQType.SelectedIndex != -1) 
    { 
     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(); 
    } 
} 

public static bool NoEmpty(params string[] strings) 
{ 
    return strings.All(x => x != string.Empty); 
} 

public static bool ContainsChars(IEnumerable<char> chars, string toTest) 
{ 
    return chars.Any(x => toTest.Contains(x)); 
} 

任意のタイプミス

1

があるかどうおそらく、あなたはそれぞれのTagプロパティにフィールドのクエリを保存することができますので、私はタイプミスのチェックを持っているので、私を免除されるわけではない、私はnotepad++でそれを書いたことに注意してください

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

そして、Y:あなたはextensiomメソッドを定義することができます(例えばtxt_marks.Tag"Marks ='{0}'"に設定されます)制御がTextBoxと別のドロップダウンのために、何かのようにクエリを取得しますコントロールをループするだけでと呼び、string.Join("and ", controlQueries.Where(q => !string.IsNullOrEmpty(q))

関連する問題