Friday, 2 March 2012

DatagridView editable checkbox creation at runtime

* Creating chekboxs at runtime to a datagridView in windows application
* Get the value of checked column (using cellclick event)
*  Check whether the check box is checked or not on button click
* make checkbox column readonly =true and remaining or Readonly false(using cellbeginedit event)



 Creating chekboxs at runtime to a datagridView in windows application
if (empdata.Tables[0].Rows.Count != 0)
                    {
                        dataGrid1.DataSource = empdata.Tables[0];
                        foreach (DataGridViewRow row in dataGrid1.Rows)
                        {

                            //Get the appropriate cell using index, name or whatever and cast to DataGridViewCheckBoxCell
                            DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;

                            //Compare to the true value because Value isn't boolean
                            if (cell == null)
                            {
                                column = new DataGridViewCheckBoxColumn();
                                {
                                    column.HeaderText = "select";
                                    column.AutoSizeMode =
                                        DataGridViewAutoSizeColumnMode.DisplayedCells;
                                    //column.FlatStyle = FlatStyle.Standard;
                                    //column.ThreeState = true;
                                    column.CellTemplate = new DataGridViewCheckBoxCell();
                                    column.ReadOnly = false;
                                    //column.CellTemplate.Style.BackColor = Color.Beige;
                                }
                                dataGrid1.Columns.Insert(0, column);
                                //dataGrid1.Columns["select"].ReadOnly = false;
                                //dataGrid1.Columns[0].ReadOnly = false;


                            }
                            else
                            {
                                // dataGrid1.Columns.Clear();
                            }

                        }

Get the value of checked column (using cellclick event)

  private void dataGrid1_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {

            for (int i = 0; i < this.dataGrid1.RowCount; i++)
            {
                string re_value = dataGrid1.Rows[i].Cells[0].Selected.ToString();
                this.dataGrid1.Rows[i].Cells[0].Value = re_value; //"true";           

            }



        }

*  Check whether the check box is checked or not
 foreach (DataGridViewRow dr in dataGrid1.Rows)
                        {
                           
                            if (Convert.ToBoolean(dr.Cells[0].Value) == true)
                            {
                                //rows_with_checked_column.Add(row);
                                uniqueid = dr.Cells[1].Value.ToString();
                            }


                        }
 make checkbox column readonly =true and remaining or Readonly false(using cellbeginedit event)
 private void dataGrid1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex != 0)
            {
                e.Cancel = true;
            }

        }


No comments:

Post a Comment