[英]datagridview auto-select columns disable c#
我有datagridview顯示一些數據。
我用:
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
選擇整個列。
當我運行應用程序datagridview時,自動選擇第一列。 我正在嘗試禁用它。
如果我添加:
dataGridView1.ClearSelection();
什么都沒發生。
如果我添加:
dataGridView1.Columns[-1].Selected = true;
如果在Virtual Studio中進行構建,效果很好,但是當在不使用Virtual Studio的情況下進行構建時,會出現錯誤:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:index
如果單擊“繼續”,它可以工作,但錯誤令人討厭。
我嘗試過:
dataGridView1.SelectedColumns.Clear();
使用Virtual Studio進行構建時效果很好,但是在不使用Virtual Studio進行構建時使用時,出現錯誤消息:
Collection is read-only.
這個錯誤也很煩人。
我認為最好的解決方案是將collection設置為非只讀。 但是我不知道該怎么做。
任何簡單的解決方案都會很棒! 謝謝!
根據您的情況,您有幾種方法可以通過DataGridView
自動選擇:
在表單的Load
事件上:
dataGridView1.ClearSelection();
或在表單的構造函數上(或在初始化DataGridView
所有地方):
SynchronizationContext.Current.Post(
delegate
{
dataGridView1.ClearSelection();
}, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.