Addnew()

H

Hemang Shah

I have a checkbox, date time picker & few comboboxes bound to the same
dataset

I know checkbox, datetimepicker won't let me have null when I call AddNew()

Also because if I bind my combo boxes to the same dataset they all change
automagically.

So my theory:

On Addnew(), before addnew is called, unbind the checkbox & datetimepicker &
comboboxes and before endcommit is called, populate the new record with the
value in these controls.

sounds good eh ? Now can someone help me put that into code?

Thanks :)

HS
 
K

Keenan Newton

why can't you default the values of the checkbox contolr and the
datetimepicker control
 
N

news.microsoft.com

Checkbox I can

Date is not always possible like "hire date" or "date of birth" the client
wants it blank rather than giving it some vague date as default.

Also then the issue remains with multiple combo boxes bound to the same
datasource.

Thanks
 
K

Keenan Newton

Well with the datetimepicker is setting a DateTime value, Since
DateTime cannot be null, a date must be there. As for the multiple
cobo boxes what i tyoically do i once I get my busienss entity (dataset
or whatever) I add a record to it and call it "<Please Specify>" or
maybe "<Non Selected>". If you need to have a blank datetimepicker try
looking for some third party controls (infragistics, Component One,
etc...)
 
G

Guest

DateTime stuff is really tricky for some reason. We had to write a lot of
code in our DateTimeControl subclass to handle stuff. I didn't write the
code, so I can't point you in the right direction, other than to say that it
needs a lot of work.

Now, the issue with the multiple Combos ... you need to have different
Tables as your DataSource for each Combo. It's simple enough to do though ...
just make a Copy of each table and use a copy for each Combo's DataSource.

~~Bonnie
 
H

Hemang Shah

I understand its tricky

But what if I don't want to bind it ?

Before calling ADDNew() I unbind the checkbox and datetime

Before calling endcurrentEdit I assign the value of these controls to my
binding.current.[fields] = datetimepicker.value

and then called update. Won't that work ?

I'm not able to get my typed dataset to give me false for null boolean for
checkboxes either.

Thanks

Hemang
 
G

Guest

I assume that you're using the DataBinding's AddNew() method? I doubt if that
will work correctly if you're unbinding your controls. I don't add rows that
way ... instead I add directly to the DataTable, so you'd do something like
this:

DataRow row = MyTable.NewRow();
// code to populate the row
MyTable.Rows.Add(row);

Although, it seems kinda kludgy to handle it this way (by unbinding stuff).
I think it's better to take the time to write a DateTime picker sub-class.

Also, for the CheckBox DBNULL problem ... that's easily handled by a
sub-class of the CheckBox control by having an Event Handler for the Format
event:

protected virtual void FormatHandler(object sender, ConvertEventArgs e)
{
if (e.Value == System.DBNull.Value)
e.Value = false;
}

~~Bonnie



Hemang Shah said:
I understand its tricky

But what if I don't want to bind it ?

Before calling ADDNew() I unbind the checkbox and datetime

Before calling endcurrentEdit I assign the value of these controls to my
binding.current.[fields] = datetimepicker.value

and then called update. Won't that work ?

I'm not able to get my typed dataset to give me false for null boolean for
checkboxes either.

Thanks

Hemang
Bonnie Berent said:
DateTime stuff is really tricky for some reason. We had to write a lot of
code in our DateTimeControl subclass to handle stuff. I didn't write the
code, so I can't point you in the right direction, other than to say that
it
needs a lot of work.

Now, the issue with the multiple Combos ... you need to have different
Tables as your DataSource for each Combo. It's simple enough to do though
...
just make a Copy of each table and use a copy for each Combo's DataSource.

~~Bonnie
 
H

Hemang Shah

Thanks Bonnie

I don't mind taking the time to write subclass for null for datetimepicker,
I just don't know how. Codeproject has a good control but according to the
messages on it, its not fully operational. I can't even find a commercial
control that I can pay for.

I'll google subclass to see how do I do that.

I know about the format & parse method. But don't I have to do that for
each checkbox on my form ?

I guess not, if there is a way to format the base checkbox control from
where everyone is instanced from. I have to do this in a separate dll ? or
I can do it in my app itself.

I'm migrating from Access development as I wanted to take the plunge into
"real" development, these are rough waters though.

If you can point me to any article on this it would be great.

Thanks a lot again

HS

Bonnie Berent said:
I assume that you're using the DataBinding's AddNew() method? I doubt if
that
will work correctly if you're unbinding your controls. I don't add rows
that
way ... instead I add directly to the DataTable, so you'd do something
like
this:

