P
Peter
ASP.NET 2003
In the DataGrid how do I select current cell value in the dropdown box when
I click on the edit link, currently when I click on the edit link in the
DataGrid the dropdown box appears in the cell, but allways the first item in
the dropdown box is shown not the current cell value?
How do I make the current value in the cell automaticaly be selected in the
dropdown box.
// Code Behind
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 DataGridDemo
{
/// <summary>
/// Summary description for WebForm4.
/// </summary>
public class Dropdown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}
private void BindData()
{
// Select from the authors table of the pubs database
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Employees",
sqlConnection1);
sqlCmd.CommandType = CommandType.Text;
sqlConnection1.Open();
// Set the data source for this datagrid to the datareader
// returned from the ExecuteReader method
DataGrid1.DataSource = sqlCmd.ExecuteReader();
// Bind the datagrid to the datasource
DataGrid1.DataBind();
sqlConnection1.Close();
}
public ICollection LoadNames()
{
// A new connection must be created because the one
// used for binding the DataGrid is still in use.
SqlConnection sqlNewConnection = new SqlConnection();
sqlNewConnection.ConnectionString = sqlConnection1.ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("SELECT FirstName FROM Employees",
sqlNewConnection);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0].DefaultView;
}
#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.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=DEVCON4SVR;packet
size=4096;user id=sa;data source=DEVCON4SVR;pers" +
"ist security info=False;initial catalog=Northwind";
this.sqlConnection1.InfoMessage += new
System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnCancel);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnEdit);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdate);
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void OnEdit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.DataSetIndex;
BindData();
}
private void OnUpdate(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// Find the drop down list
DropDownList DropList = (DropDownList)e.Item.FindControl("dlNames");
string strTest = DropList.SelectedItem.Text;
// Process updated data here
DataGrid1.EditItemIndex = -1;
BindData();
}
private void OnCancel(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}
private void sqlConnection1_InfoMessage(object sender,
System.Data.SqlClient.SqlInfoMessageEventArgs e)
{
}
private void DataGrid1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
}
}
}
// ASPX page
<%@ Page language="c#" Codebehind="Dropdown.aspx.cs" AutoEventWireup="false"
Inherits="DataGridDemo.Dropdown" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta content="Microsoft Visual Studio 7.0" 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="WebForm3" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 11px; POSITION:
absolute; TOP: 45px" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="First Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.FirstName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist ID="dlNames" DataSource='<%# LoadNames() %>'
DataTextField="FirstName" runat="server">
</asp:dropdownlist>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.city") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.city") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid> </form>
</body>
</HTML>
In the DataGrid how do I select current cell value in the dropdown box when
I click on the edit link, currently when I click on the edit link in the
DataGrid the dropdown box appears in the cell, but allways the first item in
the dropdown box is shown not the current cell value?
How do I make the current value in the cell automaticaly be selected in the
dropdown box.
// Code Behind
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 DataGridDemo
{
/// <summary>
/// Summary description for WebForm4.
/// </summary>
public class Dropdown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}
private void BindData()
{
// Select from the authors table of the pubs database
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Employees",
sqlConnection1);
sqlCmd.CommandType = CommandType.Text;
sqlConnection1.Open();
// Set the data source for this datagrid to the datareader
// returned from the ExecuteReader method
DataGrid1.DataSource = sqlCmd.ExecuteReader();
// Bind the datagrid to the datasource
DataGrid1.DataBind();
sqlConnection1.Close();
}
public ICollection LoadNames()
{
// A new connection must be created because the one
// used for binding the DataGrid is still in use.
SqlConnection sqlNewConnection = new SqlConnection();
sqlNewConnection.ConnectionString = sqlConnection1.ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("SELECT FirstName FROM Employees",
sqlNewConnection);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0].DefaultView;
}
#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.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=DEVCON4SVR;packet
size=4096;user id=sa;data source=DEVCON4SVR;pers" +
"ist security info=False;initial catalog=Northwind";
this.sqlConnection1.InfoMessage += new
System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnCancel);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnEdit);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdate);
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void OnEdit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.DataSetIndex;
BindData();
}
private void OnUpdate(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// Find the drop down list
DropDownList DropList = (DropDownList)e.Item.FindControl("dlNames");
string strTest = DropList.SelectedItem.Text;
// Process updated data here
DataGrid1.EditItemIndex = -1;
BindData();
}
private void OnCancel(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}
private void sqlConnection1_InfoMessage(object sender,
System.Data.SqlClient.SqlInfoMessageEventArgs e)
{
}
private void DataGrid1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
}
}
}
// ASPX page
<%@ Page language="c#" Codebehind="Dropdown.aspx.cs" AutoEventWireup="false"
Inherits="DataGridDemo.Dropdown" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta content="Microsoft Visual Studio 7.0" 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="WebForm3" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 11px; POSITION:
absolute; TOP: 45px" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="First Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.FirstName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist ID="dlNames" DataSource='<%# LoadNames() %>'
DataTextField="FirstName" runat="server">
</asp:dropdownlist>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.LastName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.city") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.city") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid> </form>
</body>
</HTML>