2016-12-05 1 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 조합이 다른 조건을 제공하기 때문에 사용자가 표준 값을 표시 할 수 있지만 subject가 아닌 다른 값을 부여 할 수 있다고 가정합니다. 가능한 모든 조건, 5 가지 요인 조건을 작성했지만 모두 최소화하려고합니다. – Mamta

답변

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

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

아마 당신은 각각의 Tag 속성에 필드 쿼리를 저장할 수있는 오타

1

이 있다면 나에게 내가 그래서 오타 검사를하지 않아도 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))

관련 문제