Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to fill a column with a variable that is decided if a checkbox.checked = true.
I have added 3 columns, AddCol1 is Shift (To be filled with the variable, and AddCol2 and AddCol3 to be user entered information.

I want to fill AddCol1 column with the declared shift variable, and I am thinking I need to do a Cells.Value but cannot get the syntax correct. The datagridview has about 100 rows
Here is the sub I am working on.
VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim sql As String
    Dim datepicked As Date
    datepicked = DateTimePicker1.Text
    'Checkbox Check Loop
    If CheckBox1.CheckState = False And CheckBox2.CheckState = False And CheckBox3.CheckState = False Then
        MessageBox.Show("Please select a Shift")
        Exit Sub
    End If
    If CheckBox1.Checked = True And CheckBox2.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If
    If CheckBox2.Checked = True And CheckBox3.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If
    If CheckBox1.Checked = True And CheckBox3.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If

    'Selection of Shift
    Dim shift As Integer
    If CheckBox1.Checked = True Then
        sql = "SELECT MACHINENUMBER, STYLEWIDTH FROM MachineSet WHERE RunFlag1 = 1"
        shift = 1
    ElseIf CheckBox2.Checked = True Then
        sql = "SELECT MACHINENUMBER, STYLEWIDTH FROM MachineSet WHERE RunFlag2 = 1"
        shift = 2
    ElseIf CheckBox3.Checked = True Then
        sql = "SELECT MACHINENUMBER, STYLEWIDTH FROM MachineSet WHERE RunFlag3 = 1"
        shift = 3
    End If

    'SQL Data connection
    Dim sqlCon1 As New SqlConnection("Data Source=SERVER1\DEV01;database=Production;uid=sa;pwd=passwordhere")
    Dim daSWC1 As New SqlDataAdapter(sql, sqlCon1)
    Dim SQLcmdBuilder1 As New SqlCommandBuilder(daSWC1)
    Dim ds1 As New DataSet

    'Adds the columns Picks, Runtime and Date.
    Dim AddCol1 As New DataGridViewTextBoxColumn
    Dim AddCol2 As New DataGridViewTextBoxColumn
    Dim AddCol3 As New DataGridViewTextBoxColumn

    AddCol1.DataPropertyName = "Shift"
    AddCol1.HeaderText = "Shift"
    AddCol1.Name = "Shift"
    AddCol2.DataPropertyName = "Picks"
    AddCol2.HeaderText = "PICKS"
    AddCol2.Name = "PICKS"
    AddCol3.DataPropertyName = "RunTime"
    AddCol3.HeaderText = "RunTime"
    AddCol3.Name = "RunTime"


    daSWC1.Fill(ds1, "MACHINENUMBER")
    DataGridView1.DataSource = ds1.Tables(0)
    DataGridView1.Columns.Add(AddCol1)' need to autofill with shift variable here
    DataGridView1.Columns.Add(AddCol2)
    DataGridView1.Columns.Add(AddCol3)

End Sub


The plan is to write/append this information to a different table.
Posted

You are adding the columns to your DataGridView1. You could instead add DataColumns to your DataTable object, if you want to. Either way, you need to loop through each DataRow in the DataTable or each DataGridViewRow in the DataGridView to set your values.

if you go the DataTable route:
VB
For Each row as DataRow in ds1.Tables(0).Rows
    If Checkbox1.Checked Then
        row("Shift") = "Value if Checked"
    Else
        row("Shift") = "Value if Not Checked"
    End If
Next


or if you stay with the DataGridView
VB
For Each dgvr as DataGridViewRow In DataGridView1.Rows
    If Checkbox1.Checked Then
        dgvr.Cells("Shift").Value = "Value if Checked"
    Else
        dgvr.Cells("Shift").Value = "Value if Not Checked"
    End If
Next
 
Share this answer
 
Instead of DataGridViewTextBoxCoumn, use DataGridViewCheckBoxColumn[^], but...

DataGridView can create columns automatically, if you use DataTable and bind this DataTable data to DataGridView (DGV).
When your data contains field with bit data type (stores only o and 1), it should display CheckBox in DGV column.

For further information, please see:
Basics to advance working with DataGridView Checkbox columns[^]
 
Share this answer
 
This is what worked for me. Got rid of the shift declaration. Thanks for the help.!!!
VB
daSWC1.Fill(ds1, "MACHINENUMBER")
DataGridView1.DataSource = ds1.Tables(0)
DataGridView1.Columns.Add(AddCol1)
For Each dgvr As DataGridViewRow In DataGridView1.Rows
    If CheckBox1.Checked Then
        dgvr.Cells("Shift").Value = 1
    ElseIf CheckBox2.Checked Then
        dgvr.Cells("Shift").Value = 2
    ElseIf CheckBox3.Checked Then
        dgvr.Cells("Shift").Value = 3
    End If
Next
DataGridView1.Columns.Add(AddCol2)
DataGridView1.Columns.Add(AddCol3)
 
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