Peter
Peter

Reputation: 942

DataGridView without selected row at the beginning

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!

Upvotes: 21

Views: 70260

Answers (15)

oliva nikita
oliva nikita

Reputation: 76

I know I am very late but I am almost with a very suitable solution to your question, so all you have to do is add a "timer" set it to ENABLE in the TICK method disable the timer and then put the DGV.ClearSelection(); keep the timer interval to 100

e.g

  private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Enabled= false;
        DGV.ClearSelection();
    }

tada Problem solved and the selection still works fine later.

Upvotes: 0

Radoš R
Radoš R

Reputation: 1

public void Reset()

{

dataGridView.ClearSelection();

}

Upvotes: 0

Victor Olivo
Victor Olivo

Reputation: 1

Had the same situation, "dataGridView.Shown" event was the only solution that worked like magic for me:

// Clears the default selection of both Data Grid Views
private void dataGridView_Shown(object sender, EventArgs e)
{
    dataGridView.ClearSelection();
}

Upvotes: 0

Ahmad Shbita
Ahmad Shbita

Reputation: 73

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

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

Upvotes: 0

Soly
Soly

Reputation: 1

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.

Upvotes: 0

HaiNguyen
HaiNguyen

Reputation: 312

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
    }
}

Upvotes: 0

Nitin Patil
Nitin Patil

Reputation: 120

Try This may be helpful

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

Upvotes: 2

daniele3004
daniele3004

Reputation: 13880

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;
    }
}

Upvotes: 0

jhony3
jhony3

Reputation: 272

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

Upvotes: 2

user4860969
user4860969

Reputation:

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;
    }

Upvotes: 1

Peter
Peter

Reputation: 942

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

Upvotes: 17

ramires.cabral
ramires.cabral

Reputation: 940

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

Upvotes: 20

Ajayadas
Ajayadas

Reputation: 1

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

Upvotes: 0

Jacob Seleznev
Jacob Seleznev

Reputation: 8131

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

Upvotes: 1

Nicolas Tyler
Nicolas Tyler

Reputation: 10552

This works for me:

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

Upvotes: 17

Related Questions