473,231 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 2278
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JD | last post by:
How do i get rid of the 1st column that appears in a datagrid? I do not want this? Also how do i change the colour of the background of a cell when the user selects it. i.e for the first coloumn...
1
by: Arno | last post by:
Hi I am using a dataset to populate a editable datagrid. When I apply a table style however, the datagrid seems to become read-only... How can I make the datagrid editable again? Is there a method...
5
by: Dalibor Kusic | last post by:
The DataTable contains, for example, only one column of lets say Person class. Person class has three fields, and a constructor that takes a single string and parses it over those three fields. ...
4
by: Stephan Bour | last post by:
Hi, I have a datagrid databound to a SQL query. I'd like to allow editing of some columns but not all. Is there a way to turn off the conversion of the datagrid cells to textboxes for some columns...
5
by: J McD | last post by:
Hi I have a DataGrid with an ImageButton column. When I click on an imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I can actually comment out the line where I add this...
0
by: DaveR | last post by:
I have a webform with a two-column datagrid based on an Arraylist. The Arraylist draws the data for the two columns from two different tables in an SQL database. The data is displayed in datagrid...
3
by: Angela Chen | last post by:
Hi, I have a readOnly textbox. I want to be able to double click it to make it editable? How to implement it?\ Thanks a lot
2
by: zambizzi | last post by:
....I can't seem to get my hands on a control I'm loading in an editable datagrid. Here's my datagrid control: <asp:datagrid id="GLRulesGrid" runat="server" autogeneratecolumns="False"...
7
by: julian.tklim | last post by:
Hi, I need to build an editable Datagrid with add & delete buttons on each row using javascript. DataGrid need not be pre-populated with values. To make the thing complicated, one of the...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.