如何在Datagridview标题列C#

时间:2015-12-11 22:03:11

标签: c# checkbox datagridview

如何在Gridview标题栏上添加一个复选框,并选择all / deselect all? 单击任何列时,我想更改复选框值并计算新值。它的工作但不完美。

     private void setCheckBoxDG()
        {
            DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
            checkboxColumn.Width = 30;
            checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            DGDoc.Columns.Insert(0, checkboxColumn);

            Rectangle rect = DGDoc.GetCellDisplayRectangle(0, -1, true);

            rect.X = DGDoc.Size.Width - 28;
            rect.Y = rect.Y + 2;
            CheckBox checkboxHeader = new CheckBox();
            checkboxHeader.Name = "checkboxHeader";
            checkboxHeader.Size = new Size(18, 18);
            checkboxHeader.Location = rect.Location;
            checkboxHeader.CheckedChanged += new EventHandler (checkboxHeader_CheckedChanged);
            DGDoc.Controls.Add(checkboxHeader);
        }

and 

选择全部/取消选择:

string strIDs = "";
Int64 intSumPrice = 0;
Int64 intSumTax = 0;
Int64 intSumPay = 0;
foreach (DataGridViewRow item in DGDoc.Rows)
{
    if (item.Cells[0].Value == null)
        item.Cells[0].Value = "True";
        if (bool.Parse(item.Cells[0].Value.ToString()))
        {
            item.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(241, 215, 215);
            strIDs += "," + item.Cells[1].Value.ToString();
            intSumPrice += Int64.Parse(item.Cells[4].Value.ToString());
            intSumTax += Int64.Parse(item.Cells[5].Value.ToString());
            intSumPay += Int64.Parse(item.Cells[6].Value.ToString());
        }
        else
        {
            item.DefaultCellStyle.BackColor = System.Drawing.Color.Empty;
        }
    }
    DGDoc.EndEdit();

    txtSumPrice.Text = intSumPrice.ToString();   
    txtSumTax.Text = intSumTax.ToString();
    txtSumPay.Text = intSumPay.ToString();
    DGDoc.Refresh();
}
你知道另一种帮助我的方法吗?

0 个答案:

没有答案