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