Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to allow specific checkbox's if checked for the data to be merged and to be inserted into the datagridview single column.

The form is adding column as follows;
table.Columns.Add("Drinks", Type.GetType("System.Int32"))

Then

Checkbox 1 = Coke
Checkbox 2 = Pepsi Checkbox 3 = Fanta

Public Class has the following code;

Dim Drinks As String

Each of the checkbox has the following codes along with their text;

Drinks = "Coke"
The button to generate the information is as follows;

table.Rows.Add(Drinks.ToString)
   DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
   DataGridView1.RowTemplate.Height = 100
   DataGridView1.AllowUserToAddRows = False
   DataGridView1.DataSource = table


Therefore if someone selects 'Checkbox1 & Checkbox2' how can i get datagridview coloum 'Drinks' to show coke & pepsi?

What I have tried:

N/A i haven't tried anything as i can't find similar thread
Posted
Updated 23-Apr-17 12:31pm
Comments
Kornfeld Eliyahu Peter 23-Apr-17 9:22am    
While it is fairly unclear...It seems to me that in any case your code overrides previous values when user checks a checkbox...
When user checks the box for Coke, Drinks will be set to 'Coke'; when user checks the box for Pepsi, Drinks will be set to 'Pepsi' and 'Coke' is gone...
It maybe useful too, to think about an enum for those checkbox (a bit valued enum maybe)...

1 solution

Note that your column can accept only integer value, because of this line:
VB.NET
table.Columns.Add("Drinks", Type.GetType("System.Int32"))

And you're trying to add string (which is wrong!):
VB.NET
Dim Drinks As String
Drinks = "Coke"
table.Rows.Add(Drinks.ToString) 'Note: ToString() method is redundant


If you want to add integer value, you have to check if Checkbox is Checked[^], then to add a row into datatable.

Assuming that each drink has its numeric value, you can use something like this:
VB.NET
'Coke = 1
If CheckBox1.Checked Then table.Rows.Add(New Object(){1})
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900