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

DataGrid not responding to Update/Cancel event

This is my second post now on this subject has I seem to be getting no
where and the problem is really starting to bug me now. I can get a
datagrid into an editable state but then neither the Update or Cancel
events seem to be firing and I am at a lost to explan why. My code is
below, if any one can tell me what I am doing wrong then that would be
excellent!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DataGrid2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection con;
protected System.Data.SqlClient.SqlCommand cmd;
protected System.Data.SqlClient.SqlDataReader rdr;
protected System.Web.UI.WebControls.Label lbloutput;
protected System.Data.DataTable tbl;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

private void onPageOpen()
{
dbc();
createTable();
getData();
this.DataGrid1.Visible = true;
}

private void dbc()
{
this.con = new SqlConnection("Data Source = *;Initial Catalog =
PLDWH;user id = *;password = *");
}

private void createTable()
{
tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("SKU",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("Description", typeof(string)));
tbl.Columns.Add(new DataColumn("Grade", typeof(string)));
tbl.Columns.Add(new DataColumn("Status", typeof(string)));
}

private void getData()
{
DataRow row;
int i = 1;

try
{
this.con.Open();
this.cmd = new SqlCommand("select top 10 productnumber,
productdescription, productgrade, productstatus from
productdetailscurrent", this.con);
this.cmd.CommandType = CommandType.Text;
this.rdr = this.cmd.ExecuteReader();

while (this.rdr.Read())
{
row = this.tbl.NewRow();
row[0] = i;
row[1] = this.rdr.GetInt32(0);
row[2] = this.rdr.GetString(1);
row[3] = this.rdr.GetString(2);
row[4] = this.rdr.GetString(3);
this.tbl.Rows.Add(row);

i++;
}

this.rdr.Close();
this.cmd.Dispose();
Cache.Insert("table", this.tbl);
bindGrid();
}
catch (SqlException sqlex)
{
this.lbloutput.Text = sqlex.ToString();
}
finally
{
this.con.Close();
}
}

private void bindGrid()
{
this.DataGrid1.DataSource = (DataTable)Cache["table"];
this.DataGrid1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// onPageOpen();
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
onPageOpen();
}
#endregion

private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
bindGrid();
this.lbloutput.Text = "Edit";
}

private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Cancel";
}

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Update";

}
}
}

<%@ Page language="c#" Codebehind="test.aspx.cs"
AutoEventWireup="false" Inherits="DataGrid2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 24px" runat="server"
AutoGenerateColumns="False" DataKeyField="ID" Visible="False">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True"
HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="SKU" ReadOnly="True"
HeaderText="SKU"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Description" Text='<%#
DataBinder.Eval(Container.DataItem, "Description") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grade">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Grade") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox1" Text='<%#
DataBinder.Eval(Container.DataItem, "Grade") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox2" Text='<%#
DataBinder.Eval(Container.DataItem, "Status") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
<asp:Label id="lbloutput" style="Z-INDEX: 102; LEFT: 24px; POSITION:
absolute; TOP: 496px"
runat="server" Width="456px" Height="160px"></asp:Label></form>
</body>
</HTML>

Feb 3 '06 #1
2 1559
Hi Andrew,

Put

onPageOpen();

in Page_Load rather than in InitializeComponent.

And it's better only query DB in first load, like

private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
onPageOpen();
}
}
HTH

Elton Wang

"an************@poundland.co.uk" wrote:
This is my second post now on this subject has I seem to be getting no
where and the problem is really starting to bug me now. I can get a
datagrid into an editable state but then neither the Update or Cancel
events seem to be firing and I am at a lost to explan why. My code is
below, if any one can tell me what I am doing wrong then that would be
excellent!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DataGrid2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection con;
protected System.Data.SqlClient.SqlCommand cmd;
protected System.Data.SqlClient.SqlDataReader rdr;
protected System.Web.UI.WebControls.Label lbloutput;
protected System.Data.DataTable tbl;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

private void onPageOpen()
{
dbc();
createTable();
getData();
this.DataGrid1.Visible = true;
}

