[02/03] setting dynamic dropdownlist in datagrid (asp.net - vb)
I am using vs2002 and am able to populate a dropdownlist dynamically in a datagrid when I click the edit button. My next issue is in Edit mode to have the value selected in the EditItemTemplate dropdownlist called ddlABCD_edit.
I have provided what I think is all the code used for this column. Anyone have a sample or see an easy fix for me?
If I have the following values:
Red
Green
Blue
and for that record in the datagrid is Green, I need Green to be selected in the dropdownlist.
VB Code:
<asp:datagrid id="dgrdAccountInfo" runat="server" ShowFooter="True" AutoGenerateColumns="False" Width="850px" HorizontalAlign="Center" OnEditCommand="dgrdAccountInfo_EditCommand"
OnCancelCommand="dgrdAccountInfo_CancelCommand"
OnUpdateCommand="dgrdAccountInfo_UpdateCommand" OnDeleteCommand="dgrdAccountInfo_DeleteCommand"
DataKeyField="Account_ID" OnItemCommand="doInsert">
<AlternatingItemStyle Font-Size="X-Small" Font-Names="Arial" BackColor="#ECECEC"></AlternatingItemStyle>
<ItemStyle Font-Size="X-Small" Font-Names="Arial" VerticalAlign="Top" BackColor="White"></ItemStyle>
<HeaderStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="#4A7184"></HeaderStyle>
<Columns>
'---------- REMOVED COLUMNS ----------------
<asp:TemplateColumn HeaderText="SCAC" SortExpression="ABCD">
<HeaderStyle HorizontalAlign="Center" Width="75px"></HeaderStyle>
<ItemStyle Font-Size="X-Small" HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label id= "lblABCD" runat= "server" Text= '<%# Container.DataItem("ABCD") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp: dropDownList runat="server" id="ddlABCD_edit" DataValueField="CarrierID" DataTextField="ABCD" DataSource='<%# GetABCD() %>' />
</EditItemTemplate>
<FooterTemplate>
<asp: dropDownList id="ddlABCD_add" runat="server"></asp: dropDownList>
</FooterTemplate>
</asp:TemplateColumn>
'---------- REMOVED COLUMNS ----------------
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Updt" HeaderText="Edit" ButtonType="PushButton">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:EditCommandColumn>
</Columns>
</asp:datagrid>
Code from the codebehind
VB Code:
'Create a connection
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("company.DB"))
Dim ddlDataSet As DataSet = New DataSet()
Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
dgrdAccountInfo.ShowFooter = False
dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex
sbFillDataGrid()
End Sub
Function GetABCD() As DataSet
'Populate the ddlDataSet
Const sSQL As String = "SELECT CarrierID, ABCD FROM tblTABLENAME ORDER BY ABCD"
Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(sSQL, myConnection)
myDataAdapter.Fill(ddlDataSet, "tblTABLENAME")
Return ddlDataSet
End Function
Re: [02/03] setting dynamic dropdownlist in datagrid (asp.net - vb)
where you populate the drop down (assuming on onitemdatabound) right after you fill the drop down, u can do this
ListItem li =dll.FindByStrin("value you want seleted')
li.Selected = true
Re: [02/03] setting dynamic dropdownlist in datagrid (asp.net - vb)
So I am guessing you mean in the EditCommand. I have changed my EditCommand as shown below but my dropdownlist doesn't have the option of FindBySting available but did find a .Item.FindByText.
Now I get the following at li = dlCarrierName.Item.....
Object reference not set to an instance of an object.
VB Code:
Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
dgrdAccountInfo.ShowFooter = False
dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex
sbFillDataGrid()
Dim ddlCarrierName As DropDownList
ddlCarrierName = e.Item.FindControl("ddlCarrierName_edit")
'Dim ListItem li = ddlCarrierName_add.FindByString("value you want seleted')
Dim li As ListItem
li = ddlCarrierName.Items.FindByText("Green")
li.Selected = True
End Sub
Re: [02/03] setting dynamic dropdownlist in datagrid (asp.net - vb)
Quote:
Originally Posted by lleemon
So I am guessing you mean in the EditCommand. I have changed my EditCommand as shown below but my dropdownlist doesn't have the option of FindBySting available but did find a .Item.FindByText.
Now I get the following at li = dlCarrierName.Item.....
Object reference not set to an instance of an object.
VB Code:
Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
dgrdAccountInfo.ShowFooter = False
dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex
sbFillDataGrid()
Dim ddlCarrierName As DropDownList
ddlCarrierName = e.Item.FindControl("ddlCarrierName_edit")
'Dim ListItem li = ddlCarrierName_add.FindByString("value you want seleted')
Dim li As ListItem
li = ddlCarrierName.Items.FindByText("Green")
li.Selected = True
End Sub
no i dont mean the edit command,
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
}
this event is called for each row you are binding,
and in this you check to see what type of row it is, if its in edit mode this is where you shoudl be populating the dropdown and selecting
and here is the select method. sorry its FindByText and not String and its on the item collection
ListItem li = DropDownList1.Items.FindByText("something")
Re: [02/03] setting dynamic dropdownlist in datagrid (asp.net - vb)
Finally am able to get something other then the first item selected with the help from Kovan.
One last thing, how do I get the value of that cell before it is in edit mode?
code added to get working:
VB Code:
Private Sub dgrdAccountInfo_ItemDataBound(ByVal sender As System.Object, ByVal e As DataGridItemEventArgs) Handles dgrdAccountInfo.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim ddlCarrierName As DropDownList
ddlCarrierName = e.Item.FindControl("ddlCarrierName_edit")
Dim li As ListItem
li = ddlCarrierName.Items.FindByText("Green")
li.Selected = True
End If
End Sub
If I have
Red
Green
Black
and the item editing is Red it currently only sets it to Green.
Re: [02/03] setting dynamic dropdownlist in datagrid (asp.net - vb)
you could use a hiddent textbox that will always have the value in it even in the edit mode