我有一個C#中的datagridview,當用戶單擊「搜索」時,它將根據搜索條件與客戶進行填充。我遇到的問題是,當用戶多次點擊搜索按鈕搜索客戶時,或者用戶想要執行其他搜索時,上次搜索中的所有詳細信息仍然位於datagridview中。現在的主要問題不是列沒有被刪除或額外添加,但datagridview中的按鈕的列索引在搜索多次後變爲「0」。清空DataGridView
現在我已經知道了這個問題,我想我會在這裏保留所有的細節,以防將來有人想查看細節。問題是,看來,當我刪除custDataGridView.DataSource = null;部分,一切似乎都工作
至於似乎有一些不確定性,這裏是代碼全:
DataGridViewButtonColumn custAddJobSelect = new DataGridViewButtonColumn();
Datatable dt = new Datatable();
// ---> This was the culprit ---> custDataGridView.DataSource = null;
dt.Rows.Clear();
#region SQL connection String
...//CustQuery is the SQL stuff, conn is connection to DB.
#endregion
da = new System.Data.SqlClient.SqlDataAdapter(CustQuery, conn);
da.Fill(dt);
conn.Close();
custDataGridView.DataSource = dt;
if (AddJobGone == false)
{
AddJobGone = true;
custAddJobSelect.DisplayIndex = 0;
custDataGridView.Columns.Add(custAddJobSelect);
}
private void custDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.custDataGridView.Columns.Count - 1)
{
string addJobsCustNo = custDataGridView.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
txtAddJobsCustNo.Text = addJobsCustNo;
pnlAddJobSearchCustomer.Visible = false;
}
}
感謝您的耐心,希望這可以幫助別人!