我有網格視圖,其中所有問題都從訪問數據庫中顯示。現在我想在給定的文本框中執行搜索操作,用戶可以在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();
}
同樣上述編碼已經完成,每一個給定的控制。
謝謝。
耶穌基督那肯定是一些代碼。在一個側面說明,如果你這樣做'string marks = Convert.ToString(txt_marks.Text);',爲什麼你到處都在使用'txt_marks'? –
由於txt_Marks與不同控件的組合給出了不同的條件,假設用戶可能會給出標準值標誌值而不是主題,否則用戶可能會給出主題值而不是標準值,我寫了所有可能的條件,5個因子條件但是想要最小化所有條件。 – Mamta