Aks*_*y J 1 .net c# 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;
}
}
Run Code Online (Sandbox Code Playgroud)
在调试器中,我发现在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();
}
}
Run Code Online (Sandbox Code Playgroud)
为了实现这一点,ClearSelection
您将需要再设置一个属性
尝试这个在DataBindingComplete
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
Run Code Online (Sandbox Code Playgroud)
编辑
根据您的评论,您可以将代码修改为
if (e.KeyCode == Keys.Up && dataGridView1.CurrentCell.RowIndex == 0)
{
this.ActiveControl = textBoxPartySearch;
dataGridView1.Refresh();
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了上述方法,但没有任何帮助。
最后,我只是决定将选定单元格的默认颜色设置为与未选定单元格相同。
然后,在单元格单击方法中,将它们重新设置。这样,在单击之前,什么都不会显示为选中状态,这足以满足我的应用程序的需要。这是我在click方法中用来将所有设置恢复为正常的代码:
dataGridView.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
dataGridView.DefaultCellStyle.SelectionForeColor = SystemColors.Window;
Run Code Online (Sandbox Code Playgroud)
令人讨厌的是,如果将ClearSelection方法放在一个按钮中,它实际上可以很好地工作,但是如果我创建一个包含数据网格的控件,将数据加载到其中,然后尝试立即将其清除,则该方法不起作用。我希望我知道为什么...