The following example code details how to use the .NET datagrid edit features with a dropdownlist.
<%@ Page language=”c#” Codebehind=”WebForm1.aspx.cs” AutoEventWireup=”false” Inherits=”eform.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=”https://schemas.microsoft.com/intellisense/ie5″ name=”vs_targetSchema”>
</HEAD>
<body>
<form id=”Form1″ method=”post” runat=”server”>
<TABLE id=”Table1″ cellSpacing=”1″ cellPadding=”1″ width=”300″ border=”1″>
<TR>
<TD>
<asp:Label id=”Label1″ runat=”server”>Label</asp:Label></TD>
</TR>
<TR>
<TD><asp:datagrid id=”DataGrid1″ runat=”server” OnUpdateCommand=”DataGrid_Update” OnCancelCommand=”DataGrid_Cancel” OnEditCommand=”DataGrid_Edit” AutoGenerateColumns=”False”>
<Columns>
<asp:BoundColumn HeaderText=”Employee ID” DataField=”emp_id” ReadOnly=”True” ItemStyle-Width=150 HeaderStyle-Wrap=False ItemStyle-Wrap=”False” />
<asp:BoundColumn HeaderText=”Last Name” DataField=”lname” ItemStyle-Width=200 HeaderStyle-Wrap=False ItemStyle-Wrap=”False” />
<asp:BoundColumn HeaderText=”First Name” DataField=”fname” ItemStyle-Width=200 HeaderStyle-Wrap=False ItemStyle-Wrap=”False” />
<asp:TemplateColumn HeaderText=”Job Title” HeaderStyle-Wrap=False ItemStyle-Width=200 ItemStyle-Wrap=”False”>
<ItemTemplate>
<asp:Label ID=”JobTitle” Runat=”server” Text='<%# DataBinder.Eval(Container.DataItem, “job_desc”) %>’ />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID=”ddlJobTitle” Runat=”server” DataSource=”<%# GetJobDataTable() %>” DataTextField=”job_desc” DataValueField=”job_id” />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType=”LinkButton” CancelText=”Cancel” EditText=”Edit” UpdateText=”Save” />
</Columns>
</asp:datagrid></TD>
</TR>
<TR>
<TD></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
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 Microsoft.ApplicationBlocks.Data;
namespace eform
{
///
///
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
GetData();
BindData();
}
}
private void GetData()
{
string ConnString = ConfigurationSettings.AppSettings[“PUBSCONNSTRING”];
DataSet ds = SqlHelper.ExecuteDataset(ConnString,”pr_GetEmployees”);
Session[“EmployeeDataTable”] = ds.Tables[0];
Session[“JobsDataTable”]= ds.Tables[1];
}
private void BindData()
{
DataTable dt = (DataTable) Session[“EmployeeDataTable”];
DataGrid1.DataSource = dt;
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);
}
///
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected void DataGrid_Cancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}
protected void DataGrid_Delete(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
protected void DataGrid_Edit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
protected void DataGrid_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string ConnString = ConfigurationSettings.AppSettings[“PUBSCONNSTRING”];
string val2 = e.Item.Cells[0].Text;
string val3 = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string val4 = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
int val5 = Int32.Parse(((DropDownList)e.Item.Cells[3].Controls[1]).SelectedItem.Value);
SqlParameter[] arParams = new SqlParameter[4];
arParams[0] = new SqlParameter(“@EMP_ID”, e.Item.Cells[0].Text);
arParams[1] = new SqlParameter(“@FNAME”, ((TextBox)e.Item.Cells[1].Controls[0]).Text);
arParams[2] = new SqlParameter(“@LNAME”, ((TextBox)e.Item.Cells[2].Controls[0]).Text);
arParams[3] = new SqlParameter(“@JOB_ID”, Int32.Parse(((DropDownList)e.Item.Cells[3].Controls[1]).SelectedItem.Value));
try
{
SqlHelper.ExecuteNonQuery(ConnString,”pr_UpdateEmployee”,arParams);
}
catch(SqlException sqlEx)
{
Label1.Text = sqlEx.Message.ToString();
}
DataGrid1.EditItemIndex = -1;
GetData();
BindData();
}
private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DataRowView objDataRowView = (DataRowView)e.Item.DataItem;
string CurrentJobDesc = (string)objDataRowView[4].ToString();
DropDownList ctlDropDownList = (DropDownList)e.Item.FindControl(“ddlJobTitle”);
ctlDropDownList.SelectedIndex = ctlDropDownList.Items.IndexOf(ctlDropDownList.Items.FindByText(CurrentJobDesc));
}
}
protected DataTable GetJobDataTable()
{
return (DataTable) Session[“JobsDataTable”];
}
}
}