Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Checkbox in Datagrid - check in one click

Checkbox in Datagrid - check in one click

Scheduled Pinned Locked Moved C#
questiondatabasehelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ruchi Gupta
    wrote on last edited by
    #1

    Hi All, I am stuck at some point in implementing checkbox column in datagrid. This is how I am adding checkbox column to the DataGridTableStyle. DataGridColumnStyle boolCol = new DataGridBoolColumn(); boolCol.MappingName = "PrimaryFlag"; boolCol.HeaderText = "Primary"; ((DataGridBoolColumn)boolCol).TrueValue = "true"; ((DataGridBoolColumn)boolCol).FalseValue = "false"; ((DataGridBoolColumn)boolCol).ReadOnly = false; boolCol.Width = 40; ts.GridColumnStyles.Add(boolCol); And from database I am receiving "true" or "false" values in the PrimaryFlag column. When I set the datasource of datagrid with the dataset that I receive from database, it correctly sets the checkboxes as checked or unchecked based on "true" or "false" values in the PrimaryFlag column. Now my question is how can I handle the event such that with one click on the checkbox column it should toggle the check value of column. Right now it requires 2-3 clicks to toggle. Any comments are very appreciated. Thanks Ruchi

    H 1 Reply Last reply
    0
    • R Ruchi Gupta

      Hi All, I am stuck at some point in implementing checkbox column in datagrid. This is how I am adding checkbox column to the DataGridTableStyle. DataGridColumnStyle boolCol = new DataGridBoolColumn(); boolCol.MappingName = "PrimaryFlag"; boolCol.HeaderText = "Primary"; ((DataGridBoolColumn)boolCol).TrueValue = "true"; ((DataGridBoolColumn)boolCol).FalseValue = "false"; ((DataGridBoolColumn)boolCol).ReadOnly = false; boolCol.Width = 40; ts.GridColumnStyles.Add(boolCol); And from database I am receiving "true" or "false" values in the PrimaryFlag column. When I set the datasource of datagrid with the dataset that I receive from database, it correctly sets the checkboxes as checked or unchecked based on "true" or "false" values in the PrimaryFlag column. Now my question is how can I handle the event such that with one click on the checkbox column it should toggle the check value of column. Right now it requires 2-3 clicks to toggle. Any comments are very appreciated. Thanks Ruchi

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      First, declare your variable as a DataGridBoolColumn instead of a DataGridColumnStyle. You can then avoid expensive cast operations and can still add it to the DataGridTableStyle.GridColumnStyles collection property because it's still a DataGridColumnStyle object (from which it inherits). Set AllowNull to false (the default) and make sure that none of your rows contains a NULL value for that column. If they do, the DataGridBoolColumn will automatically draw a tri-state combo box. In most cases with boolean columns in a database it's best to define a default and not allow nulls anyway - it saves you from having to check for null values since - when converting to a bool - value types cannot be null.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups