[英]Remove blue colored row from DataGridView WinForms
當我逐行填充DataGridView時(稱其為add函數),第一行變為藍色。 由於我也嘗試過ClearSelection(),所以未選擇它,但是它沒有用。 誤以為第一行被選中。
如何擺脫它?
void FillDGV()
{
dataGridViewParties.Rows.Clear();
for (int i = 0; i < dataGridViewParties.Columns.Count; i++)
{
dataGridViewParties.Columns[i].Width = this.Width / dataGridViewParties.Columns.Count;
}
DataTable partyTbl = UtilityClass.GetDataTable(@"SELECT [PartyID]
,[PartyName]
,[PartyAddress]
,[PartyState]
,[PartyCity]
,[PartyPhone]
FROM [VegiManager].[dbo].[Parties]
WHERE [PartyName] LIKE '" + textBoxPartySearch.Text + "%' ");
foreach (DataRow dr in partyTbl.Rows)
{
dataGridViewParties.Rows.Add(1);
dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[0].Value = dr["PartyID"].ToString();
dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[1].Value = dr["PartyName"].ToString();
dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[2].Value = dr["PartyAddress"].ToString();
dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[3].Value = dr["PartyState"].ToString();
dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[4].Value = dr["PartyCity"].ToString();
dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[5].Value = dr["PartyPhone"].ToString();
}
if (dataGridViewParties.Rows.Count > 0)
{
dataGridViewParties.ClearSelection();
dataGridViewParties.CurrentCell = null;
}
}
在調試器中,我發現在DataGridViewParties.CurrentCell = null;之前CurrentCell已經為null; 執行。
這個問題http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c440c4f6-6dfc-47b6-97c0-1ce49c105b64/也與此相關,但未提供解決方案。
編輯:它很奇怪,但它適用於Load事件,我正在構造函數中。 我希望在選擇第一行並在用戶按下UP箭頭鍵時將焦點移到某個文本框。 但是在這種情況下,它不起作用(第一行顯示為藍色)
private void dataGridViewInwards_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && dataGridViewParties.SelectedRows.Count > 0 && dataGridViewParties.SelectedRows[0].Index == 0)
{
textBoxPartySearch.Focus();
dataGridViewParties.ClearSelection();
dataGridViewParties.CurrentCell = null;
}
else if (e.KeyCode == Keys.Up && dataGridViewParties.SelectedRows.Count == 0)
{
textBoxPartySearch.Focus();
}
}
我嘗試了上述方法,但沒有任何幫助。
最后,我只是決定將選定單元格的默認顏色設置為與未選定單元格相同。
然后,在單元格單擊方法中,將它們重新設置。 這樣,在單擊之前,什么都不會顯示為選中狀態,這對我的應用程序來說已經足夠了。 這是我在click方法中用來將所有設置恢復為正常的代碼:
dataGridView.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
dataGridView.DefaultCellStyle.SelectionForeColor = SystemColors.Window;
令人討厭的是,如果將ClearSelection方法放在一個按鈕中,它實際上可以很好地工作,但是如果我創建了一個包含數據網格的控件,將數據加載到其中,然后嘗試立即將其清除,那將是行不通的。 我希望我知道為什么...
要與ClearSelection
一起實現此ClearSelection
您將需要再設置一個屬性
在DataBindingComplete
嘗試一下
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
編輯
根據您的注釋,您可以將代碼修改為
if (e.KeyCode == Keys.Up && dataGridView1.CurrentCell.RowIndex == 0)
{
this.ActiveControl = textBoxPartySearch;
dataGridView1.Refresh();
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
e.Handled = true;
}
嘗試將ClearSelection()添加到VisibleChanged事件,如下所示:
private void datagridview1_VisibleChanged(object sender, EventArgs e)
{
datagridview1.ClearSelection();
}
在Form.Shown
事件中,只需執行以下操作:
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
經過三個小時的故障排除后,我發現了: 在調用dataGridView.ClearSelection()
之前,您的DataGridView
需要顯示在屏幕上。
我怎么知道這有效:
我有兩個DataGridViews
,每個都在不同的面板中。 一次只能看到一個面板。 在最初可見的面板中, DataGridView
從未忽略過我對ClearSelection()
調用,因此我從未見過立即選擇行。
我曾經在顯示另一個包含DataGridView
的面板之前填充另一個DataGridView
並清除了它的選擇,但是第一行始終始終突出顯示。 在更改DataGridView
的選擇之前,我更改了代碼以顯示另一個面板,對ClearSelection()
的調用按ClearSelection()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.