Reputation: 5971
I fill a datagridview with an data table filled by an adapter. I have some columns that are smallint. They are used as flags, like booleans. How do I display this columns as checkboxes? Notice, that I can't change the database column type to boolean.
Upvotes: 1
Views: 3304
Reputation: 2689
You just need to create an DataGridViewCheckBoxColumn
then you tell it what's false
and what's true
this.ckbCol = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.dataGridView.Columns.Add(this.active);
this.ckbCol.DataPropertyName = "ACTIVE"; //if u want to bind it to a table or something
this.ckbCol.HeaderText = "Aktiv";
this.ckbCol.Name = "Aktiv";
//Now the important stuff follows!
this.ckbCol.FalseValue = "0";
this.ckbCol.TrueValue = "1";
This works just fine for me and it's even possible to set it in the Designer!
Upvotes: 3
Reputation: 3494
You have 2 ways to do this if I remember my .Net correctly. First is the simple one don't use smallint use boolean and is will show you checkboxes by default. The second one is you have to do the gridview programatically. Use TemplateColumn and bind the data programatically in RowDataBound. her's a tutorial to help you get started http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-checkboxes-vb
Upvotes: 0
Reputation: 44595
You could use a TemplateColumn with DataBinder.Eval properly assigning the checked value to the checkbox or in the RowDataBound event handler you can check the row.DataItem and if your column is '1' you set the checkbox as checked. in the second case you get a reference to the checkbox control using (FindControl("checkboxId") as CheckBox)
Upvotes: 0