DataRow row = MyTable.NewRow();
// code to populate the row
MyTable.Rows.Add(row);

Although, it seems kinda kludgy to handle it this way (by unbinding
stuff).
I think it's better to take the time to write a DateTime picker sub-class.

Also, for the CheckBox DBNULL problem ... that's easily handled by a
sub-class of the CheckBox control by having an Event Handler for the
Format
event:

protected virtual void FormatHandler(object sender, ConvertEventArgs e)
{
if (e.Value == System.DBNull.Value)
e.Value = false;
}

~~Bonnie



Hemang Shah said:
I understand its tricky

But what if I don't want to bind it ?

Before calling ADDNew() I unbind the checkbox and datetime

Before calling endcurrentEdit I assign the value of these controls to my
binding.current.[fields] = datetimepicker.value

and then called update. Won't that work ?

I'm not able to get my typed dataset to give me false for null boolean
for
checkboxes either.

Thanks

Hemang
"Bonnie Berent [C# MVP]" <[email protected]>
wrote
in message news:[email protected]...
DateTime stuff is really tricky for some reason. We had to write a lot
of
code in our DateTimeControl subclass to handle stuff. I didn't write
the
code, so I can't point you in the right direction, other than to say
that
it
needs a lot of work.

Now, the issue with the multiple Combos ... you need to have different
Tables as your DataSource for each Combo. It's simple enough to do
though
...
just make a Copy of each table and use a copy for each Combo's
DataSource.

~~Bonnie

:

I have a checkbox, date time picker & few comboboxes bound to the same
dataset

I know checkbox, datetimepicker won't let me have null when I call
AddNew()

Also because if I bind my combo boxes to the same dataset they all
change
automagically.

So my theory:

On Addnew(), before addnew is called, unbind the checkbox &
datetimepicker &
comboboxes and before endcommit is called, populate the new record
with
the
value in these controls.

sounds good eh ? Now can someone help me put that into code?

Thanks :)

HS
 
G

Guest

Let me give you a little Jump Start on sub-classes. Sub-classing the base
controls is the first thing one should do in any language. I've been doing it
for years ... way before I ever started with .NET. There are always little
quirks in behavior in some of the base class controls that you want to get
around for every instance of an object.

Say, as an example, that you've developed a few forms for your application.
You've used the base class controls. At some point, you find something's not
quite acting right and you want to change that behavior or you may just want
to add something (some properties or something) to every ComboBox that you
use. Now, since you used the base class controls to begin with ... guess
what? You're outta luck!! You've got a lot of work ahead of you to change all
those. Whereas, if you had sub-classed all the controls first (even if you
haven't as yet put code in those sub-classes) and used the sub-classed
controls on your forms, then you'll have no extra work to do when you make
changes to your sub-classed control. (and yes, even the form should be
sub-classed).


Basically, you'll want a class library that contains your sub-classed UI
controls, like textbox, button, checkbox, etc. Something like this:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyCompany.WinUI.MyClasses
{
public class MyComboBox : System.Windows.Forms.ComboBox
{
// code here
}

public class MyTextBox : System.Windows.Forms.TextBox
{
// code here
}

public class MyButton : System.Windows.Forms.Button
{
// code here
}
}

That's it. These controls can't be sub-classed visually, but as you can see,
it's easy enough to do it in code. I have all the basic controls sub-classed
in one class library file. Once they're added to the ToolBox, then can be
dragged onto any design surface in the IDE.

And this is what I was talking about when I said to put a Format
eventhandler in your CheckBox ... put it in the sub-class and then you'll
never have to worry about it again.

I hope this helps you get started!!

~~Bonnie
 
H

Hemang Shah

Hello

I tried this, but when i call AddNew() the form is not refreshed with blank
items. As soon as I unbind to this check box it blanks out:

using System;

using System.Windows.Forms;

using System.ComponentModel;

using System.Drawing;

namespace ctlNullCheckBox

{

/// <summary>

/// Summary description for ctlChkBox.

/// </summary>

public class ctlChkBox : System.Windows.Forms.CheckBox

{

protected virtual void FormatHandler(object sender,
System.Windows.Forms.ConvertEventArgs e)

{

if (e.Value == System.DBNull.Value)

e.Value = false;

}

public ctlChkBox()

{

//

// TODO: Add constructor logic here

//

}

}

}



Am I missing something ?

Thanks



Bonnie Berent said:
I assume that you're using the DataBinding's AddNew() method? I doubt if
that
will work correctly if you're unbinding your controls. I don't add rows
that
way ... instead I add directly to the DataTable, so you'd do something
like
this:

DataRow row = MyTable.NewRow();
// code to populate the row
MyTable.Rows.Add(row);

Although, it seems kinda kludgy to handle it this way (by unbinding
stuff).
I think it's better to take the time to write a DateTime picker sub-class.

Also, for the CheckBox DBNULL problem ... that's easily handled by a
sub-class of the CheckBox control by having an Event Handler for the
Format
event:

protected virtual void FormatHandler(object sender, ConvertEventArgs e)
{
if (e.Value == System.DBNull.Value)
e.Value = false;
}

~~Bonnie



Hemang Shah said:
I understand its tricky

But what if I don't want to bind it ?

Before calling ADDNew() I unbind the checkbox and datetime

Before calling endcurrentEdit I assign the value of these controls to my
binding.current.[fields] = datetimepicker.value

and then called update. Won't that work ?

I'm not able to get my typed dataset to give me false for null boolean
for
checkboxes either.

Thanks

Hemang
"Bonnie Berent [C# MVP]" <[email protected]>
wrote
in message news:[email protected]...
DateTime stuff is really tricky for some reason. We had to write a lot
of
code in our DateTimeControl subclass to handle stuff. I didn't write
the
code, so I can't point you in the right direction, other than to say
that
it
needs a lot of work.

Now, the issue with the multiple Combos ... you need to have different
Tables as your DataSource for each Combo. It's simple enough to do
though
...
just make a Copy of each table and use a copy for each Combo's
DataSource.

~~Bonnie

:

I have a checkbox, date time picker & few comboboxes bound to the same
dataset

I know checkbox, datetimepicker won't let me have null when I call
AddNew()

Also because if I bind my combo boxes to the same dataset they all
change
automagically.

So my theory:

On Addnew(), before addnew is called, unbind the checkbox &
datetimepicker &
comboboxes and before endcommit is called, populate the new record
with
the
value in these controls.

sounds good eh ? Now can someone help me put that into code?

Thanks :)

