繁体   English   中英

如何在dataGridView Header Column C#上创建一个复选框?

[英]how to create a CheckBox on dataGridView Header Column C#?

如何在 dataGridView 中创建复选框标题,我有一个 dataGridView,我有一个 CheckBox Column index=12,我需要在标题上做一个复选框,这是我的代码,但我有一个错误,我该如何更改此代码修复它,以创建复选框标题。 任何机构都可以提供帮助???? 我添加了一些代码来解释我需要做什么。

DataGridViewCheckBoxCell checkBoxCell = new DataGridViewCheckBoxCell();
 //i have error here
dataGridViewSellers.Columns[12].HeaderCell = checkBoxCell;

foreach(Seller aSeller in sellers){
    DataGridViewButtonCell buttonEditCell = new DataGridViewButtonCell();
    DataGridViewButtonCell buttonDeleteCell = new DataGridViewButtonCell();
    DataGridViewButtonCell buttonRestPassCell = new DataGridViewButtonCell();
    dataGridViewSellers[0, i].Value = aSeller.IdNumber;
    dataGridViewSellers[1, i].Value = aSeller.Name;
    dataGridViewSellers[2, i].Value = aSeller.LastName;
    dataGridViewSellers[3, i].Value = aSeller.PhoneNumber;
    dataGridViewSellers[4, i].Value = aSeller.Address;
    dataGridViewSellers[5, i].Value = aSeller.Email;
    dataGridViewSellers[6, i].Value = aSeller.UserName;
    dataGridViewSellers[7, i].Value = aSeller.EntryDate;
    dataGridViewSellers[8, i].Value = aSeller.BusinessHours;
    dataGridViewSellers[9, i] = buttonEditCell;
    dataGridViewSellers[10, i] = buttonDeleteCell;
    dataGridViewSellers[11, i] = buttonRestPassCell;
}

这是固定代码..

    private void AddHeaderCheckBox()
    {
        HeaderCheckBox = new CheckBox();

        HeaderCheckBox.Size = new Size(15, 15);

        //Add the CheckBox into the DataGridView
        this.dataGridViewSellers.Controls.Add(HeaderCheckBox);
    }

    private void ResetLocation(int ColumnIndex, int RowIndex)
    {
        //Get the column header cell bounds
        Rectangle oRectangle =
          this.dataGridViewSellers.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);

        Point oPoint = new Point();


        oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
        oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;

        //Change the location of the CheckBox to make it stay on the header
        HeaderCheckBox.Location = oPoint;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM