User Tools

Site Tools


Action disabled: source
prog:csharp:250513-01:index

C#: WinForm 清除DataGridView的預設選取值 (2025-05-13)

Local Backup

  • 在Windows Form使用DataGridView時,載入資料後,DataGridView會自動選取第一筆資料
  • 查了MSDN文件,發現有ClearSelection這個方法可以使用。
  • 其實這個方法是把DataGridView每一欄和列的Selected屬性設為False。
    • When this method is called, each column and row’s Selected property is set to false.
  • 但以下的程式並沒有作用,
    DataTable dt = ......   //取得資料來源
    testDGV.DataSource = dt;
     
    //清除預設的選取
    testDGV.ClearSelection();
  • 後來推測,有可能是因為ClearSelection執行順序的影響,把它放到DataGridView的DataBindingComplete事件中,就達到我想要的成果了。
  • private void testDGV_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
         testDGV.ClearSelection();
    }
  • 40 person(s) visited this page until now.

prog/csharp/250513-01/index.txt · Last modified: 2025/05/13 14:41 by jethro