Databound controls won't give up input focus

C

Cory Burkhardt

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");
 
C

Cory Burkhardt

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


Sijin Joseph said:
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



Cory said:
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 dial og 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");
 
S

Sijin Joseph

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/us...library/en-us/dnadvnet/html/vbnet02252003.asp
http://msdn.microsoft.com/library/d...dnwinforms/html/databinding_winforms10_11.asp


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



Cory said:
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

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




The

og a

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");
 
G

Guest

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
 
Top