我有一个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;
}
}
感谢您的耐心,希望这可以帮助别人!