stackexchange12
stackexchange12

Reputation: 642

How to prevent datagridview from selecting the first cell as currentcell on opening

I'm trying to find a way to prevent my datagridview from selecting the first cell as default cell. Right now I have code that turns the backcolor of the cells in my datagridview to red if negative numbers are in the cells on import. However this won't work properly in my first cell since its already highlighted by default on import. If anyone can find out how to turn the selecting of the cell off I would greatly appreciate it! :)

I know it must be something simple like DataGridView1.CurrentCell.Selected = False

Upvotes: 2

Views: 17900

Answers (6)

Kinjal
Kinjal

Reputation: 1

Bind event for Paint for datagridview.

Click datagridview > Properties > Paint > double click on space near Paint > It will create method similar to this: datagridview1_Paint(object sender, PaintEventArgs e)

Write these two lines in that method as displayed:enter code here

private void datagridview1_Paint(object sender, PaintEventArgs e) {
    this.datagridview1.ClearSelection();
    this.datagridview1.CurrentCell = null;
}

Upvotes: 0

Parge
Parge

Reputation: 1

What worked for me was to clear the selection and then set the row based on the row index. Hope this helps.

   GridView.ClearSelection()
   GridView.Rows(RowIndex).Selected = True
   GridView.DataSource = DataTable
   GridView.Refresh()

Upvotes: 0

Mantas Anni
Mantas Anni

Reputation: 13

Try this, works for me. Put this code anywhere within your form code with the datagrid in it.

Private Sub YourDataGridName_DataBindingComplete(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) _
    Handles YourDataGridName.DataBindingComplete

    Dim DGV As DataGridView
    DGV = CType(sender, DataGridView)
    DGV.ClearSelection()
End Sub

(source)

Upvotes: 1

field_b
field_b

Reputation: 698

Handle the DataBindingComplete event of the DataGridView as so:

private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    myGrid.ClearSelection();
}

Upvotes: 9

digad
digad

Reputation: 1

This one works (and with differents selectionmode gridviews)

Public Function Grdvw_Cell_Unselect(ByVal Grdvw As DataGridView) As Boolean
    ' cancel all defaut selection in a datagridview
    Grdvw_Cell_Unselect = False
    Try
        Grdvw.ClearSelection()
        Grdvw.Item(0, 0).Selected = False
        Grdvw_Cell_Unselect = True
    Catch ex As Exception
    End Try
End Function

then use as this: Grdvw_Cell_Unselect(your_datagridview) ...

Upvotes: 0

shaik ibrahim
shaik ibrahim

Reputation: 187

try

 datagridview.currentrow.selected = true

this would make the whole row selected so the code that change the curent back color wont be effected.

I had a code for creating focus but i forget it. to set the selection of the grid you need to cahnge the direction

Upvotes: 0

Related Questions