开发者

Checkbox column in datagridView

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.


You just need to create an DataGridViewCheckBoxColumn then you tell it what's falseand 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!


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