vb.net 如何防止datagridview在打开时选择第一个单元格作为当前单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17978887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:29:08  来源:igfitidea点击:

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

vb.netdatagridview

提问by stackexchange12

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

我试图找到一种方法来防止我的 datagridview 选择第一个单元格作为默认单元格。现在,如果导入时单元格中有负数,我有代码可以将 datagridview 中单元格的背景颜色变为红色。但是,这在我的第一个单元格中无法正常工作,因为它在导入时默认已突出显示。如果有人能找到如何关闭单元格的选择,我将不胜感激!:)

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

我知道它一定很简单,比如 DataGridView1.CurrentCell.Selected = False

回答by field_b

Handle the DataBindingComplete event of the DataGridView as so:

像这样处理 DataGridView 的 DataBindingComplete 事件:

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

回答by shaik ibrahim

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

我有一个创建焦点的代码,但我忘记了。要设置您需要更改方向的网格选择

回答by digad

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

然后使用如下: Grdvw_Cell_Unselect(your_datagridview) ...

回答by Mantas Anni

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)

来源

回答by Parge

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