Hi all,
I know this has been posted before, but can't seem to get it right. I have a datagrid which looks like the following:
What I am trying to do is to on edit ensure that the value is selected in the dropdown list item. I know there are a few ways of achieving this, but for some reason, I am getting mixed up. I tried this:
which doesn't work. I can read the value of the column/row, but am unable to set that dropdown to the value have from the database. I have tried populating the selected index from the web page using databinder.eval, but i get an error doing that as well.
I am not sure, but maybe it is because I can't access the control in edititemtemplate? Any help?
(i only put the dropdown column in my grid code sample, i have a whole bunch of other columns, that is why e.item.cells(4).text shows 4, it is the 5th column in my grid)
I know this has been posted before, but can't seem to get it right. I have a datagrid which looks like the following:
Code:
<asp:DataGrid ID="dgrdDataUpdates" Runat="server"
AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True"
DataKeyField="vchFieldValue" ShowFooter="True"
OnEditCommand="dgrdDataUpdates_EditCommand"
OnCancelCommand="dgrdDataUpdates_CancelCommand"
OnUpdateCommand="dgrdDataUpdates_UpdateCommand"
OnItemCommand="dgrdDataUpdates_ItemCommand">
<Columns>
<asp:TemplateColumn HeaderText="Translated Source" >
<FooterTemplate>
<asp:DropDownList ID="cboAddSys" AutoPostBack="True" Runat="server"> <asp:ListItem Value="P">P</asp:ListItem>
<asp:ListItem Value="M">M</asp:ListItem>
<asp:ListItem Value="B" Selected=True>Both</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<%# Container.DataItem("chrTranslatedSource") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cboSys" AutoPostBack="True" Runat="server">
<asp:ListItem Value="P">P</asp:ListItem>
<asp:ListItem Value="M">M</asp:ListItem> <asp:ListItem Value="B">B</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Code:
Dim ddlPaySys As DropDownList = CType(e.Item.FindControl("cboSys"), DropDownList)
Dim ddlPaySysValue = ddlPaySys.SelectedIndex
ddlPaySys.Items.FindByValue(e.Item.Cells(4).Text).Selected = True
which doesn't work. I can read the value of the column/row, but am unable to set that dropdown to the value have from the database. I have tried populating the selected index from the web page using databinder.eval, but i get an error doing that as well.
I am not sure, but maybe it is because I can't access the control in edititemtemplate? Any help?
(i only put the dropdown column in my grid code sample, i have a whole bunch of other columns, that is why e.item.cells(4).text shows 4, it is the 5th column in my grid)