Hi localhost,
Welcome to Microsoft Newsgroup Service. Based on your problem description,
you have a DataGrid with a Template column, in which, the ItemTemplate is a
Lable and the EditItemTemplat e is a DropDownList, and you want to set the
DropDownList's selectedIndex according to the value when show in the
Label(not in edit mode), is my understanding correct?
I've tested the code you provided, I found that there are something
incorrect when run it. Such as :
DataRowView ddView = eA.Item.DataIte m;
DropDownList ddTemp = eA.Item.FindCon trol
( "ddnChoiceI d" );
the DropDownList hasn't been created at the DataGrid's EditCommand event,
so we are unable to retrieve it using the "FindContro l". Don't worry,
though we can't retrieve the DropDownList in the DataGrid's EditCommand
event, there is another way we initialize its value. You can specify a
"OnLoad" event handler for the DropDownList, for example:
<asp:TemplateCo lumn>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"gender") %>
</ItemTemplate>
<EditItemTempla te>
<asp:DropDownLi st ID="lstGender" OnLoad="lstGend er_Load" Runat="server">
<asp:ListItem Value="male">Ma le</asp:ListItem>
<asp:ListItem Value="female"> Female</asp:ListItem>
</asp:DropDownLis t>
</EditItemTemplat e>
</asp:TemplateCol umn>
Then, implement this handler function("lstGe nder_Load") in the Page Class:
protected void lstGender_Load( object source, System.EventArg s e)
{
DropDownList lstGender = (DropDownList)s ource;
//get the datasource of the DataGrid(you should change it to other
//if you don't use DataTAble as the datasource)
DataTable tb = (DataTable)grid Test.DataSource ;
DataRow row = tb.Rows[gridTest.EditIt emIndex];
if(row["gender"].Equals("male") )
{
lstGender.Selec tedIndex = 0;
}
else
{
lstGender.Selec tedIndex = 1;
}
}
Then, in the DataGrid's EditCommand event, you just need to set its
EditItemIndex and rebind the data:
private void gridTest_EditCo mmand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
gridTest.EditIt emIndex = e.Item.ItemInde x;
gridTest.DataSo urce = Get_Data();//Get_Data() returns a DataTable for
testing
gridTest.DataBi nd();
}
#notice that the "gridTest_EditC ommand" is called before the
"lstGender_Load ", that also indicate that the DropDownList hasn't been
created when the DataGrid_EditCo mmand event is fired.
Please try the preceding suggestion and let me know whether they help.
Below is my test page and its page class's source
----------------DataGridEdit.as px-----------
<%@ Page language="c#" Codebehind="Dat aGridEdit.aspx. cs"
AutoEventWireup ="false" Inherits="MyWeb App.DataGridEdi t" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGrid Edit</title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="C#" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><asp:datagr id id="gridTest" runat="server"
AutoGenerateCol umns="False">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"id") %>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:TemplateCo lumn>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"name") %>
</ItemTemplate>
<EditItemTempla te>
<input type="text" value='<%#
DataBinder.Eval (Container.Data Item,"name") %>'/>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"email") %>
</ItemTemplate>
<EditItemTempla te>
<input type="text" value='<%#
DataBinder.Eval (Container.Data Item,"email") %>'/>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"gender") %>
</ItemTemplate>
<EditItemTempla te>
<asp:DropDownLi st ID="lstGender" OnLoad="lstGend er_Load"
Runat="server">
<asp:ListItem Value="male">Ma le</asp:ListItem>
<asp:ListItem Value="female"> Female</asp:ListItem>
</asp:DropDownLis t>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:EditComman dColumn ButtonType="Lin kButton" UpdateText="Upd ate"
CancelText="Can cel" EditText="Edit" ></asp:EditCommand Column>
</Columns>
</asp:datagrid></td>
</tr>
<tr>
<td><FONT face="ËÎÌå">
<asp:TextBox id="txtTest" runat="server"> </asp:TextBox></FONT>
</td>
</tr>
</table>
</form>
</body>
</HTML>
---------------DataGridEdit.as px.cs-----------------------------
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
namespace MyWebApp
{
/// <summary>
/// Summary description for DataGridEdit.
/// </summary>
public class DataGridEdit : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Text Box txtTest;
protected System.Web.UI.W ebControls.Data Grid gridTest;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
gridTest.DataSo urce = Get_Data();
gridTest.DataBi nd();
}
}
protected DataTable Get_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add( "id");
tb.Columns.Add( "name");
tb.Columns.Add( "email");
tb.Columns.Add( "gender");
for(int i=0;i<20;i++)
{
DataRow row = tb.NewRow();
row["id"] = i+1;
row["name"] = "Name" + i.ToString();
row["email"] = "Email" + i.ToString();
if(i%2 == 0)
{
row["gender"] = "male";
}
else
{
row["gender"] = "female";
}
tb.Rows.Add(row );
}
return tb;
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.gridTest.E ditCommand += new
System.Web.UI.W ebControls.Data GridCommandEven tHandler(this.g ridTest_EditCom m
and);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
protected void lstGender_Load( object source, System.EventArg s e)
{
DropDownList lstGender = (DropDownList)s ource;
DataTable tb = (DataTable)grid Test.DataSource ;
DataRow row = tb.Rows[gridTest.EditIt emIndex];
if(row["gender"].Equals("male") )
{
lstGender.Selec tedIndex = 0;
}
else
{
lstGender.Selec tedIndex = 1;
}
}
private void gridTest_EditCo mmand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
gridTest.EditIt emIndex = e.Item.ItemInde x;
gridTest.DataSo urce = Get_Data();
gridTest.DataBi nd();
}
}
}
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)