Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c# - 将CheckBox添加到C#中的Datagridview标头中,并使用更改滚动条更改复选框的位置(Add CheckBox to Datagridview Header in C# and change place of checkbox with change scrolbar)

I use this code to locate a checkbox in Header of Datagridview.

(我使用此代码在Datagridview标头中找到一个复选框。)

But with changing scrolbar of datagridview, locate of checkbox changed.

(但是随着datagridview滚动栏的更改,复选框的位置也发生了变化。)

        // customize dataviewgrid, add checkbox column
        DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
        checkboxColumn.Width = 30;
        checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dataGridView1.Columns.Insert(0, checkboxColumn);

        // add checkbox header
        Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);

        // set checkbox header to center of header cell. +1 pixel to position correctly.
        rect.X = rect.Location.X + 8;
        rect.Y = rect.Location.Y + 10;
        rect.Width  = rect.Size.Width;
        rect.Height  = rect.Size.Height;

        CheckBox checkboxHeader = new CheckBox();
        checkboxHeader.Name = "checkboxHeader";
        checkboxHeader.Size = new Size(15, 15);
        checkboxHeader.Location = rect.Location;
        checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);

        dataGridView1.Controls.Add(checkboxHeader);

Like this pictures :

(像这样的图片:)

在此处输入图片说明

  ask by user3812553 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It was very easy.

(这很容易。)

I use this code and solved it.

(我使用此代码并解决了它。)

dataGridView1.Columns[0].Frozen = true;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...