[英]DataGridView Default Row/Cell select issue
DataGridView設置為“全行選擇”。 需要一種方法來清除由DataGridView的默認性質完成的默認選定行
我也在使用SelectionChanged事件dgv有4列
如果我保留默認的行選擇,則SelectionChanged在加載時會觸發4次,我根本不希望其觸發。
我已經嘗試使用RowsPostPaint事件來清除選擇,並且不會觸發SelectionChanged事件,但是之后無法選擇任何行。
有任何想法嗎? 謝謝
用這個
在表單加載中:
datagridview.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(datagridview_DataBindingComplete);
對於聽眾:
private void datagridview_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
datagridview.ClearSelection();
}
但是請注意,無論何時更改數據源,默認選擇都會隱藏。
填充數據網格后,嘗試此操作。
datagrid.ClearSelection();
要取消選擇初始行選擇,可以枚舉SelectedRows
並將Selected
屬性設置為false
。 為防止在SelectionChanged
初始行時觸發SelectionChanged
事件,請在取消選擇事件后手動添加事件。
例如,在表單的Load
事件中:
foreach(DataGridViewRow row in dataGridView1.SelectedRows)
row.Selected = false;
dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.