DataGridView在开头没有选定的行

在我的WinForms中,我有DataGridView 。 我想一次选择完整行,所以我将SelectionMode设置为FullRowSelect 。 现在我有问题,因为在开始时我的表单为第一行加下划线(所选行的集合为空,第一行未选中但仅加下划线)。 我尝试了很多东西,比如:

  private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { dataGridView1.ClearSelection(); } 

一切都失败了,因为事实上没有选择。

我该怎样摆脱这个下划线?

谢谢你的帮助!

这对我有用:

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

不幸的是,这些答案都没有帮助我,但我找到了其他解决方案。 我将用这段代码隐藏它,而不是无法选择:

 dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor; dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor; 

因此,如果有人只想隐藏选择,它将会很好地工作。

干杯:)

只需输入dataGridView1.ClearSelection(); 在表单的加载事件中。

试试这可能会有所帮助

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

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

您应该尝试放入Shown事件datagridView.ClearCelection()以及datagridView.CurrentCell=null ,例如,如果要选择行来删除或更改信息,只需执行if(datagridView.CurrentCell==null){ MessageBox.Show("You must select row");}它对我有用

这对我来说可以清楚地选择数据绑定

 Protected Sub GridCancel_DataBinding(sender As Object, e As EventArgs) Handles GridCancel.DataBinding GridCancel.SelectedIndex = -1 End Sub 

你可以像这样调用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; } 

在start时为禁用的选定行设置的事件是this,并管理FLAG以停止ClearSelection

 private void dataGridView_SelectionChanged(object sender, EventArgs e) { if (FLAG==true) { dataGridView.ClearSelection(); FLAG=false; } } 

如果这是因为它在初始加载时引发了不需要的GridView1_SelectionChanged事件,您可以使用一个标志来处理这个

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

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

你可以这样做:

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.与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.在更改选择时将单元格样式更改为默认值,如cell_click或cell_double单击:

 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.当您想要关闭表单时,将全部恢复为默认值:

 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 

希望这能帮到你们。