Add datetimepicker date to added column in Datagridview

Moordoom

Member
Joined
Nov 12, 2013
Messages
23
Programming Experience
1-3
I am trying to add a column to a DataGridView that is populated by a DateTimePicker on the form.
No errors in my code, but the column is blank at runtime.
VB.NET:
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Windows.Forms.DataGridView
Imports System.Windows.Forms.CheckBox
Imports System.Windows.Forms.DateTimePicker
Public Class Form5
    Public Sub Form5_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim CheckBox1 As New CheckBox
        Dim CheckBox2 As New CheckBox
        Dim CheckBox3 As New CheckBox
        Dim DateTimePicker1 As New DateTimePicker 'Date needed for DataGridView Column
    End Sub
    'Loads Datagrid
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim sql As String
        Dim datepicked As Date [COLOR=#008000]'Declaration of Date to pass to Added Column 3[/COLOR]
        datepicked = DateTimePicker1.Text [COLOR=#008000]'Equals date selected in Picker[/COLOR]
        '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 
        If CheckBox1.Checked = True Then
            sql = "SELECT MACHINENUMBER, STYLE FROM Production_Set WHERE RunFlag1 = 1"
        ElseIf CheckBox2.Checked = True Then
            sql = "SELECT MACHINENUMBER, STYLE FROM Production_Set WHERE RunFlag2 = 1"
        ElseIf CheckBox3.Checked = True Then
            sql = "SELECT MACHINENUMBER, STYLE FROM Production_Set WHERE RunFlag3 = 1"
        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 = "Sections"
        AddCol1.HeaderText = "Sections"
        AddCol1.Name = "Sections"
        AddCol2.DataPropertyName = "RunTime"
        AddCol2.HeaderText = "RunTime"
        AddCol2.Name = "RunTime"
        AddCol3.DataPropertyName = "Date"
        AddCol3.HeaderText = "Date"
        AddCol3.Name = "Date"
        daSWC1.Fill(ds1, "MACHINENUMBER")
        DataGridView1.DataSource = ds1.Tables(0)
        DataGridView1.Columns.Add(AddCol1) ' Column Blank for User Input
        DataGridView1.Columns.Add(AddCol2) ' Column Blank for User Input
        DataGridView1.Columns.Add(AddCol3).Equals(datepicked) [COLOR=#008000]'COLUMN WITH ISSUE[/COLOR]
    End Sub
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub
End Class

What am I missing here? I have comments next to the code that has issues.

Thanks in Advance :)
I have not got to the point where I will append this data to another (different) table.
 
You seem to be under the impression that this line:
VB.NET:
DataGridView1.Columns.Add(AddCol3).Equals(datepicked)
is going to populate that column with that value. It is doing nothing of the sort. That Equals method doesn't assign anything to anything. It compares two values and returns a Boolean to indicate whether they are equal or not. Obviously a DataGridViewColumn is never going to be equal to a Date so Equals returns False. You're not doing anything with the result though, so you don't see that specifically.

If you expect that column to be populated with that date then you have individually populate each cell with that value. If the grid is bound then that means populating the data source, otherwise you need to populate the cells directly.
 
Back
Top