473,326 Members | 2,732 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,326 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 2280
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.