private void dbc()
{
this.con = new SqlConnection("Data Source = *;Initial Catalog =
PLDWH;user id = *;password = *");
}

private void createTable()
{
tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("SKU",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("Description", typeof(string)));
tbl.Columns.Add(new DataColumn("Grade", typeof(string)));
tbl.Columns.Add(new DataColumn("Status", typeof(string)));
}

private void getData()
{
DataRow row;
int i = 1;

try
{
this.con.Open();
this.cmd = new SqlCommand("select top 10 productnumber,
productdescription, productgrade, productstatus from
productdetailscurrent", this.con);
this.cmd.CommandType = CommandType.Text;
this.rdr = this.cmd.ExecuteReader();

while (this.rdr.Read())
{
row = this.tbl.NewRow();
row[0] = i;
row[1] = this.rdr.GetInt32(0);
row[2] = this.rdr.GetString(1);
row[3] = this.rdr.GetString(2);
row[4] = this.rdr.GetString(3);
this.tbl.Rows.Add(row);

i++;
}

this.rdr.Close();
this.cmd.Dispose();
Cache.Insert("table", this.tbl);
bindGrid();
}
catch (SqlException sqlex)
{
this.lbloutput.Text = sqlex.ToString();
}
finally
{
this.con.Close();
}
}

private void bindGrid()
{
this.DataGrid1.DataSource = (DataTable)Cache["table"];
this.DataGrid1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// onPageOpen();
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
onPageOpen();
}
#endregion

private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
bindGrid();
this.lbloutput.Text = "Edit";
}

private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Cancel";
}

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Update";

}
}
}

<%@ Page language="c#" Codebehind="test.aspx.cs"
AutoEventWireup="false" Inherits="DataGrid2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 24px" runat="server"
AutoGenerateColumns="False" DataKeyField="ID" Visible="False">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True"
HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="SKU" ReadOnly="True"
HeaderText="SKU"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Description" Text='<%#
DataBinder.Eval(Container.DataItem, "Description") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grade">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Grade") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox1" Text='<%#
DataBinder.Eval(Container.DataItem, "Grade") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox2" Text='<%#
DataBinder.Eval(Container.DataItem, "Status") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
<asp:Label id="lbloutput" style="Z-INDEX: 102; LEFT: 24px; POSITION:
absolute; TOP: 496px"
runat="server" Width="456px" Height="160px"></asp:Label></form>
</body>
</HTML>

Feb 3 '06 #2
Thanks Elton, I have now got this working.

Feb 3 '06 #3

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

Similar topics

1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
0
by: Steve | last post by:
I have a datagrid that is created at run time DataGrid dgG = new DataGrid(); BoundColumn bcB; dgG.CellPadding = 5; dgG.CellSpacing = 0; dgG.GridLines = GridLines.Both; dgG.CssClass =...
2
by: Chris Kennedy | last post by:
I have two datagrids on a page. On the update event of the first I take some of the selected data, create a dataset and add it and then bind it to the second datagrid. If I hit update several...
4
by: Luis Esteban Valencia | last post by:
I have a asp.net page (C#), with a datagrid. I use template for all columns, and have <asp:requiredfieldvalidator> in with one of the textboxes, to make sure it's filled in. However, this...
4
by: siaj | last post by:
Hello All, If some one has faced a similar issue.. My datagrid Update command is not getting fired in fact it seems that the no event fires on clicking the update link. Although the cancel and the...
5
by: Tina | last post by:
the Edit, Update, Cancel, and Delete buttons in my datagrid are causing validation elsewhere on the page. I want to specify that these buttons should not cause validation but they have no design...
6
by: Scott McDaniel | last post by:
I'm using Visual Studio 2005 with the .NET 2.0.50727 framework, and we're using the standard Datagrid control bound to a SQL Server. We have two groups of users - "full edit" users, and "view...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: arlie_maija | last post by:
Hey - I'm writing a control that contains a DataGrid, and I'm unable to get the update event to fire. When I click the update link, the edit event fires. heres the details... my control...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.