c# - Datagridview复选框单元格更改按钮和画线的颜色

标签 c# winforms checkbox datagridview colors

问题是通过单击 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)
        }
    }

最佳答案

下面是 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);
    }
}

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

关于c# - Datagridview复选框单元格更改按钮和画线的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45572630/

相关文章:

c# - 使用 'Microsoft.Office.Interop.Word._Document.Close' 时出现警告 CS0467

winforms - 列表框中项目的菜单?

c# - 是否可以开设私有(private)类?

c# - 在 Visual Studio 2010 中构建不同的项目集

c# - 使用通用参数获取/计算 .NET 类型的外部 MSDN url

windows - ClickOnce 是否可以在不询问用户的情况下自动更新?

c# - 如何在列表框中的项目之间添加填充?

php - 如何在codeigniter中将复选框中的多值插入到数据库中

javascript - HTML 复选框生成选中或单击事件绑定(bind)

javascript - jquery dom explorer代码的优化