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:
Just modify the code a bit to suit to your sql

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] DataSet1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataSet = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/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] = "Emp_Profile"[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] initMethod()[/SIZE]
[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] = "SELECT emp_no, emp_name, sel FROM Emp_Profile ORDER BY emp_no"[/SIZE]
[SIZE=2]generateTbl()[/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]DAdapter1.Fill(DataSet1, m__C_TBL_ITEMS)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] generateTbl()[/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(m__C_TBL_ITEMS).Clear()[/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/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("Selected", [/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] colemp_no [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("emp_no", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]colemp_no.DefaultValue = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty[/SIZE]
[SIZE=2]dTable.Columns.Add(colemp_no)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] colemp_name [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("emp_name", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]colemp_name.DefaultValue = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty[/SIZE]
[SIZE=2]dTable.Columns.Add(colemp_name)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] colsel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("sel", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]colsel.DefaultValue = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty[/SIZE]
[SIZE=2]dTable.Columns.Add(colsel)[/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][COLOR=#0000ff]Me[/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]
[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 = "Selected"[/SIZE]
[SIZE=2]boolCol.HeaderText = "Selected"[/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 = "emp_no"[/SIZE]
[SIZE=2]TextCol.HeaderText = "emp no"[/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 = "emp_name"[/SIZE]
[SIZE=2]TextCol.HeaderText = "name"[/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]TextCol = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn[/SIZE]
[SIZE=2]TextCol.MappingName = "sel"[/SIZE]
[SIZE=2]TextCol.HeaderText = "sel"[/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][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Emp_Grid.TableStyles.Add(dgTableStyle)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

Anythings just post here.
 
Thanks, it works fine but there is one minor problem on combobox, when i execute the application, the combobox default state is "grey color" and it seems there are three states for first time clicking, how to get rid of it and set to false value to display all of them in "non ticked on" state?
 
If you use my code above, then you will find I am trying to set the checkbox column style.

VB.NET:
[COLOR=#0000ff]Dim[/COLOR][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 = "Selected"[/SIZE]
[SIZE=2]boolCol.HeaderText = "Selected"[/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]

The checkbox display "grey color" is because you allow it to have null value state. You will have 2 states only if you add the proper table style
 
I want to ask something extra here. what if i want to store the true of false in database? What should i do to get rid of the "grey state"? Now the application works according to what i want, just true or false but if i create another one column called "sel" and execute it. The "grey state" will be there"? Any idea on it? Thank you.

May i also know how to implement an idea like this, when i click on the button, it will check the true of false of each and every row, for the true row, it will do action, for the false row, it will do another action? Please note that the true or false of each row isn't stored in the database so we cannot do the check using database SQL statement.
 
Last edited:
Just add a GridColumnStyles to the "sel" column to get rid of the grey state, eg:

VB.NET:
boolCol = [SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridBoolColumn[/SIZE]
[SIZE=2]boolCol.MappingName = "sel"[/SIZE]
[SIZE=2]boolCol.HeaderText = "sel"[/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]

For question 2, use this kind of code to loop through your dataset:
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] row [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] DataSet1.Tables(m__C_TBL_ITEMS).Rows
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] row.Item("Selected") [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' selected
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' not selected
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
 
Hi, lingsn, need to bother you again, this time i am dealing with combobox and the following code doesn't work, may i know why?
VB.NET:
Dim TextCol As DataGridTextBoxColumn
TextCol = New DataGridTextBoxColumn
combobox123.BringToFront()
combobox123.Visible = True
TextCol.TextBox.Controls.Add(combobox123)
TextCol.HeaderText = "Combo"
TextCol.Width = 100
TextCol.ReadOnly = True
What about the hyperlink column? Thank you
 
Last edited by a moderator:
I don't think datagrid got the combobox features, unless you are using the DataGridView. To enable the combobox in datagrid, you can try to google the internet to download the source for combobox that was inherited from DataGridTextBoxColumn.

For hyperlink, it is possible to do in the ASP.NET datagrid
http://www.vbdotnetforums.com/showthread.php?t=16181
 
I have got this following code from internet, but from what i can understand, the content of the combobox is retrieved from one column of database and however i dont need database for the content, may i know how to code the content for the combobox.

Another problem is can you guys show me how to add TextBoxColumn and make it consistent with the following code? Thank you.

VB.NET:
[COLOR=#000066][FONT=Courier New][FONT=Verdana]With DataGridView1  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana] 
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]' Set DataGridView Combo Column for CarID field   
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]Dim ColumnCar As New DataGridViewComboColumn  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]' DataGridView Combo ValueMember field has name "CarID"  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]' DataGridView Combo DisplayMember field has name "Car"  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]With ColumnCar  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]    .DataPropertyName = "CarID"  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]    .HeaderText = "Car Name"  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]    .Width = 80  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]    ' Bind ColumnCar to Cars table  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]    .box.DataSource = ds.Tables("Cars")  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]    .box.ValueMember = "CarID"  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]    .box.DisplayMember = "Car"  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]End With  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana].Columns.Add(ColumnCar)  
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana] 
[/FONT][/FONT][/COLOR][COLOR=#000066][FONT=Courier New][FONT=Verdana]End With  [/FONT][/FONT][/COLOR]
 
Back
Top