Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid Event Handler 1

Status
Not open for further replies.

rsshetty

Programmer
Dec 16, 2003
236
US
Hello,

I am a .NET newbie and I have been trying to work out an exercise.

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 _315Chap06
{
	/// <summary>
	/// Summary description for stepByStep6_26.
	/// </summary>
	public class stepByStep6_26 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.TextBox TextBox2;
		protected System.Web.UI.WebControls.TextBox TextBox3;
		protected System.Web.UI.WebControls.Button Button1;
	
		SqlConnection cnn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
		DataSet ds =new DataSet();
		protected System.Web.UI.WebControls.DataGrid dgCustomers;
		SqlDataAdapter da = new SqlDataAdapter();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			SqlCommand cmdSelect = cnn.CreateCommand();
			cmdSelect.CommandType = CommandType.Text;
			cmdSelect.CommandText = "Select customerid, companyname,contactname from customers";

			SqlCommand cmdUpdate = cnn.CreateCommand();
			cmdUpdate.CommandType = CommandType.Text;
			cmdUpdate.CommandText = " Update Customers set companyname=@CompanyName"+
				",contactname=@ContactName where customerid=@CustomerId";
			cmdUpdate.Parameters.Add("@CompanyName",SqlDbType.NVarChar,30,"CompanyName");
			cmdUpdate.Parameters.Add("@ContactName",SqlDbType.NVarChar,40,"ContactName");
			cmdUpdate.Parameters.Add("@CustomerId",SqlDbType.NChar,5,"CustomerId");
			cmdUpdate.Parameters["@CustomerId"].SourceVersion=DataRowVersion.Original;

			SqlCommand cmdInsert = cnn.CreateCommand();
			cmdInsert.CommandType = CommandType.Text;
			cmdInsert.CommandText = " Insert into Customers (CompanyName,ContactName,CustomerId)";
			cmdInsert.Parameters.Add("@CompanyName",SqlDbType.NVarChar,30,"CompanyName");
			cmdInsert.Parameters.Add("@ContactName",SqlDbType.NVarChar,40,"ContactName");
			cmdInsert.Parameters.Add("@CustomerId",SqlDbType.NChar,5,"CustomerId");
			cmdInsert.Parameters["@CustomerId"].SourceVersion=DataRowVersion.Original;

			SqlCommand cmdDelete = cnn.CreateCommand();
			cmdDelete.CommandType = CommandType.Text;
			cmdDelete.CommandText = " Delete from Customers where customerid=@CustomerId";
			cmdDelete.Parameters.Add("@CustomerId",SqlDbType.NChar,5,"CustomerId");
			cmdDelete.Parameters["@CustomerId"].SourceVersion=DataRowVersion.Original;
			
			da.SelectCommand = cmdSelect;
			da.UpdateCommand = cmdUpdate;
			da.InsertCommand = cmdInsert;
			da.DeleteCommand = cmdDelete;
			da.Fill(ds,"Customers");

			if(!IsPostBack)
			{
				LoadData();
			}
		}

		private void LoadData()
		{
			dgCustomers.DataSource = ds;
			dgCustomers.DataMember = "Customers";
			dgCustomers.DataBind();
		}

		private void dgCustomers_CancelCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			dgCustomers.EditItemIndex=-1;
			LoadData();
			
		}

		private void dgCustomers_EditCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e) 
		{
			dgCustomers.EditItemIndex=e.Item.ItemIndex;
			LoadData();
		}
		
		private void dgCustomers_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataRow[] adrEdit=ds.Tables["Customers"].Select("CustomerId='"+dgCustomers.DataKeys[e.Item.ItemIndex]+"'");

			if(adrEdit.Length > 0)
			{
				TextBox TextBox2 = (TextBox) e.Item.Cells[3].Controls[0];
				adrEdit[0]["CompanyName"]=TextBox2.Text; 

				TextBox TextBox3 = (TextBox) e.Item.Cells[4].Controls[0];
				adrEdit[0]["ContactName"]=TextBox3.Text; 
			}
		
			dgCustomers.EditItemIndex=-1;
			da.Update(ds,"Customers");
			LoadData();

		}

		private void dgCustomers_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			String strCustomerId = dgCustomers.DataKeys[e.Item.ItemIndex].ToString();

			foreach(DataRow dr in ds.Tables["Customers"].Rows)
			{
				if(dr[0].ToString() == strCustomerId)
				{
					dr.Delete();
					break;
				}
			}

			da.Update(ds,"Customers");
			LoadData();
		}



		#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.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			DataRow dr = ds.Tables["Customers"].NewRow();

			dr[0]=TextBox1.Text;
			dr[1]=TextBox2.Text;
			dr[2]=TextBox3.Text;

			ds.Tables["Customers"].Rows.Add(dr);
			da.Update(ds,"Customers");
			LoadData();

			TextBox1.Text="";
			TextBox2.Text="";
			TextBox3.Text="";
		}
	}
}

However, when I run this, nothing happens when I click on Edit or Delete or any command for that matter.
I think it has something to do with the event handlers though I'm not sure what it is...



rsshetty.
It's always in the details.
 
Can you post your Datagrid HTML here too?

thinkin your missing your OnEditCommand= and OnDeleteCommand= in your asp:datagrid tag
 
Ah... that's it... I am missing that indeed. Thanks!!!

rsshetty.
It's always in the details.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top