Window Application

UserControl Contain two combobox and Gridview Control .
When i add this control to my form and change the value of combobox selected item then the value not get into the form
But when i debug the code and reevaluate (Quick Watch it) then i get the value of combobox.selecteditem

My Code is given below :

User Control Code

Imports System.Data
Imports System.Globalization
Imports System.Threading

Public Class Daycalendar
    Dim strMonth As String() = {"January", "February", "March", "April", "May", "June", "July", "August", _
                                "September", "Octomber", "November", "December"}
    Private Sub BindMonth()

        Dim intCount As Integer = 0
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("MonthId"))
        dt.Columns.Add(New DataColumn("MonthName"))
        Dim dr As DataRow
      
        For intCount = 0 To strMonth.Length - 1
            dr = dt.NewRow()
            dr("MonthId") = (intCount + 1).ToString
            dr("MonthName") = strMonth(intCount)
            dt.Rows.Add(dr)
            dt.AcceptChanges()
            cmbMonth.Items.Insert(intCount, strMonth(intCount))


        Next
        'cmbMonth.DataSource = dt

        cmbMonth.DisplayMember = "MonthName"
        cmbMonth.ValueMember = "MonthId"
        'cmbMonth.SelectionStart = 1
        


        cmbMonth.SelectedValue = 0
        cmbMonth.SelectedIndex = 0
        cmbMonth.SelectedItem = dt.Rows(0)("MonthName").ToString
        cmbMonth.Refresh()
    End Sub

    Public Sub BindYear()
        Dim year As Integer = Today.Date.Date.Year
        Dim intCount As Integer = 0
        For intCount = year To year + 9
            cmbYear.Items.Add(intCount.ToString())

        Next


        cmbYear.SelectedValue = 0
        cmbYear.SelectedIndex = 0
        cmbYear.SelectedItem = DateTime.Now.Year
        cmbYear.Refresh()

    End Sub

    Private Sub Daycalendar_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindMonth()
        BindYear()
        lbltitle.Text = cmbMonth.Text & "," & cmbYear.Text
        lbltitle.Width = 50
        lbltitle.Height = 50
        FillCalendar()

    End Sub

    Private Sub cmbMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbMonth.SelectedIndexChanged
        Dim comBox As ComboBox = CType(sender, ComboBox)
        'MsgBox(cmbMonth.SelectedValue)
        If comBox.Items.Count > 0 AndAlso cmbYear.Items.Count > 0 AndAlso cmbYear.SelectedItem.ToString IsNot Nothing Then
            lbltitle.Text = cmbMonth.SelectedItem.ToString & "," & cmbYear.SelectedItem.ToString
            FillCalendar()
        End If
    End Sub

    Private Sub cmbYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbYear.SelectedIndexChanged
        Dim comBox As ComboBox = CType(sender, ComboBox)
       ' MsgBox(cmbYear.SelectedValue)
        If comBox.Items.Count > 0 AndAlso cmbMonth.Items.Count > 0 AndAlso cmbYear.SelectedItem.ToString IsNot Nothing Then
            lbltitle.Text = cmbMonth.SelectedItem.ToString & "," & cmbYear.SelectedItem.ToString
            FillCalendar()
        End If
    End Sub

    Private Sub FillCalendar()
        Dim dtCalendar As New DataTable
        Dim intMax As Integer = 0
        Dim blankRow As DataRow
        Try
            If cmbMonth.Text <> "" AndAlso cmbYear.Text <> "" Then

                Dim month As Integer = CInt(cmbMonth.SelectedValue) + 1
                Dim year As Integer = CInt(cmbYear.Text.Trim)
                Dim daycount As Integer = Date.DaysInMonth(year, month)
                '        MessageBox.Show(System.Globalization.CultureInfo.CurrentCulture. _
                'DateTimeFormat.ShortDatePattern())
                Dim culture As CultureInfo = New CultureInfo("en-US")
                Dim dtNow As DateTime = DateTime.Parse("1" & "/" & month.ToString & "/" & year.ToString, culture)
                Dim currentday = dtNow.DayOfWeek()

                Dim intMin As Integer = 1
                intMax = daycount


                dtCalendar.Columns.Add(New DataColumn("Monday"))
                dtCalendar.Columns.Add(New DataColumn("Tuesday"))
                dtCalendar.Columns.Add(New DataColumn("Wednesday"))
                dtCalendar.Columns.Add(New DataColumn("Thursday"))
                dtCalendar.Columns.Add(New DataColumn("Friday"))
                dtCalendar.Columns.Add(New DataColumn("Saturday"))
                dtCalendar.Columns.Add(New DataColumn("Sunday"))

                Dim dr As DataRow
                dr = dtCalendar.NewRow()

                While intMin <= intMax

                    Dim columnIndex As Integer
                    columnIndex = dtCalendar.Columns.IndexOf(dtCalendar.Columns(dtNow.DayOfWeek.ToString))

                    Select Case currentday
                        Case DayOfWeek.Monday
                            dr("Monday") = dtNow.Day.ToString

                        Case DayOfWeek.Tuesday
                            dr("Tuesday") = dtNow.Day.ToString

                        Case DayOfWeek.Wednesday
                            dr("Wednesday") = dtNow.Day.ToString

                        Case DayOfWeek.Thursday
                            dr("Thursday") = dtNow.Day.ToString

                        Case DayOfWeek.Friday
                            dr("Friday") = dtNow.Day.ToString

                        Case DayOfWeek.Saturday
                            dr("Saturday") = dtNow.Day.ToString

                        Case DayOfWeek.Sunday
                            dr("Sunday") = dtNow.Day.ToString

                    End Select
                    If columnIndex = 6 Then
                        dtCalendar.Rows.Add(dr)
                        dr = dtCalendar.NewRow()
                        blankRow = dtCalendar.NewRow()
                        dtCalendar.Rows.Add(blankRow)
                    End If
                    intMin = intMin + 1

                    dtNow = dtNow.Date.AddDays(1)
                    currentday = dtNow.DayOfWeek()
                End While
                If dr.Item(0).ToString IsNot "" Then
                    dtCalendar.Rows.Add(dr)
                    dtCalendar.AcceptChanges()

                End If
                'blankRow = dtCalendar.NewRow()
                'dtCalendar.Rows.Add(blankRow)
                'Add previous days to the calendar
                Dim dtCurrent As DateTime = DateTime.Parse("1" & "/" & month.ToString & "/" & year.ToString, culture)
                Dim fColumnIndex As Integer = dtCalendar.Columns.IndexOf(dtCalendar.Columns(dtCurrent.DayOfWeek.ToString)) - 1
                Dim previousMonth = dtCurrent.Date.AddMonths(-1)
                Dim intPrevousDays As Integer = Date.DaysInMonth(previousMonth.Year, previousMonth.Month)
                Dim currentPreviousday = intPrevousDays - fColumnIndex
                Dim intDays = intPrevousDays
                dr = dtCalendar.Rows(0)
                While currentPreviousday <= intPrevousDays AndAlso fColumnIndex >= 0
                    '  dtCalendar.Rows(0).SetModified()
                    dr(fColumnIndex) = currentPreviousday
                    'dtCalendar.Rows(0)
                    dr(fColumnIndex) = intDays
                    'dtCalendar.AcceptChanges()
                    fColumnIndex = fColumnIndex - 1
                    currentPreviousday = currentPreviousday + 1
                    intDays = intDays - 1
                End While


                'Add next month days to the calendar
                'Dim column As DataColumn = dtCalendar.Columns
                If dtCalendar IsNot Nothing AndAlso dtCalendar.Rows.Count > 0 AndAlso dtCalendar.Rows(dtCalendar.Rows.Count - 2)(dtCalendar.Columns.Count - 1).ToString = "" Then
                    dtCalendar.AcceptChanges()
                    Dim count As Integer = dtCalendar.Rows.Count - 1
                    Dim intCount As Integer = 0
                    Dim value As String = String.Empty
                    Dim intDay As Integer = 0

                    For intc = 0 To dtCalendar.Columns.Count - 1
                        value = (dtCalendar.Rows(dtCalendar.Rows.Count - 1)(intc).ToString())
                        If value Is "" Then
                            dtCalendar.Rows(dtCalendar.Rows.Count - 1)(intc) = CInt(intDay + 1).ToString
                            intDay = intDay + 1
                        End If
                    Next
                End If
                blankRow = dtCalendar.NewRow()
                dtCalendar.Rows.Add(blankRow)
                dgvCalendar.DataSource = dtCalendar
                dgvCalendar.Refresh()
                FornatGridview()
                dgvCalendar.Refresh()

            End If

        Catch ex As Exception
            Throw ex
        Finally
            dtCalendar = Nothing
        End Try
    End Sub

    Private Sub FornatGridview()
        Dim intCount As Integer = 0
        dgvCalendar.Rows(0).Height = 15
        dgvCalendar.Rows(0).ReadOnly = True
        dgvCalendar.Rows(0).Selected = False
        dgvCalendar.Rows(0).DefaultCellStyle.BackColor = Color.SkyBlue
        dgvCalendar.Rows(0).DefaultCellStyle.ForeColor = Color.Black
        dgvCalendar.Rows(0).DefaultCellStyle.SelectionBackColor = Color.SkyBlue
        dgvCalendar.Rows(0).DefaultCellStyle.SelectionForeColor = Color.Black
        dgvCalendar.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
        dgvCalendar.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
        dgvCalendar.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
        dgvCalendar.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
        dgvCalendar.Columns(4).SortMode = DataGridViewColumnSortMode.NotSortable
        dgvCalendar.Columns(5).SortMode = DataGridViewColumnSortMode.NotSortable
        dgvCalendar.Columns(6).SortMode = DataGridViewColumnSortMode.NotSortable
        For intCount = 1 To dgvCalendar.RowCount - 1
            If intCount Mod 2 <> 0 Then
                dgvCalendar.Rows(intCount).Height = 65
                dgvCalendar.Rows(intCount).DefaultCellStyle.SelectionBackColor = Color.SkyBlue
                dgvCalendar.Rows(intCount).DefaultCellStyle.SelectionForeColor = Color.Black
                dgvCalendar.Rows(intCount).ReadOnly = True
                dgvCalendar.Rows(intCount).Selected = False
            Else
                dgvCalendar.Rows(intCount).Height = 15
                dgvCalendar.Rows(intCount).ReadOnly = True
                dgvCalendar.Rows(intCount).DefaultCellStyle.BackColor = Color.SkyBlue
                dgvCalendar.Rows(intCount).DefaultCellStyle.ForeColor = Color.Black
                dgvCalendar.Rows(intCount).DefaultCellStyle.SelectionBackColor = Color.SkyBlue
                dgvCalendar.Rows(intCount).DefaultCellStyle.SelectionForeColor = Color.Black
                'dgvCalendar.Rows(intCount).DefaultCellStyle.
            End If

        Next
        dgvCalendar.Refresh()

    End Sub

    Private Sub cmbMonth_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbMonth.SelectedValueChanged
        Dim comBox As ComboBox = CType(sender, ComboBox)
        'cmbMonth.SelectedValue = comBox.SelectedValue
    End Sub

    Private Sub cmbYear_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbYear.SelectedValueChanged

    End Sub

    Private Sub cmbYear_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbYear.TextChanged
        ' cmbYear.Text = e.ToString
    End Sub

    Private Sub cmbMonth_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbMonth.SelectionChangeCommitted

    End Sub
