CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2007
    Posts
    116

    Lightbulb How to get DataGriedView CheckBox is Check or not!

    I have CheckBox in my datagridview1 and I am trying to get CheckBox is check or not.

    for(int i; i< datagridview1.rows.count; i++)
    {
    bool IsChecked = ((CheckBox)datagridview1.rows[i].Cell["ID"].Value).Checked;
    }

    Above code is not working,please let me know where I am wrong.
    I am using C#, VisualStudio2008.

    Thanks

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How to get DataGriedView CheckBox is Check or not!

    this should work for you...

    Code:
    bool IsChecked  = bool.Parse(datagridview1.Rows[i].Cells["ID"].Value.ToString());
    ===============================
    My Blog

  3. #3
    Join Date
    Mar 2007
    Posts
    116

    Re: How to get DataGriedView CheckBox is Check or not!

    Object reference not set to an instance of an object.

    I am getting above error.

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How to get DataGriedView CheckBox is Check or not!

    I normally use the number instead of the name of the column. if it's the first column in your grid, use this..

    Code:
    bool IsChecked  = bool.Parse(datagridview1.Rows[i].Cells[0].Value.ToString());
    I know this works because I currently use this exact code.

  5. #5
    Join Date
    Mar 2007
    Posts
    116

    Re: How to get DataGriedView CheckBox is Check or not!

    I am getting same error again!

  6. #6
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How to get DataGriedView CheckBox is Check or not!

    what column is your checkbox in?
    ===============================
    My Blog

  7. #7
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How to get DataGriedView CheckBox is Check or not!

    can you post the designer code for the DataGridView?
    ===============================
    My Blog

  8. #8
    Join Date
    Mar 2007
    Posts
    116

    Re: How to get DataGriedView CheckBox is Check or not!

    here you go.

    namespace Value
    {
    partial class Form3
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
    if (disposing && (components != null))
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.dataGridView1 = new System.Windows.Forms.DataGridView();
    this.ID= new System.Windows.Forms.DataGridViewCheckBoxColumn();
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
    this.SuspendLayout();
    //
    // dataGridView1
    //
    this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    this.ID});
    this.dataGridView1.Location = new System.Drawing.Point(32, 67);
    this.dataGridView1.Name = "dataGridView1";
    this.dataGridView1.Size = new System.Drawing.Size(487, 302);
    this.dataGridView1.TabIndex = 0;
    this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
    this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
    //
    // ID
    //
    this.Check.HeaderText = "ID";
    this.Check.Name = "ID";
    //
    // Form3
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(568, 446);
    this.Controls.Add(this.dataGridView1);
    this.Name = "Form3";
    this.Text = "Form3";
    this.Load += new System.EventHandler(this.Form3_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
    this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.DataGridViewCheckBoxColumn ID;
    }
    }

  9. #9
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How to get DataGriedView CheckBox is Check or not!

    As a quick fix, here is what you can do...

    Code:
    bool IsChecked = (dataGridView1.Rows[i].Cells[0].Value == null) ? false : bool.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
    Basically, depending on how you have your grid setup, not all rows will have a value for the checkbox. Just because it's unchecked doesn't mean it's "false". It could be null. This will handle that scenario. This is an inline IF statement. If the value is null, return false, else, parse the value to a boolean value and return that value.

    Now, you can give the cell a default value when you add your rows to the grid. Giving it a "true" or "false" will also fix the issue when you try to loop through it.

    However, if you have the property AllowUserToAddRows set to TRUE, this will give the "blank" row below the other rows in the grid. When you are looping through the rows, this row IS PART OF THE GRID. Since it has no values, it will be null, which will also throw the error.
    ===============================
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured