2017-08-08 59 views
2

问题是通过单击datagridview复选框单元格来更改按钮的颜色,并通过单击相同的复选框来另外突出显示一个绘制的箭头。Datagridview复选框单元格更改按钮和绘制线的颜色

这正在与TaW的解决方案一起工作。感谢那。

private void dataGridView1_CellContentClick(object f_sender_dgv, DataGridViewCellEventArgs f_event_dgv) 
    {  

     DataGridView dataGridView1 = f_sender_dgv as DataGridView; 


     dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 

     DataGridViewCell cb_1 = dataGridView1[2, 0];// 
     //DataGridViewCell cb_1 = dataGridView1[f_event_dgv.ColumnIndex , f_event_dgv.RowIndex]; 

     if (cb_1 is DataGridViewCheckBoxCell) 
     { 
      bool c = (bool)(cb_1 as DataGridViewCheckBoxCell).Value; 
      button_gv_1.BackColor = c ? Color.Yellow : Color.Snow; 
      button_v_5.BackColor = c ? Color.Yellow : Color.Snow; 
     } 
     this.Invalidate(); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     Graphics g; 
     g = e.Graphics; 
     Pen myPen = new Pen(Color.Black); 
     myPen.Width = 1; 
     myPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; 
     //g.DrawLine(myPen1, 400, 700, 1000, 100);//(X_start, Y_start, X_ende, Y_ende) 

     // test if data is loaded: 
     if (!(dataGridView1.RowCount > 0 && dataGridView1.ColumnCount > 2)) return; 

     //use the correct indices = [e.ColumnIndex, e.RowIndex] 
     DataGridViewCell cell = dataGridView1[2,0]; 
     if (cell is DataGridViewCheckBoxCell && cell.Value != null) 
     { 
      bool c = (bool)(cell as DataGridViewCheckBoxCell).Value; 
      Pen myPen1 = c ? Pens.Yellow : Pens.Black; 
      g.DrawLine(myPen1, 400, 700, 1000, 100);//(X_start, Y_start, X_ende, Y_ende) 
     } 
    } 
+0

什么,如果现在点击复选框时发生了什么?你的代码是什么datagridview? – MaCron

回答

1

下面是button.BackColor等效代码:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 

    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex]; 
    if (cell is DataGridViewCheckBoxCell) 
    { 
     bool c = (bool) (cell as DataGridViewCheckBoxCell).Value; 
     button1.BackColor = c ? Color.Yellow: Color.Snow; 
     this.Invalidate(); 
    } 
} 

Paint事件可以测试该细胞在或多或少相同的方式:

protected override void OnPaint(object sender, PaintEventArgs e) 
{ 
    // test if data are loaded: 
    if (! (dataGridView1.RowCount > 0 && dataGridView1dgv.ColumnCount > 2)) return; 

    DataGridViewCell cell = dataGridView1[0, 2]; // <-- use the correct indices!! 
    if (cell is DataGridViewCheckBoxCell && cell.Value != null) 
    { 
     bool c = (bool)(cell as DataGridViewCheckBoxCell).Value; 

     Pen myPen = c ? Pens.Yellow : Pens.Snow; 
     e.Graphics.DrawLine(myPen, 160, 285, 250, 285); 
    } 
} 

..或测试您设置的课程级别标志。

+0

感谢您的代码。它在这个最小的工作示例中工作得很好。现在我将它实现到主代码中,并产生了一个我无法处理的错误。你知道这是从哪里来的吗? – Paul

+0

Nun,die Medung ist ja klar:Einer der Indizes ist zugroß; du musst einerseits die richtigen Indizes verwenden,die sehen ja ok aus;如果(!(dgv.RowCount> 0和dgv.ColumnCount> 2))返回,则返回;否则,返回;否则返回。 '或ähnliches。 Gerade das Paint Ereignis tritt ja schon vor dem Laden der Daten auf! - Ich hab die Antwortergänzt.. – TaW