End Class

----------------------------------------------------------------------------------------

MyForm.cs

'Option Explicit On
'Option Strict On
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Linq
Imports AccountApp.Daycalendar
Imports System.Globalization

Public Class frmSchedule
    Dim dalSchedule As DALSchedularMaster
    Dim balSchedule As BALSchedulerMaster
    Dim caledar As New Daycalendar
    Dim objGlobal As clsMain

    Private Sub frmPayee_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Height = FrmMain.Height
        Me.Width = FrmMain.Width
        'Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        'Me.WindowState = FormWindowState.Maximized
        pnlLeft.Height = Me.Height
        pnlLeft.Width = CInt((Me.Width * 0.2))
        pnlRight.Width = CInt((Me.Width * 0.78))
        pnlRight.Height = CInt(Me.Height * 0.85)
        tb.Height = CInt(Me.Height * 0.85)
        tb.Width = CInt((Me.Width * 0.78))
        ' calScheduler.Height = CInt(Me.Height * 0.84)
        'calScheduler.Width = CInt((Me.Width * 0.78))
        dgvSchedule.Height = CInt(Me.Height * 0.75)
        dgvSchedule.Width = CInt((Me.Width * 0.75))


 Dim caledar As New Daycalendar
        caledar.Name = "MyCal"
        tbCalendar.Controls.Add(caledar)
        'bind scheduler
        BindScheduler()
        'bind calendar
        BindCalendar()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        dalSchedule = New DALSchedularMaster
        Dim strScheduleId As String = String.Empty
        If dgvSchedule.SelectedRows(0).Cells(0).Value IsNot Nothing Then
            strScheduleId = dgvSchedule.SelectedRows(0).Cells("ScheduleId").Value.ToString
            If MsgBox("Are you sure to delete record ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                dalSchedule.DeleteSchedulerMaster(strScheduleId)
            End If
            BindScheduler()
        End If
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        BillDeposit.ShowDialog()
    End Sub


    Public Sub BindScheduler()
        Dim ds As New DBAccount
        Dim dalScheduler As New DALSchedularMaster
        Dim dalAccount As New DALAccountMaster
        Dim dalPayee As New DALPayeeMaster
        Dim dalCategory As New DALCategoryMaster
        Dim clsGlobal As New clsMain

        Dim dtScheduler As DataTable = dalScheduler.SelectSchedulerMaster()
        Dim dtAccount As DataTable = dalAccount.SelectAccountMaster
        Dim dtPayee As DataTable = dalPayee.SelectPayeeMaster()
        Dim dtCategory As DataTable = dalCategory.SelectCategoryMaster()
        Dim dtFrequency As DataTable = clsGlobal.BindFrequency()

        Dim query = From category In dtCategory.AsEnumerable _
                    Join scheduler In dtScheduler.AsEnumerable On scheduler.Field(Of String)("CategoryId") Equals _
                    category.Field(Of String)("CategoryMasterId") _
                     Join account In dtAccount On account.Field(Of String)("AccountMasterId") Equals _
                    scheduler.Field(Of String)("AccountMasterId") Join payee In dtPayee.AsEnumerable _
                    On scheduler.Field(Of String)("PayeeMasterId") Equals payee.Field(Of String)("PayeeMasterId") _
                    Join Frequency In dtFrequency.AsEnumerable On Frequency.Field(Of String)("FrequencyId") Equals _
                    scheduler.Field(Of String)("Frequency") _
                    Where scheduler.Field(Of String)("PayeeMasterId") IsNot "" _
                    Select New With {.AccountName = account.Field(Of String)("Name"), .PayeeName = payee.Field(Of String)("Name"), _
                                    .NextDate = scheduler.Field(Of String)("NextDate"), .Frequency = Frequency.Field(Of String)("Frequency"), _
                                    .Transaction = scheduler.Field(Of String)("Transaction"), .CategoryName = category.Field(Of String)("Name"), _
                                    .AutoPost = scheduler.Field(Of String)("IsAuto"), .Amount = scheduler.Field(Of String)("Amount"), .ScheduleId = scheduler.Field(Of String)("SchedulerMasterId")}

        Dim query1 = From category In dtCategory.AsEnumerable _
                          Join scheduler In dtScheduler.AsEnumerable On scheduler.Field(Of String)("CategoryId") Equals _
                          category.Field(Of String)("CategoryMasterId") _
                           Join account In dtAccount On account.Field(Of String)("AccountMasterId") Equals _
                          scheduler.Field(Of String)("AccountMasterId") _
                          Join Frequency In dtFrequency.AsEnumerable On Frequency.Field(Of String)("FrequencyId") Equals _
                          scheduler.Field(Of String)("Frequency") _
                          Where scheduler.Field(Of String)("PayeeMasterId") Is "" _
                           Select New With {.AccountName = account.Field(Of String)("Name"), _
                                          .NextDate = scheduler.Field(Of String)("NextDate"), .Frequency = Frequency.Field(Of String)("Frequency"), _
                                          .Transaction = scheduler.Field(Of String)("Transaction"), .CategoryName = category.Field(Of String)("Name"), _
                                          .AutoPost = scheduler.Field(Of String)("IsAuto"), .Amount = scheduler.Field(Of String)("Amount"), .ScheduleId = scheduler.Field(Of String)("SchedulerMasterId")}
        Dim intCount As Integer = 0
        Dim tempdt As New DataTable
        tempdt.Columns.Add(New DataColumn("NextDate"))
        tempdt.Columns.Add(New DataColumn("Frequency"))
        tempdt.Columns.Add(New DataColumn("Transaction"))
        tempdt.Columns.Add(New DataColumn("PayeeName"))
        tempdt.Columns.Add(New DataColumn("CategoryName"))
        tempdt.Columns.Add(New DataColumn("IsAuto"))
        tempdt.Columns.Add(New DataColumn("Amount"))
        tempdt.Columns.Add(New DataColumn("SchedulerMasterId"))
        For Each result In query
            tempdt.Rows.Add(New Object() {result.NextDate, result.Frequency, result.Transaction, result.PayeeName, result.CategoryName, result.AutoPost, result.Amount, result.ScheduleId})
        Next
        For Each result In query1
            tempdt.Rows.Add(New Object() {result.NextDate, result.Frequency, result.Transaction, "", result.CategoryName, result.AutoPost, result.Amount, result.ScheduleId})
        Next
        dgvSchedule.Columns("ScheduleId").DataPropertyName = "SchedulerMasterId"
        dgvSchedule.Columns("Frequency").DataPropertyName = "Frequency"
        dgvSchedule.Columns("Transaction").DataPropertyName = "Transaction"
        dgvSchedule.Columns("NextDate").DataPropertyName = "NextDate"
        dgvSchedule.Columns("Category").DataPropertyName = "CategoryName"
        dgvSchedule.Columns("PayeeName").DataPropertyName = "PayeeName"
        dgvSchedule.Columns("ScheduleAutopost").DataPropertyName = "IsAuto"
        dgvSchedule.Columns("Amount").DataPropertyName = "Amount"
        dgvSchedule.DataSource = tempdt
        'dgvSchedule.Height = CInt(Me.Height * 0.85)
        'dgvSchedule.Width = CInt((Me.Width * 0.78))
        dgvSchedule.Columns("NextDate").Width = CInt((dgvSchedule.Width * 0.15))
        dgvSchedule.Columns("Frequency").Width = CInt((dgvSchedule.Width * 0.2))
        dgvSchedule.Columns("Transaction").Width = CInt((dgvSchedule.Width * 0.25))
        dgvSchedule.Columns("PayeeName").Width = CInt((dgvSchedule.Width * 0.2))
        dgvSchedule.Columns("Category").Width = CInt((dgvSchedule.Width * 0.14))
        dgvSchedule.Columns("ScheduleAutoPost").Width = CInt((dgvSchedule.Width * 0.14))
        dgvSchedule.Columns("Amount").Width = CInt((dgvSchedule.Width * 0.14))
    End Sub


    Public Sub BindCalendar()

        objGlobal = New clsMain
        'AddHandler tbCalendar.Controls("cmbMonth")
        tbCalendar.Controls("MyCal").Show()
        caledar.Height = CInt(Me.Height * 0.85)
        caledar.Width = CInt((Me.Width * 0.75))
        caledar.dgvCalendar.Height = CInt(Me.Height * 0.8)
        caledar.dgvCalendar.Width = CInt((Me.Width * 0.72))
        caledar.Update()
        caledar.dgvCalendar.Refresh()
        'Bind Calendar data to by passing selected date
        Dim culture As CultureInfo = New CultureInfo("en-US")
        'caledar = New Daycalendar
        caledar.Refresh()
        Dim cmbMonth As ComboBox = caledar.cmbMonth
        Dim cmbYear As ComboBox = caledar.cmbYear

        If cmbMonth.SelectedItem IsNot Nothing AndAlso cmbYear.SelectedItem IsNot Nothing Then

            Dim selDate = DateTime.Parse("01" & "/" & cmbMonth.SelectedItem.ToString() & "/" & cmbYear.SelectedItem.ToString, culture)
            Dim strMonth = objGlobal.GetMonthValue(cmbMonth.SelectedItem.ToString())
            dalSchedule = New DALSchedularMaster()
            Dim query = From dtscheduler In dalSchedule.SelectSchedulerMaster _
                       Select New With {.NextDate = dtscheduler.Field(Of String)("NextDate")}

            Dim dt = dalSchedule.SelectSchedulerMaster()
        End If

    End Sub

    Private Sub tb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tb.SelectedIndexChanged
        BindCalendar()

    End Sub
End Class

Edited 6 Years Ago by __avd: Added [code] tags.

I ran your code and your right it doesn't work.

I changed your cmbMonth.selectedValue to cmbMonth.SelectedIndex

Moved your valdating if statement to the top of the sub, which does nothing but clean up the code a tad.

It works. At least it changes the calendar when you change the month.

Hope this is what you needed.

Private Sub FillCalendar()
        If cmbMonth.SelectedIndex = -1 OrElse cmbYear.SelectedIndex = -1 Then Exit Sub

        Dim dtCalendar As New DataTable
        Dim intMax As Integer = 0
        Dim blankRow As DataRow
        Try

            Dim month As Integer = CInt(cmbMonth.SelectedIndex) + 1
            Dim year As Integer = CInt(cmbYear.Text.Trim)

Edited 6 Years Ago by Unhnd_Exception: n/a

Another thing.

Your bindMonth Sub where your trying to bind a row with a month and year and obtain it with the selected value can go away. Its not going to work and theres no point.

You can shorten the same thing to:

Private Sub FillMonth()
        For i = 1 To 12
            cmbMonth.Items.Add(DateAndTime.MonthName(i))
        Next
    End Sub

Edited 6 Years Ago by Unhnd_Exception: n/a

Heres an example of the display member with a combobox if the id isn't the same as the index like the month above.

If you add and object to the combobox you can set the display member to one of the properties and access it through the combobox.selecteditem property.

This doesn't work for a datarow.

Add a class with a name and id property.

Public Class NameAndID
        Private _Name As String
        Private _ID As Integer

        Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property

        Property ID() As Integer
            Get
                Return _ID
            End Get
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property

        Sub New(ByVal Name As String, ByVal ID As String)
            _Name = Name
            _ID = ID
        End Sub

    End Class



 Private Sub FillMonth()
        cmbMonth.DisplayMember = "Name"

        'fill the combobox with name and ids
        For i = 1 To 12
            cmbMonth.Items.Add(New NameAndID(DateAndTime.MonthName(i), i))
        Next

        cmbMonth.SelectedIndex = 3

        'access the properties through the selectedItem
        With CType(cmbMonth.SelectedItem, NameAndID)
            MsgBox("Name: " & .Name & "  ID: " & .ID)
        End With
    End Sub
This article has been dead for over six months. Start a new discussion instead.