当用户单击DataGridView控件的空白(非行)部分时,我想删除该控件中所有选定的行。我该怎么做?
DataGridView
fdbelqdn1#
要删除DataGridView中的所有行和单元格,您可以使用ClearSelection method:
ClearSelection
myDataGridView.ClearSelection()
如果你不希望第一行/单元格显示为选中状态,你可以将CurrentCell property设置为Nothing/null,这将暂时隐藏焦点矩形,直到控件再次获得焦点:
CurrentCell
Nothing
null
myDataGridView.CurrentCell = Nothing
要确定用户何时单击了DataGridView的空白部分,您必须处理其MouseUp事件。在这种情况下,您可以HitTest单击位置,并观察它是否指示HitTestInfo.Nowhere。举例来说:
MouseUp
HitTest
HitTestInfo.Nowhere
Private Sub myDataGridView_MouseUp(ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) ''# See if the left mouse button was clicked If e.Button = MouseButtons.Left Then ''# Check the HitTest information for this click location If myDataGridView.HitTest(e.X, e.Y) = DataGridView.HitTestInfo.Nowhere Then myDataGridView.ClearSelection() myDataGridView.CurrentCell = Nothing End If End IfEnd Sub
Private Sub myDataGridView_MouseUp(ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs)
''# See if the left mouse button was clicked
If e.Button = MouseButtons.Left Then
''# Check the HitTest information for this click location
If myDataGridView.HitTest(e.X, e.Y) = DataGridView.HitTestInfo.Nowhere Then
End If
End Sub
当然,您也可以继承现有的DataGridView控件,以便将所有这些功能合并组合到一个自定义控件中。您需要像上面显示的那样覆盖它的OnMouseUp method。为了方便起见,我还想提供一个公共的DeselectAll方法,它既调用ClearSelection方法,又将CurrentCell属性设置为Nothing。(Code示例在VB.NET中都是任意的,因为问题没有指定语言-如果这不是您的母语,请道歉。
OnMouseUp
DeselectAll
7y4bm7vi2#
C#版本:
if (e.Button == System.Windows.Forms.MouseButtons.Left) { DataGridView.HitTestInfo hit = dgv_track.HitTest(e.X, e.Y); if (hit.Type == DataGridViewHitTestType.None) { dgv_track.ClearSelection(); dgv_track.CurrentCell = null; } }
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
DataGridView.HitTestInfo hit = dgv_track.HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.None)
dgv_track.ClearSelection();
dgv_track.CurrentCell = null;
}
f8rj6qna3#
我发现了为什么我的第一行是默认选择的,并发现了如何不选择它默认。默认情况下,我的datagridview是windows窗体上具有第一个制表位的对象。使tab在另一个对象上首先停止(也许对datagrid禁用tabstop也可以)禁用选择第一行
laik7k3q4#
设置
dgv.CurrentCell = null;
当用户点击DGV的空白部分时。
vwkv1x7d5#
我遇到了同样的问题,并找到了解决办法(不完全是我自己,但有互联网)
Color blue = ColorTranslator.FromHtml("#CCFFFF");Color red = ColorTranslator.FromHtml("#FFCCFF");Color letters = Color.Black;foreach (DataGridViewRow r in datagridIncome.Rows){ if (r.Cells[5].Value.ToString().Contains("1")) { r.DefaultCellStyle.BackColor = blue; r.DefaultCellStyle.SelectionBackColor = blue; r.DefaultCellStyle.SelectionForeColor = letters; } else { r.DefaultCellStyle.BackColor = red; r.DefaultCellStyle.SelectionBackColor = red; r.DefaultCellStyle.SelectionForeColor = letters; }}
Color blue = ColorTranslator.FromHtml("#CCFFFF");
Color red = ColorTranslator.FromHtml("#FFCCFF");
Color letters = Color.Black;
foreach (DataGridViewRow r in datagridIncome.Rows)
if (r.Cells[5].Value.ToString().Contains("1")) {
r.DefaultCellStyle.BackColor = blue;
r.DefaultCellStyle.SelectionBackColor = blue;
r.DefaultCellStyle.SelectionForeColor = letters;
else {
r.DefaultCellStyle.BackColor = red;
r.DefaultCellStyle.SelectionBackColor = red;
这是一个小技巧,你可以看到一行被选中的唯一方法是通过第一列(不是列[0],而是因此)。当您单击另一行时,您将不再看到蓝色选择,只有箭头指示已选择的行。如您所知,我在网格视图中使用rowSelection。
bf1o4zei6#
在VB.net中使用Sub:
Private Sub dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv.MouseUp ' deselezionare se click su vuoto If e.Button = MouseButtons.Left Then ' Check the HitTest information for this click location If Equals(dgv.HitTest(e.X, e.Y), DataGridView.HitTestInfo.Nowhere) Then dgv.ClearSelection() dgv.CurrentCell = Nothing End If End IfEnd Sub
Private Sub dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv.MouseUp
' deselezionare se click su vuoto
' Check the HitTest information for this click location
If Equals(dgv.HitTest(e.X, e.Y), DataGridView.HitTestInfo.Nowhere) Then
dgv.ClearSelection()
dgv.CurrentCell = Nothing
bxgwgixi7#
System. out. println();return null;用这个方法,你的情况会有用的
7条答案
按热度按时间fdbelqdn1#
要删除
DataGridView
中的所有行和单元格,您可以使用ClearSelection
method:如果你不希望第一行/单元格显示为选中状态,你可以将
CurrentCell
property设置为Nothing
/null
,这将暂时隐藏焦点矩形,直到控件再次获得焦点:要确定用户何时单击了
DataGridView
的空白部分,您必须处理其MouseUp
事件。在这种情况下,您可以HitTest
单击位置,并观察它是否指示HitTestInfo.Nowhere
。举例来说:当然,您也可以继承现有的
DataGridView
控件,以便将所有这些功能合并组合到一个自定义控件中。您需要像上面显示的那样覆盖它的OnMouseUp
method。为了方便起见,我还想提供一个公共的DeselectAll
方法,它既调用ClearSelection
方法,又将CurrentCell
属性设置为Nothing
。(Code示例在VB.NET中都是任意的,因为问题没有指定语言-如果这不是您的母语,请道歉。
7y4bm7vi2#
C#版本:
f8rj6qna3#
我发现了为什么我的第一行是默认选择的,并发现了如何不选择它默认。
默认情况下,我的datagridview是windows窗体上具有第一个制表位的对象。使tab在另一个对象上首先停止(也许对datagrid禁用tabstop也可以)禁用选择第一行
laik7k3q4#
设置
当用户点击DGV的空白部分时。
vwkv1x7d5#
我遇到了同样的问题,并找到了解决办法(不完全是我自己,但有互联网)
这是一个小技巧,你可以看到一行被选中的唯一方法是通过第一列(不是列[0],而是因此)。当您单击另一行时,您将不再看到蓝色选择,只有箭头指示已选择的行。如您所知,我在网格视图中使用rowSelection。
bf1o4zei6#
在VB.net中使用Sub:
bxgwgixi7#
System. out. println();
return null;
用这个方法,你的情况会有用的