我有一個DataGridView
與幾列和幾行數據。其中一列是DataGridViewCheckBoxColumn
和(基於行中的其他數據)我希望在某些行中「隱藏」複選框。我知道如何讓它只讀,但我寧願它根本不顯示,或至少顯示不同(灰色)比其他複選框。這可能嗎?C#DataGridViewCheckBoxColumn Hide/Gray-Out
4
A
回答
12
某些解決方法:將其設置爲只讀並將顏色更改爲灰色。 對於一個特定的細胞:
dataGridView1.Rows[2].Cells[1].Style.BackColor = Color.LightGray;
dataGridView1.Rows[2].Cells[1].ReadOnly = true;
或者更好,但更「複雜」的解決方案:
假設你有2列:先用數量,第二,複選框,應該是不可見的,當數> 2。您可以處理CellPainting
事件,僅繪製邊框(例如背景)並打破休息的繪製。添加事件CellPainting
爲DataGridView中(可選測試的DBNull值,以避免異常增加的空行新數據時):
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//check only for cells of second column, except header
if ((e.ColumnIndex == 1) && (e.RowIndex > -1))
{
//make sure not a null value
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != DBNull.Value)
{
//put condition when not to paint checkbox
if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value) > 2)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.Border | DataGridViewPaintParts.Background); //put what to draw
e.Handled = true; //skip rest of painting event
}
}
}
}
它應該工作,但是如果你在第一列,在那裏你檢查條件手動更改值,必須刷新第二單元,所以添加其他事件一樣CellValueChanged
:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dataGridView1.InvalidateCell(1, e.RowIndex);
}
}
0
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell.aspx
DataGridViewCheckBoxCell.Visible = false;
編輯:哦,等等,它是隻讀的。 DERP。
在這種情況下,請嘗試用空的DataGridViewTextBoxCell替換單元格。
0
從Customize the Appearance of Cells in the Windows Forms DataGridView Control兩者,你可以趕上CellPainting事件,如果以只讀模式不繪製細胞。例如:
public Form1()
{
InitializeComponent();
dataGridView1.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}
private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
// Change 2 to be your checkbox column #
if (this.dataGridView1.Columns[2].Index == e.ColumnIndex && e.RowIndex >= 0)
{
// If its read only, dont draw it
if (dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly)
{
// You can change e.CellStyle.BackColor to Color.Gray for example
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
e.Handled = true;
}
}
}
}
唯一需要注意的是,你需要調用dataGridView1.Invalidate();
當你改變DataGridViewCheckBox
細胞的之一ReadOnly
財產。
相關問題
- 1. DataGridViewCheckBoxColumn
- 2. .NET datagridviewcheckboxcolumn
- 3. DataGridViewCheckBoxColumn窗口
- 4. DataGridViewButtonColumn充當DataGridViewCheckBoxColumn
- 5. 添加一個DataGridViewCheckBoxColumn
- 6. 檢查DataGridViewCheckBoxColumn的值
- 7. DataGridViewCheckBoxColumn CellContentClick事件問題
- 8. 綁定XML真假DataGridViewCheckBoxColumn
- 9. DataGridViewCheckBoxColumn:布爾列上的FormatException
- 10. c#DataGridViewCheckBoxColumn抑制點擊時的默認行爲
- 11. 無法在DataGridViewCheckBoxColumn中複選框?
- 12. DataGridViewCheckBoxColumn - 如果複選框檢查問題
- 13. 將DataGridViewCheckBoxColumn綁定到布爾值數組
- 14. CellContentClick事件中的Datagridviewcheckboxcolumn問題(VB Net)
- 15. 綁定DataGridViewCheckBoxColumn沒有更新綁定源
- 16. DataGridViewCheckBoxColumn更改複選框顯示
- 17. C#編程創建datagridview - DataGridViewCheckBoxColumn cellValueChanged檢查狀態總是返回FALSE
- 18. 有沒有辦法阻止DataGridViewCheckBoxColumn自動檢查點擊?
- 19. 如何在DatagridviewCheckBoxColumn的Header中添加複選框控件?
- 20. 手動生成的DataGridViewCheckBoxColumn不更新綁定對象
- 21. 工具提示沒有顯示在DataGridViewCheckBoxColumn上
- 22. 無法選擇或取消DataGridViewCheckBoxColumn細胞編程
- 23. WPF限制在datagridviewcheckboxcolumn中檢查的行數
- 24. 爲什麼DatagridviewCheckboxColumn的複選框不被檢查?
- 25. DataGridViewCheckBoxColumn不入店應有的水平保護
- 26. 如何獲取DataGridViewCheckBoxColumn以顯示DataRelation狀態?
- 27. WinForm應用程序:無法設置DataGridViewCheckBoxColumn的默認值
- 28. 當第一個checkBox列被選中時獲取兩個DataGridViewCheckBoxColumn
- 29. DataGridViewCheckBoxColumn:如何更新屬性更改綁定的DataSource而不是驗證
- 30. DatagridViewCheckBoxColumn將複選框左側的文本定位到其後面的文本
'Visible'屬性是隻讀的。 – john