DateTimePicker - Only updates bound control correctly when 'ShowCheckBox' = True

reyes

Member
Joined
Nov 11, 2016
Messages
20
Programming Experience
1-3
Hi all

I'm having a nightmare trying to get this bl**dy DateTimePicker working with DBNull values. I'm almost there, but not quite. Here's my situation.


  • I have a service-based database for testing. Within it, it has 2 tables:
    • Customers
    • Finances - Contains 'CustomerID' as a FK to the relevant column in 'Customers'.
      • This table contains the column I'm having problems with - a 'SettledDate' column that allows nulls.
  • I have a typed dataset, simply with each of these tables dropped into it - no special SQL query, just a SELECT * in each.
  • I have my binding code which is:

C#:
ds = new dsTest();

         private void button1_Click(object sender, EventArgs e)
        {
          dsTestTableAdapters.CustomersTableAdapter Cta = new dsTestTableAdapters.CustomersTableAdapter();
            dsTestTableAdapters.FinancesTableAdapter Fta = new dsTestTableAdapters.FinancesTableAdapter();


            Cta.Fill(ds.Customers);
            Fta.Fill(ds.Finances);


            bsCust = new BindingSource(ds, ds.Customers.TableName);
            bsFinance = new BindingSource(bsCust, ds.Relations["FK_Cust_Finance"].RelationName);


            tb_CustID.DataBindings.Add(new Binding("Text", bsCust, ds.Customers.CustIDColumn.ColumnName));
            tb_CustName.DataBindings.Add(new Binding("Text", bsCust, ds.Customers.FullNameColumn.ColumnName));


            // Treatments
            tb_TotalCost.DataBindings.Add(new Binding("Text", bsFinance, ds.Finances.TotalCostColumn.ColumnName));
            cb_Settled.DataBindings.Add(new Binding("Checked", bsFinance, ds.Finances.SettledColumn.ColumnName));
            dt_SettledDate.DataBindings.Add(new Binding("Text", bsFinance, ds.Finances.SettledDateColumn.ColumnName));
            dt_SettledDate.ValueChanged += new EventHandler(dt_SettledDate_ValueChanged);
}
        private void dt_SettledDate_ValueChanged(object sender, EventArgs e)
        {
            if(dt_SettledDate.Checked == false)
            {
                dt_SettledDate.Value = dt_SettledDate.MinDate;
                dt_SettledDate.Checked = false;
            }
        }

Within my 'Finances' table are 3 rows, the first which contains a date in 'SettledDate', the second 2 containing null values. What I'm experiencing is:



  • When I click the button, the DateTimePicker shows the date contained within the database. I then click my 'Next' button which calls 'bsCust.MoveNext()'.
  • At this point, I see the 'MinDate' from my event handler shown above. Clicking next again does the same thing.
  • When I eventually go back (calling 'MoveNext()' from my 'Previous' button) from my second record back to my first (that contains an actual date), the date is now not shown, it simply shows the 'MinDate'.

BUT


  • I change the 'ShowCheckBox' property on my DateTimePicker control, run the application, try the above again, and now when I change records everything updates as it should - it works as I want and as you would expect. The problem is, I don't want this checkbox shown to the client.

I'm confused, and lost!

Any ideas?

Thanks in advance - any help will be gratefully received, trust me!
 
I've only ever seen this done effectively with a custom control. There's such a control here, written in VB, that I have tested and confirmed to be working. You can either compile that into a DLL, convert the code to C# or look for a similar existing C# control.
 
I've only ever seen this done effectively with a custom control. There's such a control here, written in VB, that I have tested and confirmed to be working. You can either compile that into a DLL, convert the code to C# or look for a similar existing C# control.
Thank you very much jmcilhinney. One question though:

If the client is going to be presented with a 'MinValue', as in my case, is the code solution a better one compared to default database values that carry an obviously incorrect date (such as a literal 'MinValue')?

I ask because I'm not massively experienced so advice would be helpful. To make the date obviously irrelevant when the client browses records within the application, I could simply add some basic code to manage the display of the date picker.

Sent from my SM-G935F using Tapatalk
 
You should design your database based on the data, not on your UI. If a record logically doesn't have a value for a date column then it should contain null, not a place-holder value.

You could perform a mapping between database data and UI data to replace nulls with place-holder dates and vice versa but, given that this workaround allows you to display a blank DTP for null, just as you would display a blank TextBox, I would consider that the best solution.
 
You should design your database based on the data, not on your UI. If a record logically doesn't have a value for a date column then it should contain null, not a place-holder value.

You could perform a mapping between database data and UI data to replace nulls with place-holder dates and vice versa but, given that this workaround allows you to display a blank DTP for null, just as you would display a blank TextBox, I would consider that the best solution.
All makes sense. Where there's logic, I listen!

Thanks very much. I'll get that code converted.

Sent from my SM-G935F using Tapatalk
 
Back
Top Bottom