Must click twice on datagrid to make drid row editable

D

Diane

Hi-

I've been struggling with this problem and none of the fixes posted
seem to help out at all. Yet, it seems like such a simple problem...

I have a DataGrid, and load it up with data on page load. In my
EditCommand event handler I set the editItemindex. All good. Except,
the user needs to click twice to get the DataGrid to become editable.

It's obvious to me (perhaps worngly) that ASP.NET is storing the data
form my grid in the viewstate, and setting up the datagrid BEFORE the
Edit button event. That's why the grid appears editable the next page
load.

So, how do I refresh the grid to show the editability? Predictably,
calling DataBind makes all the data go away... String the data set in
the session seems redundantg, since the page is definitely storing the
data for me in the viewstate. Not that I can get to it...

Any ideas?

Diane

<PRE>
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack == false)
{
sqlConnection1.ConnectionString = "..pubs..";
string query = "SELECT * FROM stores";
this.sqlDataAdapter1.SelectCommand.CommandText = query;

this.sqlDataAdapter1.SelectCommand.Connection =
sqlConnection1;


this.sqlConnection1.Open();
this.sqlDataAdapter1.Fill(this.dataSet1, "storesAll");
this.DataGrid1.DataSource =
this.dataSet1.Tables["storesAll"];
this.DataGrid1.DataBind();


this.sqlConnection1.Close();
}



}


private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = e.Item.ItemIndex;

}
 
S

S. Justin Gengo

Diane,

I'm not certain but this may be a different problem all together. I've
noticed this type of behaviour with dynamically created controls. If your
datagrid is being created dynamically or is part of a user control being
placed on a main page dynamically then try this:

Give your control an id when it's created. E.g. if this is a dynamic user
control in the control's page load routine put: Me.Id = "MyDynamicControl1"

Something in the framework looks for a control's user id to hook up the code
and doesn't work the first time through if that id isn't there... I'm still
not positive what causes the behaviour though, but giving it the Id fixes
it.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
D

Diane

S Justin -

In my case, the DataGrid is created at design time, not dynamically.
So it already has an ID. My guess is that it is a timing issue: the
button click event happens after the DataGrid has been bound already,
and so the effects don;t show up til the next page load...

Thanks,
Diane
 
S

S. Justin Gengo

Diane,

You should only need to bind your datagrid on pageload:

If Not IsPostBack Then
'---First page load
'---Call routine to bind grid
Else
'---Post back only
End If

Then, as long as viewstate is on for the grid your button should work the
first time.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
D

Diane

Justin-

Yes, I agree, if the viewstate is on the Edit button should work the
first time. That is the behavior I would expect, which makes it all
the more puzzling that things don't behave that way.

I don't suppose I can expect my users to click twice on the edit
button, so I guess I will have to settle for making another trip to the
databse in the event handler for the edit command. It would be cool to
be able to work with disconnected data until the update command, but
there seems to be no solution to this problem. Oh well, technology
can't be perfect.

Thanks for the support!
Diane
 
D

Diane

Justin-

Well, I've looked around at lots of examples on MS and other sites, and
everyone seems to load up the data from the DB again after the
EditCommand, or they call the Page DataBind method (which in my case
makes all the data dissapear, as I would expect. Not sure how folks
get it to behave otherwise).

Anyhow, it looks like in posted examples, people avoid this issue be
making an extra trip to the DB, or by saving the datagrid the session.

Tantalizingly, though, it almost works without a second trip to the DB.


Sigh,

Diane
 
S

S. Justin Gengo

Diane,

Too bad you didn't get it working with a single trip.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top