2013-04-08 52 views
14

在我的WinForms中我有DataGridView。我想一次選擇全行,所以我設置SelectionModeFullRowSelect。現在我遇到了問題,因爲在開始時我的表格強調了第一行(選定的行是空的,第一行沒有被選中,只是下劃線)。我嘗試了很多東西,比如:DataGridView在開始時沒有選擇行

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

而且都失敗了,因爲其實沒有選擇。

我該如何擺脫這個下劃線?

感謝您的幫助!

+1

如果手動添加的行,清除選擇將所有的行之後。 – Akanksha 2013-04-08 13:15:11

回答

13

不幸的是,這些答案都沒有幫助我,但我找到了其他解決方案。相反,無法選擇的,我將只是這段代碼隱藏:

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

所以如果有人只是想隱藏選擇它會工作得很好。

乾杯:)

+3

雖然不是真正的解決方案... – miguelmpn 2016-04-01 16:40:24

12

這個工作對我來說:

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

不幸的是它不起作用。正如我所說的第一行沒有被選中,它只是下劃線。 – 2013-04-08 13:48:35

+0

你有什麼窗戶? – 2013-04-08 13:50:46

+0

這可能是由於該行有焦點。您可以嘗試使用'focus()'方法將焦點放在不同的控件上。 – 2013-04-08 13:55:15

1

嘗試InitializeComponent()後在構造函數中設置DataGridView.AllowUserToAddRows = false

+0

它的工作原理:)我必須考慮的唯一事情就是在Shown事件中編寫這行代碼。當我在構造函數中寫這個時,什麼都沒有發生。 – FrenkyB 2015-11-09 04:04:19

0

這項工作對我來說是明確的選擇在數據綁定

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

End Sub 
+1

它可能是什麼工作......但這裏的問題與Winforms'Datagridview',而不是ASP.NET'GridView'有關,所以答案是不合適的。 – Chris 2013-09-07 11:11:25

11

只要把dataGridView1.ClearSelection();在窗體的加載事件。

+1

它適合我。謝謝。 – 2016-09-12 07:44:31

0

你可以叫dataGridView.ClearSelection()Form_Load事件像這裏面的方法...

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

,如果你要選擇行中你應該嘗試把中顯示事件datagridView.ClearCelection(),也datagridView.CurrentCell=null和例如刪除或修改信息只是做if(datagridView.CurrentCell==null){ MessageBox.Show("You must select row");}它爲我工作

0

在開始爲殘疾選擇行設置的事件是這樣的, 和管理FLAG停止ClearSelection

private void dataGridView_SelectionChanged(object sender, EventArgs e) 
{ 

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

嘗試,這會很有幫助

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

有時,當你重裝你的窗體不關閉程序,第一行會可以高亮顯示。但它不會被選中,您將爲選定的行索引獲取-1。

你可以做到這一點就像這樣:

'1. store default styles when form is loading: 
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: 
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: 
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: 
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 

希望這有助於ü傢伙。

0

如果是這樣,因爲它提出了在初始加載不需要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 
    } 
}