Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

Databound controls won't give up input focus

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cory Burkhardt - 08 Sep 2004 05:44 GMT
In my application, I am binding controls in a dialog box to a DataRow.  The
first time I launch the dialog, it works fine.  But if I launch the dialog a
second time and bind it to the same DataRow again, the first control that I
modify will not relinquish input focus to any other control in the client
area of the dialog box.  I can't even click the OK or Cancel buttons.  Even
if I cancel out of the dialog the first time without making any
modifications, which results in RejectChanges() being called on the row, the
next time I create and show the dialog, it will lock up the input focus.  I
dispose the dialog after I am through with it, and the second time I launch
the dialog is with a new instance of it.  I can even launch the dialog and
data bind it to different DataRows with no problems.  But the first time I
databind it to a DataRow for the second time, the controls lock up.  Anyone
know what is causing this strange behavior?

This is my databinding code:

if(gameRow["AwayTeamId"] == DBNull.Value)
gameRow["AwayTeamId"] = -1;
if(gameRow["HomeTeamId"] == DBNull.Value)
gameRow["HomeTeamId"] = -1;
if(gameRow["GameDate"] == DBNull.Value)
gameRow["GameDate"] = defaultDate;

ddlAwayTeam.DataSource = awayRows;
ddlHomeTeam.DataSource = homeRows;
ddlAwayTeam.DisplayMember = ddlHomeTeam.DisplayMember = "Name";
ddlAwayTeam.ValueMember = ddlHomeTeam.ValueMember = "TeamId";

ddlAwayTeam.DataBindings.Add("SelectedValue", gameRow, "AwayTeamId");
ddlHomeTeam.DataBindings.Add("SelectedValue", gameRow, "HomeTeamId");
dtpickDate.DataBindings.Add("Value", gameRow, "GameDate");
Sijin Joseph - 08 Sep 2004 13:43 GMT
Try commenting out these lines

ddlAwayTeam.DataBindings.Add("SelectedValue", gameRow, "AwayTeamId");
ddlHomeTeam.DataBindings.Add("SelectedValue", gameRow, "HomeTeamId");

Looks redundant, can you tell me why the above two lines are there?

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> In my application, I am binding controls in a dialog box to a DataRow.  The
> first time I launch the dialog, it works fine.  But if I launch the dialog a
[quoted text clipped - 27 lines]
> ddlHomeTeam.DataBindings.Add("SelectedValue", gameRow, "HomeTeamId");
> dtpickDate.DataBindings.Add("Value", gameRow, "GameDate");
Cory Burkhardt - 08 Sep 2004 17:50 GMT
I bound the two comboboxes to a set of rows that will be used to fill in the
combobox with items.  The values filled in include a team name for the
display member, and a team id for the value member.  I then binded the
Selected value to a field in another row, a row that represents a game to be
played.  In this way, the user can select a team from the combobox, and that
team's id value would get put into the game row.  Make sense?

I actually have another dialog that does the same thing, but doesn't have
this kind of double-binding.  It binds 6 datetime picker controls to values
in 3 rows (2 controls/values for each row).  This is an example of one of
these:

int year = preseasonRow.SeasonEndYear;

if(preseasonRow["StartDate"] == DBNull.Value ||
(DateTime)preseasonRow["StartDate"] == DateTime.MinValue)
preseasonRow["StartDate"] = new DateTime(year-1, 10, 1);

if(preseasonRow["EndDate"] == DBNull.Value ||
(DateTime)preseasonRow["EndDate"] == DateTime.MinValue)
preseasonRow["EndDate"] = new DateTime(year-1, 10, 28);

dtpickBeginPreseason.DataBindings.Add("Value", preseasonRow, "StartDate");
dtpickEndPreseason.DataBindings.Add("Value", preseasonRow, "EndDate");

This dialog has the same control lockup problem when I open it and bind it
to the row for the second time.  However, it works fine if I only launch the
dialog once for that specific row.

Cory

> Try commenting out these lines
>
[quoted text clipped - 38 lines]
> > ddlHomeTeam.DataBindings.Add("SelectedValue", gameRow, "HomeTeamId");
> > dtpickDate.DataBindings.Add("Value", gameRow, "GameDate");
Sijin Joseph - 09 Sep 2004 07:22 GMT
Hi Cory,

Are you using ShowDialog() or Show() to show your dialog boxes? More
importantly are you using the same form object when you show the form
the second time.

I think what is happening is that you are reusing the same form object
when you show the form the second time, if this is the case then the
databindings are getting attached twice which is causing the lockup.

Use this to check if a binding already exists

if(dtPickDate.DataBindings["Value"] == null)
{
    dtpickDate.DataBindings.Add("Value", gameRow, "GameDate");
}

Similarly you might want to check the databindings for the other
datetime controls as well. You should be ok with setting the datasource
and datamember propertys again.

Also for formatting the DateTime's you might want to take a look at
Binding.Format and Binding.Parse events, they let you format the value
before it is shown in the UI, you could use that convert null or emptty
dates to your requirement and vice-versa.

I still think the SelectedValue databindings are redundant, what you are
trying to do can be done by simply using DataSource and DataMember
properties.

Also take a look at these two good articles on DataBinding, they helped
me a lot.

http://msdn.microsoft.com/vbasic/using/columns/adventures/default.aspx?pull=/lib
rary/en-us/dnadvnet/html/vbnet02252003.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html
/databinding_winforms10_11.asp


Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> I bound the two comboboxes to a set of rows that will be used to fill in the
> combobox with items.  The values filled in include a team name for the
[quoted text clipped - 102 lines]
>>>ddlHomeTeam.DataBindings.Add("SelectedValue", gameRow, "HomeTeamId");
>>>dtpickDate.DataBindings.Add("Value", gameRow, "GameDate");
Doug Perkes - 28 Sep 2004 23:31 GMT
Cory,

Did you every come up with a fix for this issue? I've got the same problem
you describe and have been banging my head against my desk trying to come up
with a solution. It hasn't worked ;-(.

-- doug

> In my application, I am binding controls in a dialog box to a DataRow.  The
> first time I launch the dialog, it works fine.  But if I launch the dialog a
[quoted text clipped - 27 lines]
> ddlHomeTeam.DataBindings.Add("SelectedValue", gameRow, "HomeTeamId");
> dtpickDate.DataBindings.Add("Value", gameRow, "GameDate");

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.