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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. 2-state DataGridBoolColumn style

2-state DataGridBoolColumn style

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharptutorialquestion
4 Posts 3 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.
  • D Offline
    D Offline
    Douglas Dean
    wrote on last edited by
    #1

    Can anybody tell me how to implement a normal (2-state) checkbox for a column on my DataGrid? I have tried using every kind of .NET admonition ("AllowNull = false" on the style, on the column, etc.), but I always get a 3-state check box that at best is a hassle (extra clicking required to get where you want) and at worst causes an error if they leave it in the null state. I don't relish the prospect of watching my users' eyes glaze over as I explain to them the difference between "false" and "DBNull". Any help will be greatly appreciated! Doug

    R J 2 Replies Last reply
    0
    • D Douglas Dean

      Can anybody tell me how to implement a normal (2-state) checkbox for a column on my DataGrid? I have tried using every kind of .NET admonition ("AllowNull = false" on the style, on the column, etc.), but I always get a 3-state check box that at best is a hassle (extra clicking required to get where you want) and at worst causes an error if they leave it in the null state. I don't relish the prospect of watching my users' eyes glaze over as I explain to them the difference between "false" and "DBNull". Any help will be greatly appreciated! Doug

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Hmmm... setting AllowNull on the DataGridBoolColumn to false should work (I remember I used it once and it worked). To avoid the nullstate you could also set the NullValue property to either true or false.

      1 Reply Last reply
      0
      • D Douglas Dean

        Can anybody tell me how to implement a normal (2-state) checkbox for a column on my DataGrid? I have tried using every kind of .NET admonition ("AllowNull = false" on the style, on the column, etc.), but I always get a 3-state check box that at best is a hassle (extra clicking required to get where you want) and at worst causes an error if they leave it in the null state. I don't relish the prospect of watching my users' eyes glaze over as I explain to them the difference between "false" and "DBNull". Any help will be greatly appreciated! Doug

        J Offline
        J Offline
        Josh Smith
        wrote on last edited by
        #3

        You can set the AllowNull property on the DataGridBoolColumn to false and it will prevent the tri-state checkbox. I used a little reflection to get a reference to the internal DataGridTableStyle used by the grid (if there's a way to access that field via the public interface, please let me know how).

        private void Form1_Load(object sender, System.EventArgs e)
        {
        DataTable tbl = new DataTable();
        tbl.Columns.Add( "TwoState", typeof(bool) );
        tbl.Rows.Add( new object[] { true } );
        tbl.Rows.Add( new object[] { false } );
        this.dataGrid1.DataSource = tbl;

        DataGridTableStyle tableStyle = typeof(DataGrid).GetField( 
        	"myGridTable",
        	System.Reflection.BindingFlags.Instance | 
        	System.Reflection.BindingFlags.NonPublic).GetValue( this.dataGrid1 ) 
        	as DataGridTableStyle;
        
        (tableStyle.GridColumnStyles\[ "TwoState" \] as DataGridBoolColumn).AllowNull = false;
        

        }

        Josh

        D 1 Reply Last reply
        0
        • J Josh Smith

          You can set the AllowNull property on the DataGridBoolColumn to false and it will prevent the tri-state checkbox. I used a little reflection to get a reference to the internal DataGridTableStyle used by the grid (if there's a way to access that field via the public interface, please let me know how).

          private void Form1_Load(object sender, System.EventArgs e)
          {
          DataTable tbl = new DataTable();
          tbl.Columns.Add( "TwoState", typeof(bool) );
          tbl.Rows.Add( new object[] { true } );
          tbl.Rows.Add( new object[] { false } );
          this.dataGrid1.DataSource = tbl;

          DataGridTableStyle tableStyle = typeof(DataGrid).GetField( 
          	"myGridTable",
          	System.Reflection.BindingFlags.Instance | 
          	System.Reflection.BindingFlags.NonPublic).GetValue( this.dataGrid1 ) 
          	as DataGridTableStyle;
          
          (tableStyle.GridColumnStyles\[ "TwoState" \] as DataGridBoolColumn).AllowNull = false;
          

          }

          Josh

          D Offline
          D Offline
          Douglas Dean
          wrote on last edited by
          #4

          Thanks for all the answers, but setting the AllowNull is definitely not working wither for Column or Style. I am becoming convinced that this is because I am working with .NET Framework 1.1. :-(

          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