You must Sign In to post a response.
  • Category: .NET

    add check box in datagridview

    Hi

    im working in c#.net. im using datagridview, i want to add a checkbox at the end of the
    all the columns.while im running my application if i tick the checkbox it should store in database how to do that?
  • #143103
    hi
    hope this will help u
    this code will help u to even check and uncheck the check box property



    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;

    class Form1 : Form
    {
    private DataGridView dataGridView1 = new DataGridView();

    [STAThread]
    public static void Main()
    {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
    }

    public Form1()
    {
    this.AutoSize = true;
    this.Load += new EventHandler(Form1_Load);
    }

    public void Form1_Load(object sender, EventArgs e)
    {
    DataGridViewCheckBoxColumn column0 =
    new DataGridViewCheckBoxColumn();
    DataGridViewDisableCheckBoxColumn column1 =
    new DataGridViewDisableCheckBoxColumn();
    column0.Name = "CheckBoxes";
    column1.Name = "DisableCheckBoxes";
    dataGridView1.Columns.Add(column0);
    dataGridView1.Columns.Add(column1);
    dataGridView1.RowCount = 8;
    dataGridView1.AutoSize = true;
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
    DataGridViewContentAlignment.MiddleCenter;

    // Set the text for each checkBox.
    for (int i = 0; i < dataGridView1.RowCount; i++)
    {
    dataGridView1.Rows.Cells["DisableCheckBoxes"].Value =
    false;
    }

    dataGridView1.CellValueChanged +=
    new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
    dataGridView1.CurrentCellDirtyStateChanged +=
    new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
    dataGridView1.CellClick +=
    new DataGridViewCellEventHandler(dataGridView1_CellClick);

    this.Controls.Add(dataGridView1);
    }

    // This event handler manually raises the CellValueChanged event
    // by calling the CommitEdit method.
    void dataGridView1_CurrentCellDirtyStateChanged(object sender,
    EventArgs e)
    {
    if (dataGridView1.IsCurrentCellDirty)
    {
    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    }

    // If a check box cell is clicked, this event handler disables
    // or enables the checkBox in the same row as the clicked cell.
    public void dataGridView1_CellValueChanged(object sender,
    DataGridViewCellEventArgs e)
    {
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
    {
    DataGridViewDisableCheckBoxCell checkBoxCell =
    (DataGridViewDisableCheckBoxCell)dataGridView1.
    Rows[e.RowIndex].Cells["DisableCheckBoxes"];

    DataGridViewCheckBoxCell checkCell =
    (DataGridViewCheckBoxCell)dataGridView1.
    Rows[e.RowIndex].Cells["CheckBoxes"];
    checkBoxCell.Enabled = !(Boolean)checkCell.Value;

    dataGridView1.Invalidate();
    }
    }

    SONAL

  • #143131
    Thank u very much. its veryusefull for me.

  • #332155
    This is really very helpful..... Thanx a lot sonal


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.