Help with adding a checkbox to a datagrid

bbelect

Member
Joined
Apr 20, 2007
Messages
14
Programming Experience
1-3
Help with adding a checkbox column to a datagrid

I have writen a database application in vb.net 2003. Have set up all the necccessary connections, dataadapters and datasets am using oledbconnection with Access 2003 database.

Now i have a datagrid displaying the information from my database now i want to add a checkbox column to the datagrid so that a user may select the checkbox to delete a record.

Any help greatly appreciated.
 
Last edited:
Resolved but new error

Well i had to add a new field in my database then set up a
DataGridTableStyle & mappingnames to the table. Example

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tableStyle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTableStyle[/SIZE]
[SIZE=2]tableStyle.MappingName = ""[/SIZE]
[SIZE=2][COLOR=#008000]'Discontinued.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] discontinuedCol [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridBoolColumn[/SIZE]
[SIZE=2]discontinuedCol.MappingName = "Discontinued"[/SIZE]
[SIZE=2]discontinuedCol.HeaderText = ""[/SIZE]
[SIZE=2]discontinuedCol.Width = 30[/SIZE]
[SIZE=2][COLOR=#008000]'turn off tristate[/COLOR][/SIZE]
[SIZE=2]discontinuedCol.AllowNull = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]tableStyle.GridColumnStyles.Add(discontinuedCol)[/SIZE]
[SIZE=2]discontinuedCol.AllowNull = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] column [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn[/SIZE]
[SIZE=2]column.MappingName = ""[/SIZE]
[SIZE=2]column.HeaderText = ""[/SIZE]
[SIZE=2]column.Width = 30[/SIZE]
[SIZE=2]tableStyle.GridColumnStyles.Add(column)[/SIZE]

Then Made the Check Box Respond to One Click in the datagrid

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] afterCurrentCellChanged [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'if the value in the Discontinued column in the current row is true, [/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'directly set the current cell to the Discontinued column[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] dataGrid1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] DataGrid.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] discontinuedColumn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] pt [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point = DataGrid.PointToClient(Control.MousePosition)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] hti [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataGrid.HitTestInfo = DataGrid.HitTest(pt)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bmb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] BindingManagerBase = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BindingContext(DataGrid.DataSource, DataGrid.DataMember)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] afterCurrentCellChanged [/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] hti.Row < bmb.Count [/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] hti.Type = DataGrid.HitTestType.Cell [/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] hti.Column = discontinuedColumn [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]DataGrid(hti.Row, discontinuedColumn) = [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CBool[/COLOR][/SIZE][SIZE=2](DataGrid(hti.Row, discontinuedColumn))[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2]afterCurrentCellChanged = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'dataGrid1_Click[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'add a line to this existing handler[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] dataGrid1_CurrentCellChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] DataGrid.CurrentCellChanged[/SIZE]
[SIZE=2][COLOR=#008000]'if click on a discontinued row, then set [/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'currentcell to checkbox[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] discontinuedColumn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] val [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = DataGrid(DataGrid.CurrentRowIndex, discontinuedColumn)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Discontinued [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CBool[/COLOR][/SIZE][SIZE=2](val)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Discontinued [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]DataGrid.CurrentCell = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridCell(DataGrid.CurrentRowIndex, discontinuedColumn)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'add this line[/COLOR][/SIZE]
[SIZE=2]afterCurrentCellChanged = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'dataGrid1_CurrentCellChanged[/COLOR][/SIZE]

But i gett an error stating "cast from type 'DBNull" to type 'Boolean' is not valid" for this line of code Dim Discontinued AsBoolean = CBool(val).

Any help??
 
Back
Top