DataGridView Dynamic Bind CheckBox

Source: Internet
Author: User

Tag: Display format Enter new ALS oid Show check selection

The following shows how to bind a CheckBox dynamically in DataGridView:

 Public classtest{/// <summary>    ///Constructors/// </summary>     PublicTest () {InitializeComponent (); //generate a Select all CheckBoxGeneratecheckbox (); }
/// <summary> ///Selection events for the Select All button/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidCheckboxall_checkedchanged (Objectsender, EventArgs e) { foreach(DataGridViewRow Drinchdatagridview1.rows) {Dr. cells[0]. Value = (CheckBox) dataGridView1.Controls.Find ("Checkboxall",true)[0]). Checked; } } /// <summary> ///generate a Select all CheckBox/// </summary> Private voidGeneratecheckbox () {//Check if at least one column is bound in the DataGridView1 if(string. IsNullOrEmpty (datagridview1.columns[0]. Name))return; //A Column object that is specifically used to display the CheckBox control in DataGridView, and when the object is added in DataGridView1, a new Checkbox control is automatically created for each row when the data is boundDatagridviewcheckboxcolumn Gridviewcheckboxcol =NewDatagridviewcheckboxcolumn {Width= -, HeaderText="", DefaultCellStyle={Alignment=Datagridviewcontentalignment.middlecenter}, ReadOnly=false//If True, the CheckBox cannot be selected }; //set the first column of DataGridView1 to a CHECKBOX-formatted columnDataGridView1.Columns.Insert (0, Gridviewcheckboxcol); //Gets the rectangle that represents the cell display area in the DataGridView1, setting the relative position of the CheckBox through the position of the rectangleRectangle Rectangle = Datagridview1.getcelldisplayrectangle (0, -1,true); Rectangle. X= Rectangle. location.x + rectangle. Width + -; Rectangle. Y= Rectangle. LOCATION.Y + rectangle. Height +Ten; //Create a new CheckBox object for "Select All" CheckboxallCheckBox Chkofall =NewCheckBox (); Chkofall.name="Checkboxall"; Chkofall.size=NewSize ( -, -); Chkofall.location=rectangle. Location; //bind a Select all event for CheckboxallChkofall.checkedchanged + =NewEventHandler (checkboxall_checkedchanged); //Add the Checkboxall to the DataGridView1DataGridView1.Controls.Add (Chkofall); }}

DataGridView Dynamic Bind CheckBox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.