C# DataGridView 开头没有选中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15879461/
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-08-10 18:16:15  来源:igfitidea点击:

DataGridView without selected row at the beginning

c#.netwinformsdatagridview

提问by Peter

In my WinForms I have DataGridView. I wanted to select full row at once so I set SelectionModeas 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:

在我的 WinForms 中,我有DataGridView. 我想一次选择整行,所以我设置SelectionModeFullRowSelect. 现在我有问题,因为一开始我的表单在第一行下划线(选定的行集是空的,第一行没有被选中,只是有下划线)。我尝试了很多东西,例如:

    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!

谢谢你的帮助!

采纳答案by Peter

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

干杯:)

回答by Nicolas Tyler

This works for me:

这对我有用:

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

回答by Jacob Seleznev

Try setting DataGridView.AllowUserToAddRows = falsein constructor after InitializeComponent().

之后尝试DataGridView.AllowUserToAddRows = false在构造函数中设置 InitializeComponent()

回答by Ajayadas

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

回答by ramires.cabral

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

只需放入dataGridView1.ClearSelection();表单的加载事件即可。

回答by ramires.cabral

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

您可以像这样在 form_Load 事件中调用 dataGridView.ClearSelection() 方法...

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

回答by jhony3

You should try to put in Shown event datagridView.ClearCelection()and also datagridView.CurrentCell=nulland 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

您应该尝试放入 Shown 事件datagridView.ClearCelection()datagridView.CurrentCell=null例如,如果您想选择用于删除或更改信息的行,if(datagridView.CurrentCell==null){ MessageBox.Show("You must select row");}这对我有用

回答by daniele3004

The event to set for disabled selected row at start is this, and manage a FLAG to stop the ClearSelection

在开始时为禁用的选定行设置的事件是这个,并管理一个 FLAG 以停止 ClearSelection

private void dataGridView_SelectionChanged(object sender, EventArgs e)
{

    if (FLAG==true)
    {
       dataGridView.ClearSelection();
       FLAG=false;
    }
}

回答by Nitin Patil

Try This may be helpful

试试这可能有帮助

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

回答by Soly

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.

有时,当您在不关闭程序的情况下重新加载表单时,第一行将被突出显示。但它不会被选中,并且您将获得 -1 所选行索引。

You can do it just like this:

你可以这样做:

 1. Store default styles when the form is loading:

 1. 表单加载时存储默认样式:

 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:

 2. 与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:

 3. 更改单元格样式时将单元格样式更改为默认值,例如 cell_click 或 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:

 4.当你想关闭表单时,全部恢复为默认值:

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.

希望这对你们有帮助。