再来50分,datagrid里的问题,高手请进
有一datagrid,
其中一欄:
<asp:TemplateColumn HeaderText= "業類 ">
<HeaderStyle Width= "40px "> </HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ywdesp ")%>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist ID= "yk " runat= "server ">
<asp:listitem Value= "0 " Text= "其它 "/>
<asp:listitem Value= "1 " Text= "內一 "/>
<asp:listitem Value= "2 " Text= "內二 "/>
<asp:listitem Value= "3 " Text= "外銷 "/>
<asp:listitem Value= "4 " Text= "行銷 "/>
</asp:dropdownlist>
</EditItemTemplate>
</asp:TemplateColumn>
在非編輯狀態時,顯示“其它”, "內一 ", "內二 ", "外銷 ", "行銷 "之類的,但我想在編輯狀態時,是“其它”時yk(DropDownList)的SelectedIndex=0, "內一 "時SelectedIndex=1,......
但我用下面代碼,運行不成功,請幫忙,謝謝!
public void gridUserItemDataBound(object sender,DataGridItemEventArgs e)
{
if(e.Item.ItemType!=ListItemType.Header)
{
switch(e.Item.Cells[3].Text)
{
case "其它 ":
deptIndex=0;
break;
case "內一 ":
deptIndex=1;
break;
case "內二 ":
deptIndex=2;
break;
case "外銷 ":
deptIndex=3;
break;
case "行銷 ":
deptIndex=4;
break;
}
if(e.Item.ItemType==ListItemType.EditItem)
{
DropDownList myddl=(DropDownList)e.Item.FindControl( "yk ");
myddl.SelectedIndex=deptIndex;
}
}
}
------解决方案--------------------public void gridUserItemDataBound(object sender,DataGridItemEventArgs e)
{ int deptIndex;
if(e.Item.ItemType!=ListItemType.Header)
{
switch(dt.rows[e.item.itemindex].[你的字段名 "])
{
case "其它 ":
deptIndex=0;
break;
case "內一 ":
deptIndex=1;
break;
case "內二 ":
deptIndex=2;
break;
case "外銷 ":
deptIndex=3;
break;
case "行銷 ":
deptIndex=4;
break;
}
if(e.Item.ItemType==ListItemType.EditItem)
{
DropDownList myddl=(DropDownList)e.Item.FindControl( "yk ");