2016-12-05 24 views
2

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

同樣上述編碼已經完成,每一個給定的控制。

謝謝。

+0

耶穌基督那肯定是一些代碼。在一個側面說明,如果你這樣做'string marks = Convert.ToString(txt_marks.Text);',爲什麼你到處都在使用'txt_marks'? –

+0

由於txt_Marks與不同控件的組合給出了不同的條件,假設用戶可能會給出標準值標誌值而不是主題,否則用戶可能會給出主題值而不是標準值,我寫了所有可能的條件,5個因子條件但是想要最小化所有條件。 – Mamta

回答

1

謝謝@KMoussa和@Sid。 隨着你的組合這兩個建議我做了一個動態查詢的功能,並在每個控件上調用此函數,也喜歡與此網站共享。

我的功能

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

怎麼樣使用一些Functionn參數做一些檢查?

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

請注意,我已經寫notepad++,所以我沒有錯字支票,所以原諒我,如果有任何錯字

1

或許你可以現場查詢存儲在每個Tag財產

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:控制(例如txt_marks.Tag將被設置爲"Marks ='{0}'"),那麼你可以定義一個extensiom方法從TextBox,另一個下拉,像得到查詢ou可以通過控件循環,請致電GetQuery並執行string.Join("and ", controlQueries.Where(q => !string.IsNullOrEmpty(q))

相關問題