471,319 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Must click twice on datagrid to make drid row editable

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;

}

Nov 19 '05 #1
7 2182
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
"Diane" <so*************@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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;

}

Nov 19 '05 #2
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

Nov 19 '05 #3
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
"Diane" <so*************@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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

Nov 19 '05 #4
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

Nov 19 '05 #5
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

Nov 19 '05 #6
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
"Diane" <so*************@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 19 '05 #7
This article may explain the problem you are having

http://west-wind.com/weblog/posts/3016.aspx

-----------------------------------------------------------
I would much rather chew on tinfoil than try and code in C#.
Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by JD | last post: by
1 post views Thread by Arno | last post: by
5 posts views Thread by Dalibor Kusic | last post: by
4 posts views Thread by Stephan Bour | last post: by
5 posts views Thread by J McD | last post: by
reply views Thread by DaveR | last post: by
3 posts views Thread by Angela Chen | last post: by
2 posts views Thread by zambizzi | last post: by
7 posts views Thread by julian.tklim | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.