Datepicker Databinding Dilemma

8 posts, 1 answers
  1. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 04 Nov 2010 Link to this post

    Morning,

    I think this question is not directly Telerik related but hopefully someone can help.

    I have a datepicker and gridview databound. When I select a row on the gridview, the datepicker updates as I would expect but if I select a row with a blank date (DBNull I believe?) then it still retains the avlue from the previously picked date but still treats it as a null value.

    I have looked through all the options I can think of in Formatting and Advanced Binding but nothing seems to work. I have also looked at ways of refreshing the databinding on row change but got no where (plus I don't think that's really the correct way of doing things).

    If anyone could help or point me in the right direction i'd really appreciate it!

    Kind regards,


    Guy
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Nov 2010 Link to this post

    Hello Guy, 

    You have a gridview, and a separate datetime picker. Is that correct? And as you select different rows, you grab the date from one of the cells in the selected row and make that the date in the datetimepicker. Is that correct? 

    If this is the case, you can check to see if the current cell is null, and if so, also set the datetimepicker value to null. You can set a null default date in the date time picker also. 

    Let me know if that helps, or indeed doesn't. 
    Best regards, 
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 04 Nov 2010 Link to this post

    Hi Richard,

    Both the gridview and datepicker are bound to the same bindingsource. The datepicker exists with a few other bound controls which the user can then save.

    If the user selects a row with a blank date in it, the displayed value of the datepicker is the same as the last row selection which is confusing the users as they think thhe date will save but the value is still being stored as null.

    I obviously need a value to blank out that value if it's null. I have tried Databinding both the Value and Text of the datepicker and have set a blank space as the Null value for both. I have also set the Nulldate property of the control to a blank space too but no luck.

    Would the best course of action be to check on the rowchange to see if the currentrow has a nulldate and then set the text to "";?

    Thanks,

    Guy
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Nov 2010 Link to this post

    Hi Guy,

    Apologies if I'm not getting this straight away, but the RadDateTimePicker does not have a datasource property. How are you setting the the value of the radDateTimePicker?
    thanks
    Richard
  6. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 04 Nov 2010 Link to this post

    Hi Richard,

    I'm setting it from within the (DataBindings) property at the top of the Properties window. Then within Formatting and Advanced Binding I am setting the value to the bindingsource.

    I hope this helps.

    Guy
  7. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Nov 2010 Link to this post

    Hi Guy,

    Ah, ok, I see what you mean, though personally, i think this exampel would be easier. With this it shows the dates in the grid, and then has a null date and null text in the RadDateTimePicker.

    Let me know if that helps
    Richard
    Imports System
    Imports System.ComponentModel
      
    Public Class Form1
      
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Dim somethings As New Somethings()
                Me.RadGridView1.BeginUpdate()
                Me.RadGridView1.DataSource = New BindingList(Of Something)(somethings.GetSomethings())
                Me.RadGridView1.MasterTemplate.BestFitColumns()
                Me.RadGridView1.EndUpdate()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
      
      
        Private Sub RadGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadGridView1.SelectionChanged
            If Me.RadGridView1.CurrentRow.Cells("TheDate") IsNot Nothing Then
                Me.RadDateTimePicker1.Value = Convert.ToDateTime(Me.RadGridView1.CurrentRow.Cells("TheDate").Value)
            End If
        End Sub
    End Class
      
    Public Class Somethings
        Public Function GetSomethings() As System.Collections.Generic.List(Of Something)
            Dim somethingList As New System.Collections.Generic.List(Of Something)
            somethingList.Add(New Something("", "Blank Name Description", New DateTime(1999, 1, 3)))
            somethingList.Add(New Something("Name", "none", New DateTime(1999, 1, 2)))
            somethingList.Add(New Something("Name1", "Description1", New DateTime(1999, 1, 1)))
            somethingList.Add(New Something("Name2", "Description2", New DateTime(1999, 1, 4)))
            somethingList.Add(New Something("Name3", "Description3", New DateTime(1999, 1, 5)))
            somethingList.Add(New Something("Name4", "Description4", Nothing))
            Return somethingList
        End Function
    End Class
      
    Public Class Something
      
        Private m_Name As String
        Private m_Description As String
        Private m_Date As DateTime
      
        Public Sub New()
        End Sub
      
        Public Sub New(ByVal name As String, ByVal description As String, ByVal theDate As DateTime)
            m_Name = name
            m_Description = description
            m_Date = theDate
        End Sub
      
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = value
            End Set
        End Property
      
        Public Property Description() As String
            Get
                Return m_Description
            End Get
            Set(ByVal value As String)
                m_Description = value
            End Set
        End Property
      
        Public Property TheDate() As DateTime
            Get
                Return m_Date
            End Get
            Set(ByVal value As DateTime)
                m_Date = value
            End Set
        End Property
      
    End Class
  8. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 04 Nov 2010 Link to this post

    Hi Richard,

    Taking some ideas from your post I have come up with the following solution

    private void radGridView1_SelectionChanged(object sender, EventArgs e)
    {
        if (this.radGridView1.CurrentRow is GridViewGroupRowInfo || this.radGridView1.CurrentRow is GridViewNewRowInfo)
        {
            return;
        }
        if (this.radGridView1.CurrentRow is GridViewFilteringRowInfo || this.radGridView1.CurrentRow is GridViewFilteringRowInfo)
        {
            return;
        }
          
        if (this.radGridView1.CurrentRow.Cells["DateCalled"].Value.ToString() != "")
        {
            this.LastDateCalledPicker.Value = Convert.ToDateTime(this.radGridView1.CurrentRow.Cells["DateCalled"].Value);
            return;
        }
          
        this.LastDateCalledPicker.DateTimePickerElement.SetToNullValue();
    }


    Thanks once again for all your help!

    Best regards,

    Guy
  9. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Nov 2010 Link to this post

    Great. Glad you have it working now, Guy. 

    All the best
    Richard
Back to Top
UI for WinForms is Visual Studio 2017 Ready