Click here to Skip to main content
15,893,564 members
Articles / Web Development / ASP.NET

Multiple Column Dropdownlist for the ASP.NET DataGrid

Rate me:
Please Sign up or sign in to vote.
4.46/5 (43 votes)
1 Jun 20053 min read 424.4K   6.7K   140  
An article on multiple column dropdownlists for the ASP.NET DataGrid.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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.Configuration;
namespace CSharpSample
{
	/// <summary>
	/// Summary description for WebForm6.
	/// </summary>
	public class WebForm6 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Message;
		protected System.Web.UI.WebControls.DataGrid jskDataGrid;
		SqlDataAdapter sqlDa;
		SqlConnection SqlCon = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);	
		DataSet ds;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if (! IsPostBack)
			{

				BindDataGrid();

			}

		}
		protected void BindDataGrid()
		{
			
			string sqlStr = "SELECT S.CompanyName,S.SupplierID,P.ProductName,P.ProductID " +
									"from Suppliers S inner join Products P " +
									"on S.SupplierID = P.SupplierID ";
			sqlDa = new SqlDataAdapter(sqlStr, SqlCon);
			ds = new DataSet();
			sqlDa.Fill(ds, "Suppliers");
			jskDataGrid.DataSource = ds.Tables["Suppliers"];
			jskDataGrid.DataBind();

		}

		protected void jskDataGrid_Edit(object sender, DataGridCommandEventArgs e) 
		{

			jskDataGrid.EditItemIndex = e.Item.ItemIndex;
			BindDataGrid();
		}

		protected void jskDataGrid_Cancel(object sender, DataGridCommandEventArgs e) 
		{

			jskDataGrid.EditItemIndex = -1;
			BindDataGrid();

		}
		protected void jskDataGrid_Update(object sender, DataGridCommandEventArgs e) 
		{
			try
			{
				string itemValue;
				string itemText;
				
				((ddList.mcDDList)e.Item.FindControl("McDDList1")).ReturnsValue = true;
				itemValue = ((ddList.mcDDList)e.Item.FindControl("McDDList1")).Text.ToString();

				((ddList.mcDDList)e.Item.FindControl("McDDList1")).ReturnsValue = false;
				itemText = ((ddList.mcDDList)e.Item.FindControl("McDDList1")).Text.ToString();

				//write your update query
				//update table set col1 = itemtext where col2 = itemvalue
				jskDataGrid.EditItemIndex = -1;
				BindDataGrid();

			}
			catch(Exception ex)
			{
				Response.Write(ex.Message);

			}
			finally{
				/*Close your connection */
			}
		}
		public DataSet PopulateDDList()
		{
			string sqlString = " select ProductID,ProductName,CategoryName as Name,UnitPrice as Price " +
										"from Products p inner join Categories c " +
										"on p.categoryid = c.categoryid ";
			SqlDataAdapter ad = new SqlDataAdapter(sqlString,SqlCon);
			DataSet ds = new DataSet();
			ad.Fill(ds,"Categories");
			return ds;
		}

		#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.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I'm a .Net Programmer/Web Developer, mostly working on ASP.Net C# and VB.Net,Oracle,SQL server 2000.

Comments and Discussions