473,322 Members | 1,403 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,322 software developers and data experts.

Data grid EditCommandColumn

Hi
I've been following the example on

http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx

and no matter what I do, i can't get the DataGrid1_EditCommand event handler
to fire. Could someone please tell me what I'm doing wrong.
Code below.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
using(SqlCommand cmd = new SqlCommand("select * from SystemInfo",
sqlConnection1))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
sqlConnection1.Open();
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
}

public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
....
<form id="Form1" method="post" runat="server">
<asp:DataGrid Runat="server" ID="DataGrid1" AutoGenerateColumns="False"
OnEditCommand="DataGrid1_EditCommand">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel"
EditText="Edit" UpdateText="Update" />
<asp:BoundColumn ReadOnly="False" DataField="Measure"/>
</Columns>
</asp:DataGrid>
</form>
Nov 16 '05 #1
2 5633
Bonj, you're going to have some issues with the way your code is written
right now. Try doing this:

Create a method called BindData() - this method will be responsible for
binding the database content to the DataGrid. Basically you can take
your code you have now in Page_Load and move it to this new BindData()
method.

Then, in Page_Load, only call BindData() *on the first page load* and
*not* on subsequent postbacks. That is, do:

if (!Page.IsPostBack)
BindData();

Finally, in your EditCommand event handler, update your code to look like:

DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
See if updating the code to this described pattern helps at all. Thanks.

Bonj wrote:
Hi
I've been following the example on

http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx

and no matter what I do, i can't get the DataGrid1_EditCommand event handler
to fire. Could someone please tell me what I'm doing wrong.
Code below.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
using(SqlCommand cmd = new SqlCommand("select * from SystemInfo",
sqlConnection1))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
sqlConnection1.Open();
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
}

public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
...
<form id="Form1" method="post" runat="server">
<asp:DataGrid Runat="server" ID="DataGrid1" AutoGenerateColumns="False"
OnEditCommand="DataGrid1_EditCommand">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel"
EditText="Edit" UpdateText="Update" />
<asp:BoundColumn ReadOnly="False" DataField="Measure"/>
</Columns>
</asp:DataGrid>
</form>

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
Nov 16 '05 #2
Ah, cheers Scott!
I realise that was a bit rushed, I did think however that there was
something I was missing, perhaps some configurational aspect that I had
completely overlooked that wasn't related to that- so I posted the code to
see if anyone could just spot it. What did throw me a bit was the fact that
creating the event handlers automatically using the forms designer puts them
in as private, whereas they need to be at least protected for the aspx to
access them, and I was wondering whether it didn't work if you did that, so
I typed them in manually and wired them up via the OnEditCommand="~~" in the
aspx... However, I have now gone about it like you say by having a separate
method to get the data and bind it, which is only called by the page_load if
it's not a postback, and is also called by the EditCommand handler. And it
now works.
I'm also beginning to think that, that was actually what was *causing* the
problem - as the page was perhaps doing the page_load again due to the
postback, and then completely rebinding the datagrid, which was why my
EditCommand handler wasn't being fired at all. I had thought that the
EditCommand handler should at least fire first, even if the page_load didn't
test for postback and thus reset the datagrid. But it would seem to explain
it if this was not the case.. do you think?

Anyway, thanks for your help

p.s. great website by the way
"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:fb******************@newssvr21.news.prodigy.c om...
Bonj, you're going to have some issues with the way your code is written
right now. Try doing this:

Create a method called BindData() - this method will be responsible for
binding the database content to the DataGrid. Basically you can take your
code you have now in Page_Load and move it to this new BindData() method.

Then, in Page_Load, only call BindData() *on the first page load* and
*not* on subsequent postbacks. That is, do:

if (!Page.IsPostBack)
BindData();

Finally, in your EditCommand event handler, update your code to look like:

DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
See if updating the code to this described pattern helps at all. Thanks.

Bonj wrote:
Hi
I've been following the example on
http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx

and no matter what I do, i can't get the DataGrid1_EditCommand event
handler to fire. Could someone please tell me what I'm doing wrong.
Code below.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
using(SqlCommand cmd = new SqlCommand("select * from SystemInfo",
sqlConnection1))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
sqlConnection1.Open();
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
}

public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
...
<form id="Form1" method="post" runat="server">
<asp:DataGrid Runat="server" ID="DataGrid1" AutoGenerateColumns="False"
OnEditCommand="DataGrid1_EditCommand">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel"
EditText="Edit" UpdateText="Update" />
<asp:BoundColumn ReadOnly="False" DataField="Measure"/>
</Columns>
</asp:DataGrid>
</form>

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jordan O'Hare | last post by:
Hello Everyone, I am after some help with the following: I have a windows application that contains a list box and two data grids. All three controls are binded to a dataset that contains...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
3
by: Snake | last post by:
I have a vb .net program which fills a data grid upon form load from an acccess database. This works great. Now, I have to add a combo box and use it to alter the underlying sql statement and...
2
by: Brian Henry | last post by:
Hi, I have a data grid that is set up like this Page items displayed = 10 EnableViewState = false (i dont want to send large amounts of data over the internet!) CustomPaging = false...
2
by: Bonj | last post by:
Hi I've been following the example on http://aspnet.4guysfromrolla.com/articles/071002-1.3.aspx and no matter what I do, i can't get the DataGrid1_EditCommand event handler to fire. Could...
3
by: pmud | last post by:
Hi, I have a web page (asp.net, code:c#). I havean html table with text boxes. Based on the user input , records are displayed in the data grid below it. Now the datagrid has a large no. of...
6
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
0
by: hlam | last post by:
Help - Calculating the total of a column in a data grid -- when data grid is part of Master-Detail set-up I have setup a Master-Detail form using Visual Studio.Net. A ListBox is the (Master)...
0
by: zafar | last post by:
I don't know what property should be used for hiding colums in Data Grid, whereas in Data Grid View we have DataGridView1.Colums(index).Visible = False , But how can I hide Colums in Data Grid.....
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.