DataGridView without selected row at the beginning

后端 未结 12 1030
栀梦
栀梦 2021-02-12 14:23

In my WinForms I have DataGridView. I wanted to select full row at once so I set SelectionMode as FullRowSelect. And now I have problem, b

相关标签:
12条回答
  • 2021-02-12 14:32

    Try setting DataGridView.AllowUserToAddRows = false in constructor after InitializeComponent().

    0 讨论(0)
  • 2021-02-12 14:33

    "Shown" event that run after frame first displayed worked for me:

    private void frmMain_Shown(object sender, EventArgs e)
            {
                dataGridView1.ClearSelection();
            }
    
    0 讨论(0)
  • 2021-02-12 14:36

    Unfortunately none of these answers helped me, but I found other solution. Instead of unable selection I will just hide it with this piece of code:

    dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
    dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;
    

    So if anyone just wants to hide the selection it's gonna work pretty well.

    Cheers :)

    0 讨论(0)
  • 2021-02-12 14:41

    The event to set for disabled selected row at start is this, and manage a FLAG to stop the ClearSelection

    private void dataGridView_SelectionChanged(object sender, EventArgs e)
    {
    
        if (FLAG==true)
        {
           dataGridView.ClearSelection();
           FLAG=false;
        }
    }
    
    0 讨论(0)
  • 2021-02-12 14:44

    If this is because it raised unwanted GridView1_SelectionChanged event on initial loading, you can use a flag to handle this

    public partial class YourFormName
    { 
        private bool IsReady= false;
    
        private void YourFormName_Load(object sender, EventArgs e)
        { 
               //Load your GridView1...
               //Format your GridView1...
                IsReady = true;
        }
        void GridView1_SelectionChanged(object sender, EventArgs e)
        {
             if (!IsReady) 
                 return;
             //do the rest of the stuffs
        }
    }
    
    0 讨论(0)
  • 2021-02-12 14:46

    This works for me:

    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.Rows[0].Selected = false;
    }
    
    0 讨论(0)
提交回复
热议问题