Update/Cancel Command not responding in DataGrid
I hope some one can help with this. I have a data grid that responds to the Edit command but does not respond to either the Canel or Update button clicks, it just sits there. What makes this even more frustrating is the fact that in an earlier project I had this working on my first try. Now I cant see what I'm doing wrong having stared at the code for about 2 days. My code is below minus the Update_Command method, I have been toying with it. Thanks
C# server side code
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 DataGridTest
{
/// <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 lblError;
protected System.Data.DataTable datatbl;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
private void onPageOpen()
{
dbc();
createTable();
getData();
}
private void dbc()
{
con = new SqlConnection("Data Source = 10.1.0.6;Initial Catalog = PLDWH;user id = pldwhaccess;password = pldwhaccess;");
}
private void createTable()
{
this.datatbl = new DataTable();
this.datatbl.Columns.Add(new DataColumn("SKU", System.Type.GetType("System.Int32")));
this.datatbl.Columns.Add(new DataColumn("Description", typeof(string)));
this.datatbl.Columns.Add(new DataColumn("Grade", typeof(string)));
}
private void getData()
{
DataRow row;
try
{
this.con.Open();
this.cmd = new SqlCommand("select top 10 productnumber, productdescription, productgrade from productdetailscurrent", this.con);
this.cmd.CommandType = CommandType.Text;
this.rdr = this.cmd.ExecuteReader();
while (this.rdr.Read())
{
row = this.datatbl.NewRow();
row[0] = this.rdr.GetInt32(0);
row[1] = this.rdr.GetString(1);
row[2] = this.rdr.GetString(2);
this.datatbl.Rows.Add(row);
}
this.rdr.Close();
this.cmd.Dispose();
Session.Add("table", this.datatbl);
bindGrid();
}
catch(SqlException sqlex)
{
}
finally
{
this.con.Close();
}
}
private void bindGrid()
{
this.DataGrid1.DataSource = (DataTable)Session["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.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_ItemCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_EditCommand);
this.DataGrid1.CancelCommand += new DataGridCommandEventHandler(this.DataGrid1_CancelC ommand);
this.Load += new System.EventHandler(this.Page_Load);
onPageOpen();
}
#endregion
private void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
this.lblError.Text = "Edit";
this.DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
this.lblError.Text = "Edit";
this.bindGrid();
this.lblError.Text = "Edit";
}
public void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
this.lblError.Text = "Cancel";
this.DataGrid1.EditItemIndex = -1;
//getData();
this.lblError.Text = "Cancel";
}
private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandSource.ToString() == "Cancel")
{
this.lblError.Text = "Cancel";
}
}
}
}
ASPX code
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="DataGridTest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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: 24px; POSITION: absolute; TOP: 32px" runat="server"
DataKeyField="SKU" AutoGenerateColumns="False">
<Columns>
<asp:ButtonColumn Text="Select" CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn DataField="SKU" ReadOnly="True" HeaderText="SKU"></asp:BoundColumn>
<asp:BoundColumn DataField="Description" HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="Grade" HeaderText="Grade"></asp:BoundColumn>
<asp:EditCommandColumn HeaderText="Edit" EditText="Edit" CancelText="Cancel" UpdateText="Update" ButtonType="PushButton"></asp:EditCommandColumn>
</Columns>
</asp:datagrid><asp:label id="lblError" style="Z-INDEX: 102; LEFT: 360px; POSITION: absolute; TOP: 536px"
runat="server" Height="32px" Width="376px">g</asp:label></form>
</body>
</HTML>
|