[英]How can I make header not clickable for DataGridViewButtonColumn in a datagridview c#.net
我有一個 + 和 - 按鈕 (DataGridViewButtonColumn) 可以增加和減少項目的數量,我的問題是這些列的標題是可點擊的,即使我將我的排序模式設置為 NotSortable:
我似乎找不到標題的“啟用”屬性。
如果我單擊標題或按鈕,它們都具有遞增/遞減的功能。 我希望按鈕僅具有該功能並完全禁用 + 標題。
這是我的 DataGridViewButtonColumn 代碼:
private void stockGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (stockGridView.Columns[e.ColumnIndex].Name == "Add") {
quantity_change(Add);
}
if (stockGridView.Columns[e.ColumnIndex].Name == "Subtract") {
quantity_change(Subtract);
}
}
void quantity_change(object sender) {
var row = stockGridView.CurrentRow;
if (row == null || row.Index < 0)
return;
var unit = (sender == Add) ? 1 : -1;
var quantity = Convert.ToInt32(row.Cells["Quantity"].Value) + unit;
row.Cells["Quantity"].Value = quantity;
}
檢查 DataGridViewCellEventArgs 中的 RowIndex 值。 對於標題行,我認為它將是負面的。 因此,如果您在事件處理程序中添加 test e.RowIndex >= 0,您應該能夠忽略標題單元格單擊。 此外,將 e.RowIndex 傳遞給quantity_change 方法而不是依賴CurrentRow 可能更一致,但這取決於您。 還有一個建議 - 而不是將 Column 作為quantity_change 傳遞整數增量的參數傳遞,以便在添加的情況下調用quantiy_change(1),在減法的情況下調用quantiy_change(-1)。 然后,您將擺脫確定單位變量的行,並稍微簡化您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.