Extra Column In DataGrid

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
I know that we can use DataGrid to display the table data but what i want to do is not only display the data in DataGrid but also create another new column which it is used to place the radio box, each row has its row checkbox next to it, for example if the radio box is ticked on for that particular row, something action will be triggered later based on a button. May i know how to do it and should i user datagrid or datagridview? Thank you.
 
Last edited:
Based on my research, i have to make use of template column in order to include checkbox for each row. Anyone can show me the sample coding of this as well as for the combobox one? Apart from that, how to user a button to trigger action by checking the true or false of checkbox? Thank you.
 
I have successfully done the checkbox part but i have one problem. Is it possible for me to add the checkbox row without creating the row in the database because i can't see there is any point for me to store it since it only acts as intermediate process which help to lead to output? Thank you.
 
DataGridViewCheckBoxColumn Problem

Below is the code for displaying the data with datagrid and i need to another row with checkbox but i got some problem on the code. Please check the underline statement, may i know how to solve it? Besides that, may i know how to use a button to save the true or false for each row and how if i want to trigger action once the user click on the checkbox? Thank you.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SQLSTRING1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"SELECT emp_no, emp_name, sel FROM Emp_Profile ORDER BY emp_no"[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DAdapter1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDbDataAdapter = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbDataAdapter(SQLSTRING1, OleDbConnection1)[/SIZE]
[SIZE=2]DataSet1.Clear()[/SIZE]
[SIZE=2]DAdapter1.Fill(DataSet1, [/SIZE][SIZE=2][COLOR=#800000]"Emp_Profile"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#008000]'Set the DataGrid properties to bind it to our data[/COLOR][/SIZE]
[SIZE=2]Emp_Grid.DataSource = DataSet1[/SIZE]
[SIZE=2]Emp_Grid.DataMember = [/SIZE][SIZE=2][COLOR=#800000]"Emp_Profile"[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Declare objects for the DataGrid[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objDataGridTableStyle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTableStyle()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objTextCol [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn()[/SIZE]
[SIZE=2][COLOR=#008000]'set the AlternatingBackColor property[/COLOR][/SIZE]
[SIZE=2]objDataGridTableStyle.AlternatingBackColor = Color.WhiteSmoke[/SIZE]
[SIZE=2][COLOR=#008000]'Set the AlternatingBackColor property[/COLOR][/SIZE]
[SIZE=2]objDataGridTableStyle.MappingName = [/SIZE][SIZE=2][COLOR=#800000]"Emp_Profile"[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Set the MappingName for the first column[/COLOR][/SIZE]
[SIZE=2]objTextCol.MappingName = [/SIZE][SIZE=2][COLOR=#800000]"emp_no"[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Set the new HeaderText[/COLOR][/SIZE]
[SIZE=2]objTextCol.HeaderText = [/SIZE][SIZE=2][COLOR=#800000]"Emp. No."[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Set the width of the column[/COLOR][/SIZE]
[SIZE=2]objTextCol.Width = 60[/SIZE]
[SIZE=2][COLOR=#008000]'Add the column to the DataGridTableStyle[/COLOR][/SIZE]
[SIZE=2]objDataGridTableStyle.GridColumnStyles.Add(objTextCol)[/SIZE]
 
[SIZE=2][COLOR=#008000]'Get a new reference to the DataGridTextBoxColumn[/COLOR][/SIZE]
[SIZE=2]objTextCol = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn()[/SIZE]
[SIZE=2][COLOR=#008000]'Set the MappingName for the second column[/COLOR][/SIZE]
[SIZE=2]objTextCol.MappingName = [/SIZE][SIZE=2][COLOR=#800000]"emp_name"[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Set the new HeaderText[/COLOR][/SIZE]
[SIZE=2]objTextCol.HeaderText = [/SIZE][SIZE=2][COLOR=#800000]"Employee Name"[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Set the width of the column[/COLOR][/SIZE]
[SIZE=2]objTextCol.Width = 110[/SIZE]
[SIZE=2][COLOR=#008000]'Add the column to the DataGridTableStyle[/COLOR][/SIZE]
[SIZE=2]objDataGridTableStyle.GridColumnStyles.Add(objTextCol)[/SIZE]
 
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DataGridCheckBoxCol [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridViewCheckBoxColumn[/SIZE]
[SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] DataGridCheckBoxCol[/SIZE]
[SIZE=2].HeaderText = [/SIZE][SIZE=2][COLOR=#800000]"Select"[/COLOR][/SIZE]
[SIZE=2].Name = [/SIZE][SIZE=2][COLOR=#800000]"sel"[/COLOR][/SIZE]
[SIZE=2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells[/SIZE]
[SIZE=2].FlatStyle = FlatStyle.Standard[/SIZE]
[SIZE=2].CellTemplate = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridViewCheckBoxCell[/SIZE]
[SIZE=2].CellTemplate.Style.BackColor = Color.Beige[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
[SIZE=2][U]Emp_Grid.Columns[/U].Insert(0, DataGridCheckBoxCol)[/SIZE]
 
Last edited:
Question 1
May i know how to do it, can you show me some sample code. Create in the datatable but not in the database?

Question 2
Now my checkbox got three states, one is disabled type and it is in grey color, the other two is true or false and when i click on the row checkbox, the first selected checkbox will reset to default status. To make it more simply, i can only change one row status, may i know how to solve it? It is important to me. Thank you.
 
Last edited:
This is just a sample:

VB.NET:
    Private Sub generateTbl()
        Dim dTable As DataTable
        If m_myDataSet.Tables.IndexOf(m__C_TBL_ITEMS) >= 0 Then
            m_myDataSet.Tables.Remove(m__C_TBL_ITEMS)
            Me.dgList.TableStyles.RemoveAt(0)
        End If
        dTable = m_myDataSet.Tables(m__C_TBL_ITEMS)
        dTable = New DataTable(m__C_TBL_ITEMS)
        Dim colSelected As New DataColumn("Selected", GetType(Boolean))
        colSelected.DefaultValue = False
        dTable.Columns.Add(colSelected)
        Dim colCtrlID As New DataColumn("CtrlID", GetType(String))
        colCtrlID.DefaultValue = String.Empty
        dTable.Columns.Add(colCtrlID)
        Dim colDescription As New DataColumn("Description", GetType(String))
        colDescription.DefaultValue = String.Empty
        dTable.Columns.Add(colDescription)
        m_myDataSet.Tables.Add(dTable)
        Dim dvList As DataView = New DataView(m_myDataSet.Tables(m__C_TBL_ITEMS))
        dvList.AllowDelete = False
        dvList.AllowEdit = True
        dvList.AllowNew = False
        Me.dgList.DataSource = dvList
        Dim dgTableStyle As DataGridTableStyle
        dgTableStyle = New DataGridTableStyle
        dgTableStyle.MappingName = m__C_TBL_ITEMS
        ' Set other properties.
        dgTableStyle.AlternatingBackColor = Color.LightGray
        dgTableStyle.AllowSorting = True
        Dim boolCol As DataGridBoolColumn
        boolCol = New DataGridBoolColumn
        boolCol.MappingName = "Selected"
        boolCol.HeaderText = "Selected"
        boolCol.Width = 80
        boolCol.ReadOnly = False
        boolCol.AllowNull = False
        dgTableStyle.GridColumnStyles.Add(boolCol)
        Dim TextCol As DataGridTextBoxColumn
        TextCol = New DataGridTextBoxColumn
        TextCol.MappingName = "CtrlID"
        TextCol.HeaderText = "ID"
        TextCol.Width = 100
        TextCol.ReadOnly = True
        dgTableStyle.GridColumnStyles.Add(TextCol)
        TextCol = New DataGridTextBoxColumn
        TextCol.MappingName = "Description"
        TextCol.HeaderText = "Description"
        TextCol.Width = 250
        TextCol.ReadOnly = True
        dgTableStyle.GridColumnStyles.Add(TextCol)
        Me.dgList.TableStyles.Add(dgTableStyle)
    End Sub
 
lingsn, i have made use of your code but i encountered an error like this "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index", may i know how to solve it. Thank you.
 
Below is the code i used in VB.NET, you may go through it and i need to get this part done ASAP? Thank you.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] m__C_TBL_ITEMS [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"Emp_Profile"[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DTable [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] DataSet1.Tables.IndexOf(m__C_TBL_ITEMS) >= 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]DataSet1.Tables.Remove(m__C_TBL_ITEMS)[/SIZE]
[SIZE=2]Emp_Grid.TableStyles.RemoveAt(0)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[SIZE=2]DTable = DataSet1.Tables(m__C_TBL_ITEMS)[/SIZE]
[SIZE=2]DTable = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable(m__C_TBL_ITEMS)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] colSelected [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn([/SIZE][SIZE=2][COLOR=#800000]"Selected"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]colSelected.DefaultValue = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]DTable.Columns.Add(colSelected)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] colCtrlID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn([/SIZE][SIZE=2][COLOR=#800000]"CtrlID"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]colCtrlID.DefaultValue = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty[/SIZE]
[SIZE=2]DTable.Columns.Add(colCtrlID)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] colDescription [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn([/SIZE][SIZE=2][COLOR=#800000]"Description"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]colDescription.DefaultValue = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty[/SIZE]
[SIZE=2]DTable.Columns.Add(colDescription)[/SIZE]
[SIZE=2]DataSet1.Tables.Add(DTable)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dvList [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataView = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataView(DataSet1.Tables(m__C_TBL_ITEMS))[/SIZE]
[SIZE=2]dvList.AllowDelete = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]dvList.AllowEdit = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]dvList.AllowNew = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]Emp_Grid.DataSource = dvList[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dgTableStyle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataGridTableStyle[/SIZE]
[SIZE=2]dgTableStyle = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTableStyle[/SIZE]
[SIZE=2]dgTableStyle.MappingName = m__C_TBL_ITEMS[/SIZE]
[SIZE=2][COLOR=#008000]' Set other properties.[/COLOR][/SIZE]
[SIZE=2]dgTableStyle.AlternatingBackColor = Color.LightGray[/SIZE]
[SIZE=2]dgTableStyle.AllowSorting = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] boolCol [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataGridBoolColumn[/SIZE]
[SIZE=2]boolCol = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridBoolColumn[/SIZE]
[SIZE=2]boolCol.MappingName = [/SIZE][SIZE=2][COLOR=#800000]"Selected"[/COLOR][/SIZE]
[SIZE=2]boolCol.HeaderText = [/SIZE][SIZE=2][COLOR=#800000]"Selected"[/COLOR][/SIZE]
[SIZE=2]boolCol.Width = 80[/SIZE]
[SIZE=2]boolCol.ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]boolCol.AllowNull = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]dgTableStyle.GridColumnStyles.Add(boolCol)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] TextCol [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn[/SIZE]
[SIZE=2]TextCol = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn[/SIZE]
[SIZE=2]TextCol.MappingName = [/SIZE][SIZE=2][COLOR=#800000]"CtrlID"[/COLOR][/SIZE]
[SIZE=2]TextCol.HeaderText = [/SIZE][SIZE=2][COLOR=#800000]"ID"[/COLOR][/SIZE]
[SIZE=2]TextCol.Width = 100[/SIZE]
[SIZE=2]TextCol.ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]dgTableStyle.GridColumnStyles.Add(TextCol)[/SIZE]
[SIZE=2]TextCol = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn[/SIZE]
[SIZE=2]TextCol.MappingName = [/SIZE][SIZE=2][COLOR=#800000]"Description"[/COLOR][/SIZE]
[SIZE=2]TextCol.HeaderText = [/SIZE][SIZE=2][COLOR=#800000]"Description"[/COLOR][/SIZE]
[SIZE=2]TextCol.Width = 250[/SIZE]
[SIZE=2]TextCol.ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]dgTableStyle.GridColumnStyles.Add(TextCol)[/SIZE]
[SIZE=2]Emp_Grid.TableStyles.Add(dgTableStyle)[/SIZE]
 
Yes but where is the error occurring? To find out you can place a breakpoint at the beginning and step through.
Most likely the error is at the line
VB.NET:
Emp_Grid.TableStyles.RemoveAt(0)
Do you have a TableStyle in the grid?
 
Now no problem, but may i know the meaning of the following code? Thank you.


VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] DataSet1.Tables.IndexOf(m__C_TBL_ITEMS) >= 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]DataSet1.Tables.Remove(m__C_TBL_ITEMS)[/SIZE]
[SIZE=2]Emp_Grid.TableStyles.RemoveAt(0)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
There are three columns with column titles displayed but it can't retrieve the column data from database for "CtrlID" and "Description". May i know how to do it? Thank you.
 
Back
Top