HS
 
G

Guest

OK, first of all, don't create your CheckBox sub-class this way. Use the
example I posted earlier as the way to do it.

Second, you're missing the delegate for the FormatHandler(). The Format
event is actually an event on the Binding, so somewhere in your sub-class you
should have method for adding a binding and then set the delegate to the
Format event there. Something like this (just to get you started):

Binding oBinding;
DataTable BoundTable;
string BoundColumn;

public virtual void DataBind(DataTable dt, string column)
{
this.Checked = false;
this.oBinding = new Binding("Checked", dt, column);
this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
}

~~Bonnie
 
H

Hemang Shah

I gave up :(

I used :

this.BindingContext[dvClient].EndCurrentEdit();
this.BindingContext[dvClient].SuspendBinding();
this.BindingContext[dvClient].AddNew();
DataRowView drv = (DataRowView) this.BindingContext[dvClient].Current;
drv["ChildMinding"] = false; // these are the boolean checkboxes
drv["WorkPermit"] = false; // these are the boolean checkboxes
drv["WorkCDN"] = false; // these are the boolean checkboxes
this.BindingContext[dvClient].ResumeBinding();
this.BindingContext[dvClient].Position =
this.BindingContext[dvClient].Count -1;

and I bound the dates to the text field and that seems to work! why have
value & text then?
I know this is not the best method, but i'm past my deadline for the
project. But thanks so much for this immense help. Not many people take
this kind of efforts to teach.
I tried to mail you but it bounced back (Yeah I checked the proxy email
later lol).
Cheers.
HS

Bonnie Berent said:
OK, first of all, don't create your CheckBox sub-class this way. Use the
example I posted earlier as the way to do it.

Second, you're missing the delegate for the FormatHandler(). The Format
event is actually an event on the Binding, so somewhere in your sub-class
you
should have method for adding a binding and then set the delegate to the
Format event there. Something like this (just to get you started):

Binding oBinding;
DataTable BoundTable;
string BoundColumn;

public virtual void DataBind(DataTable dt, string column)
{
this.Checked = false;
this.oBinding = new Binding("Checked", dt, column);
this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
}

~~Bonnie



Hemang Shah said:
Hello

I tried this, but when i call AddNew() the form is not refreshed with
blank
items. As soon as I unbind to this check box it blanks out:

using System;

using System.Windows.Forms;

using System.ComponentModel;

using System.Drawing;

namespace ctlNullCheckBox

{

/// <summary>

/// Summary description for ctlChkBox.

/// </summary>

public class ctlChkBox : System.Windows.Forms.CheckBox

{

protected virtual void FormatHandler(object sender,
System.Windows.Forms.ConvertEventArgs e)

{

if (e.Value == System.DBNull.Value)

e.Value = false;

}

public ctlChkBox()

{

//

// TODO: Add constructor logic here

//

}

}

}



Am I missing something ?

Thanks



"Bonnie Berent [C# MVP]" <[email protected]>
wrote
in message news:[email protected]...
I assume that you're using the DataBinding's AddNew() method? I doubt if
that
will work correctly if you're unbinding your controls. I don't add rows
that
way ... instead I add directly to the DataTable, so you'd do something
like
this:

DataRow row = MyTable.NewRow();
// code to populate the row
MyTable.Rows.Add(row);

Although, it seems kinda kludgy to handle it this way (by unbinding
stuff).
I think it's better to take the time to write a DateTime picker
sub-class.

Also, for the CheckBox DBNULL problem ... that's easily handled by a
sub-class of the CheckBox control by having an Event Handler for the
Format
event:

protected virtual void FormatHandler(object sender, ConvertEventArgs e)
{
if (e.Value == System.DBNull.Value)
e.Value = false;
}

~~Bonnie
 
G

Guest

Sorry that you had to give up, but I realize how those darn deadlines can
creep up on you. When you have more time though, you should try messing
around with the concepts that I showed you, as far as sub-classing your
controls and, in particular, the code for the CheckBox. It would make your
next project far more easier.

Good luck!

~~Bonnie


Hemang Shah said:
I gave up :(

I used :

this.BindingContext[dvClient].EndCurrentEdit();
this.BindingContext[dvClient].SuspendBinding();
this.BindingContext[dvClient].AddNew();
DataRowView drv = (DataRowView) this.BindingContext[dvClient].Current;
drv["ChildMinding"] = false; // these are the boolean checkboxes
drv["WorkPermit"] = false; // these are the boolean checkboxes
drv["WorkCDN"] = false; // these are the boolean checkboxes
this.BindingContext[dvClient].ResumeBinding();
this.BindingContext[dvClient].Position =
this.BindingContext[dvClient].Count -1;

and I bound the dates to the text field and that seems to work! why have
value & text then?
I know this is not the best method, but i'm past my deadline for the
project. But thanks so much for this immense help. Not many people take
this kind of efforts to teach.
I tried to mail you but it bounced back (Yeah I checked the proxy email
later lol).
Cheers.
HS

Bonnie Berent said:
OK, first of all, don't create your CheckBox sub-class this way. Use the
example I posted earlier as the way to do it.

Second, you're missing the delegate for the FormatHandler(). The Format
event is actually an event on the Binding, so somewhere in your sub-class
you
should have method for adding a binding and then set the delegate to the
Format event there. Something like this (just to get you started):

Binding oBinding;
DataTable BoundTable;
string BoundColumn;

public virtual void DataBind(DataTable dt, string column)
{
this.Checked = false;
this.oBinding = new Binding("Checked", dt, column);
this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
}

~~Bonnie



Hemang Shah said:
Hello

I tried this, but when i call AddNew() the form is not refreshed with
blank
items. As soon as I unbind to this check box it blanks out:

using System;

using System.Windows.Forms;

using System.ComponentModel;

using System.Drawing;

namespace ctlNullCheckBox

{

/// <summary>

/// Summary description for ctlChkBox.

/// </summary>

public class ctlChkBox : System.Windows.Forms.CheckBox

{

protected virtual void FormatHandler(object sender,
System.Windows.Forms.ConvertEventArgs e)

{

if (e.Value == System.DBNull.Value)

e.Value = false;

}

public ctlChkBox()

{

//

// TODO: Add constructor logic here

//

}

}

}



Am I missing something ?

Thanks



"Bonnie Berent [C# MVP]" <[email protected]>
wrote
in message I assume that you're using the DataBinding's AddNew() method? I doubt if
that
will work correctly if you're unbinding your controls. I don't add rows
that
way ... instead I add directly to the DataTable, so you'd do something
like
this:

DataRow row = MyTable.NewRow();
// code to populate the row
MyTable.Rows.Add(row);

Although, it seems kinda kludgy to handle it this way (by unbinding
stuff).
I think it's better to take the time to write a DateTime picker
sub-class.

Also, for the CheckBox DBNULL problem ... that's easily handled by a
sub-class of the CheckBox control by having an Event Handler for the
Format
event:

protected virtual void FormatHandler(object sender, ConvertEventArgs e)
{
if (e.Value == System.DBNull.Value)
e.Value = false;
}

~~Bonnie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top