Datetime picker databindings not updating the value in c# .net

Sushil Agarwal 406 Reputation points
2024-08-02T15:09:25.9033333+00:00

In an windows c#.net form for a field supp_docno which is a date type filed in grn table is bound to a dtpSupplierbill_dt.DataBindings.Add("Value", bs, "supp_docdt", true);

default value is assigned to it at the time of bindingsource.add

bindingsource endedit is also done

in before bindingsource update dtpSupplierbill_dt..value shows but that does not get updates to grn table

bs.EndEdit();

DataRowView rowView = (DataRowView)bs.Current;

rowView.BeginEdit();

unless i assign values manually as below line

rowView["supp_docdt"] = dtpDoc_dt.Value.Date;

the value in grn table supp_docno does not update

why the value is not updating if i supress this line rowView["supp_docdt"] = dtpDoc_dt.Value.Date;

can experts help me to understand the datetime picker control bidings and its table updte

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,886 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,874 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 44,841 Reputation points Microsoft Vendor
    2024-08-05T07:06:59.91+00:00

    Hi @Sushil Agarwal , Welcome to Microsoft Q&A,

    When working with data bindings in Windows Forms, particularly with a DateTimePicker control bound to a data source, it is important to understand how the data binding mechanism works and why certain values might not automatically update in the data source.

    In your scenario, it seems that the DateTimePicker control dtpSupplierbill_dt is bound to a BindingSource (bs), which in turn is connected to the grntable. The issue is that theValueproperty of theDateTimePickerdoes not update thegrn table unless you manually set the value using rowView["supp_docdt] = dtpDoc_dt.Value.Date;.

    Make sure the DataSourceUpdateMode of the Binding object is set to OnPropertyChanged.

    dtpSupplierbill_dt.DataBindings.Add("Value", bs, "supp_docdt", true, DataSourceUpdateMode.OnPropertyChanged);
    

    Before calling EndEdit, make sure the BindingSource is in the correct state. If the BindingSource is not initialized correctly or if EndEdit is called too early, changes may not be committed.

    If necessary, you can force the BindingSource to recognize the change:

    bs.EndEdit();
    DataRowView rowView = (DataRowView)bs.Current;
    rowView.BeginEdit();
    rowView["supp_docdt"] = dtpSupplierbill_dt.Value.Date;
    rowView.EndEdit();
    

    Add event handlers to the BindingSource and DateTimePicker to monitor changes and ensure they propagate correctly:

    dtpSupplierbill_dt.ValueChanged += (s, e) => { Console.WriteLine($"DateTimePicker ValueChanged: {dtpSupplierbill_dt.Value}"); };
    bs.CurrentItemChanged += (s, e) => { Console.WriteLine($"BindingSource CurrentItemChanged: {((DataRowView)bs.Current)["supp_docdt"]}"); }; 
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Karen Payne MVP 35,406 Reputation points
    2024-08-06T12:05:05.06+00:00

    Hello,

    A DateTimePicker data binding works but not all the events like list changed as they do work with a DataGridView. The code below, the BindingSource data source is set to a DataTable with mocked data. It absolutely does not matter where the data comes from as after setting the data source we are detached from where the data came from.

    The button gets the current BirthDate value of type DateTime from the current row to validate the change worked from changing the date in the DateTimePicker.

    The code works in regards to changes being properly updated which is validated by

    • Using the BindingNavigator to traverse rows after changes are made.
    • Using code in the button click event after making a change.
    using System.Data;
    
    namespace WinFormsApp1;
    public partial class DataBindingForm : Form
    {
        private BindingSource bs = new();
    
        public DataBindingForm()
        {
            InitializeComponent();
    
            bs.DataSource = new MockedOperation().Read();
            BindingNavigator1.BindingSource = bs;
            
            dateTimePicker1.DataBindings.Add(
                "Text",
                bs,
                "BirthDate",
                true,
                DataSourceUpdateMode.OnPropertyChanged, "");
    
        }
        
        private void GetCurrentButton_Click(object sender, EventArgs e)
        {
            if (bs.Current is null) return;
            // get the birthdate from the current row
            var birthDate  = ((DataRowView)bs.Current).Row.Field<DateTime>("BirthDate");
            BirthDateTextBox.Text = birthDate.ToString("M/d/yy");
        }
    }
    
    public class MockedOperation
    {
        // mimic a database read operation
        public DataTable Read()
        {
            DataTable dataTable = new DataTable();
    
            dataTable.Columns.Add("BirthDate", typeof(DateTime));
            dataTable.Columns.Add("ID", typeof(int));
    
            dataTable.Columns["ID"].AutoIncrement = true;
            dataTable.Columns["ID"].AutoIncrementSeed = 1;
            dataTable.Columns["ID"].AutoIncrementStep = 1;
    
            dataTable.Rows.Add(new DateTime(1956,5,3), null);
            dataTable.Rows.Add(new DateTime(2001, 12, 4), null);
            dataTable.Rows.Add(new DateTime(1977, 1, 22), null);
    
            return dataTable;
        }
    }
    

    Screenshot

    A1

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.