20
votes

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, because at the beginning my form underlines first row (set of selected rows is empty, the first row is not selected but just underlined). I have tried many things, such as:

    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.ClearSelection();
    }

And all failed, because in fact there is no selection.

How can I get rid of this underline?

Thanks for any help!

12
If you are manually adding the rows, Clear selection after adding all the rows.Akanksha Gaur

12 Answers

19
votes

Just put dataGridView1.ClearSelection(); in load event of the form.

17
votes

This works for me:

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dataGridView1.Rows[0].Selected = false;
}
15
votes

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 :)

2
votes

You should try to put in Shown event datagridView.ClearCelection() and also datagridView.CurrentCell=null and for example if you want to select row for deleting or changing informations just do if(datagridView.CurrentCell==null){ MessageBox.Show("You must select row");} it works for me

2
votes

Try This may be helpful

private void dgv_order_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            dgv_order.CurrentCell.Selected = false;
            dgv_order.ClearSelection();
        }
1
votes

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

1
votes

You can call dataGridView.ClearSelection() method inside form_Load event like this...

    private void Form1_Load(object sender, EventArgs e)
    {
     // You will get selectedCells count 1 here
     DataGridViewSelectedCellCollection selectedCells = dataGridView.SelectedCells;
     // Call clearSelection 
     dataGridView.ClearSelection();
     // Now You will get selectedCells count 0 here
     selectedCells = dataGridViewSchedule.SelectedCells;
    }
0
votes

This work for me for clear selection on databind

Protected Sub GridCancel_DataBinding(sender As Object, e As EventArgs) Handles GridCancel.DataBinding
    GridCancel.SelectedIndex = -1

End Sub
0
votes

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
votes

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
votes

Sometimes, when you reload your form without closing your program, the first row will be highlighted. But it will not be selected, and you will get -1 for selected row index.

You can do it just like this:

 1. Store default styles when the form is loading:

 Public Class aRoots
    Dim df1, df2, df3, df4 As Color
    Private Sub aRoots_Load(sender As Object, e As EventArgs) Handles Me.Load
            df1 = DGV_Root.DefaultCellStyle.SelectionBackColor
            df2 = DGV_Root.DefaultCellStyle.BackColor
            df3 = DGV_Root.DefaultCellStyle.SelectionForeColor
            df4 = DGV_Root.DefaultCellStyle.ForeColor

 2. Change cell styles when interacting with datagridview:

Private Sub LoadRoot()
       For i = 0 To 5
                DGV_Root.Rows.Add()
                For j = 0 To 3
                    DGV_Root.Item(j, i).Value = ...
                Next
            Next
        'DGV_Root.ClearSelection() ==> instead of this use 2 lines below
        DGV_Root.DefaultCellStyle.SelectionBackColor = df2
        DGV_Root.DefaultCellStyle.SelectionForeColor = df4
    End Sub

 3. Change cell styles to default when selection is being changed like cell_click or cell_double click:

Private Sub DGV_Root_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DGV_Root.CellMouseClick
        DGV_Root.DefaultCellStyle.SelectionBackColor = df1
        DGV_Root.DefaultCellStyle.SelectionForeColor = df3


...
End Sub

 4. restore all to default when u want to close form:

Private Sub PbClose_Click(sender As Object, e As EventArgs) Handles PbClose.Click
        BtnCancel.PerformClick()
        DGV_Root.DefaultCellStyle.SelectionBackColor = df1
        DGV_Root.DefaultCellStyle.BackColor = df2
        DGV_Root.DefaultCellStyle.SelectionForeColor = df3
        DGV_Root.DefaultCellStyle.ForeColor = df4
        Me.Close()
End Sub

Hope this help you guys.

0
votes

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

private void frmMain_Shown(object sender, EventArgs e)
        {
            dataGridView1.ClearSelection();
        }