2013-01-15 69 views
0

前段時間我問了這個問題Hide some datagridview checkbox cell。我無法得到一個好的答案,所以我決定嘗試一些不同的東西。更改DataGridCheckBoxCell的複選框的顏色C#Winform

我有一個網格與貸款的所有分期付款。每個部分都有一個複選框。當該複選框被選中時,我表示客戶正在爲此付款。

現在,當安裝已付款時,不需要複選框。現在,我禁用付款分期付款的複選框。這是可見的,但點擊它什麼都不做。但我想要做的是用綠色畫複選框。像這樣的圖像(顏色被塗油漆,它不是一個真正的圖像)

enter image description here

好吧,我曾嘗試使用此代碼之前隱藏的複選框:

private void dgv_Cuotas_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
    {   
     if (e.RowIndex >= 0 && e.ColumnIndex >= 0) 
     { 
      if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar" && Convert.ToBoolean(dgv_Cuotas.Rows[e.RowIndex].Cells["pagada"].Value) == true) 
      { 
       e.CellStyle.BackColor = Color.Green; 
       e.PaintBackground(e.ClipBounds, true); 
       e.Handled = true; 
      } 
     } 
    } 

enter image description here

有了這個我畫了整個單元格,但我只想繪製複選框。另外,根據該實施,當我點擊該單元格,我可以看到I'm點擊複選框,背景顏色變爲白色,像這樣:

enter image description here

I'm創建的DataGridView(dgv_Cuotas)通過在運行時分配Datasource = lista_cuota(分期付款清單)來確定行和列。這就是爲什麼由代碼之後添加複選框列:

DataGridViewCheckBoxColumn chbox = new DataGridViewCheckBoxColumn(); 
     { 
      chbox.CellTemplate = new DataGridViewCheckBoxCell(); 
      chbox.HeaderText = ""; 
      chbox.Name = "Seleccionar"; 
      chbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; 
      chbox.FlatStyle = FlatStyle.Standard; 
     } 
     dgv_Cuotas.Columns.Insert(16, chbox); 
     dgv_Cuotas.Columns[16].DisplayIndex = 0; 

這樣做,這樣,我不能「隱藏」複選框單元格分配DataGridViewTextBoxCell到特定的細胞,所以我想嘗試畫只是代替複選框。

由於我使用實體框架,我不想向類中添加新字段。

回答