[英]How to Disable Double Click on the Header of a DataGridView
我想做一些東西,而雙擊Gridview(行)而不是單個單元格。 意味着雙擊事件處理程序不在MouseDoubleClick事件上。 但我無法禁用標題列和行雙擊事件..並且當我雙擊GridView行時,還想將數據加載到組合框(ComboBox在同一窗體上)。 請幫幫我..!!!
private void gvLoadAllData_DoubleClick(object sender, EventArgs e)
{
if()
{
//Do Something
}
}
我已經定義了CellDoubleClick
事件並做了一些事情,我的問題是雙擊標題會觸發該事件並因此導致應用程序崩潰。
考慮到Disaster的想法 ,我剛剛添加以下內容來解決該事件。
if (e.RowIndex == -1)
return;
DoubleClick on Row標頭
要處理雙擊行標題,請處理DataGridView
RowHeaderMouseDoubleClick
事件:
private void dataGridView1_RowHeaderMouseDoubleClick(object sender,
DataGridViewCellMouseEventArgs e)
{
var rowIndex = e.RowIndex;
//You handled a double click on row header
//Do what you need
}
DoubleClick列標題
要處理雙擊DataGridView
列標題句柄ColumnHeaderMouseDoubleClick
事件:
private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender,
DataGridViewCellMouseEventArgs e)
{
var columnIndex = e.ColumnIndex;
//You handled a double click on column header
//Do what you need
}
使用DoubleClick
此外,如果您因任何原因想要使用DoubleClick
事件,請寫下以下內容:
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
var g = sender as DataGridView;
if (g != null)
{
var p = g.PointToClient(MousePosition);
var hti = g.HitTest(p.X, p.Y);
if (hti.Type == DataGridViewHitTestType.ColumnHeader)
{
var columnIndex = hti.ColumnIndex;
//You handled a double click on column header
//Do what you need
}
else if (hti.Type == DataGridViewHitTestType.RowHeader)
{
var rowIndex = hti.RowIndex;
//You handled a double click on row header
//Do what you need
}
}
}
這樣做:
private void itemsList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex>-1)
HereIsNotHeader();
}
並將其綁定到CellDoubleClick
事件
這個怎么樣? 只有在datagridcell上觸發doubleclick事件時才會觸發它,而不是標題或空格(滾動條)。
private void gvLoadAllData_DoubleClick(object sender, MouseButtonEventArgs e)
{
if ((e.OriginalSource as FrameworkElement).Parent is DataGridCell) DoSomething();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.