我有一個datagridview和一個文本框。當用戶在文本框中輸入一些字母時,我想要對datagridview進行排序並將結果顯示爲文本框的文本。但是,如果用戶不在文本框中鍵入任何內容或刪除文本框中的內容,我希望datagridview顯示我的數據庫的所有結果。實際上,我在2周前取得了成功,但我做了一些改變,現在它不再工作了。刷新Datagridview,插入文本框時的詳細信息行
這裏是我的代碼:
private void textBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
maConnexion.Open();
string Var1 = textBox1.Text;
SqlCommand command = maConnexion.CreateCommand();
SqlCommand command1 = maConnexion.CreateCommand();
if (Program.UserType == "admin")
{
if (textBox1.Text != String.Empty)
{
command.Parameters.AddWithValue("@SerialNum", Var1 + "%");
command1.Parameters.AddWithValue("@SerialNum", Var1 + "%");
command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE SerialNum LIKE @SerialNum AND FComponent IS NOT NULL";
command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE SerialNum LIKE @SerialNum And FComponent IS NOT NULL";
}
}
else
{
if (textBox1.Text != String.Empty)
{
command.Parameters.AddWithValue("@SerialNum", Var1 + "%");
command1.Parameters.AddWithValue("@SerialNum", Var1 + "%");
command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE (SerialNum LIKE @SerialNum) AND ReportingOperator IS NULL AND FComponent IS NOT NULL ";
command1.CommandText = "SELECT DISTINCT SerialNum, Machine, BoardName, BoardNumber FROM FailAndPass WHERE ReportingOperator IS NULL AND FComponent IS NOT NULL AND (SerialNum LIKE @SerialNum) ";
}
}
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
SqlDataAdapter sda = new SqlDataAdapter(command);
SqlDataAdapter sda1 = new SqlDataAdapter(command1);
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
sda.Fill(dt);
sda1.Fill(dt1);
DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool));
DataColumn dcIsDirty1 = new DataColumn("IsDirty", typeof(bool));
dcIsDirty.DefaultValue = false;
dcIsDirty1.DefaultValue = false;
dt.Columns.Add(dcIsDirty);
dt1.Columns.Add(dcIsDirty1);
dataGridView1.DataSource = dt;
dataGridView2.DataSource = dt1;
maConnexion.Close();
dataGridView1.Columns[6].Visible = false;
dataGridView2.Columns[3].Visible = false;
}
}
正如你所看到的,在DataGridView是填寫,用戶可以通過點擊按鈕,在數據庫編輯欄等寫入新的數據。但是,如果用戶在文本框中寫入serialNum,它只顯示一個結果..而不是所有以B0033開頭的結果。
請發佈[如何創建一個最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve)。我認爲你的問題有很多不必要的代碼。 – Luke
編輯並更改。 –
請接受我的編輯 - 我也刪除了註釋的代碼。你究竟發生了什麼變化,導致它停止工作? – Luke