2
votes

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

6
what does the HTML code look like? the ASP.NET code?Malachi
Have you tried anything? Like looking through the datagridview's methods?davidsbro
What don't you select currentcell value after assigning the datasource .. ?matzone

6 Answers

7
votes

Handle the DataBindingComplete event of the DataGridView as so:

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

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

0
votes

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

0
votes

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)

0
votes

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()
0
votes

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