Results 1 to 6 of 6

Thread: [02/03] setting dynamic dropdownlist in datagrid (asp.net - vb)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    [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:
    1. <asp:datagrid id="dgrdAccountInfo" runat="server" ShowFooter="True" AutoGenerateColumns="False" Width="850px" HorizontalAlign="Center" OnEditCommand="dgrdAccountInfo_EditCommand"
    2.                     OnCancelCommand="dgrdAccountInfo_CancelCommand"
    3.                     OnUpdateCommand="dgrdAccountInfo_UpdateCommand" OnDeleteCommand="dgrdAccountInfo_DeleteCommand"
    4.                     DataKeyField="Account_ID" OnItemCommand="doInsert">
    5.                             <AlternatingItemStyle Font-Size="X-Small" Font-Names="Arial" BackColor="#ECECEC"></AlternatingItemStyle>
    6.                             <ItemStyle Font-Size="X-Small" Font-Names="Arial" VerticalAlign="Top" BackColor="White"></ItemStyle>
    7.                             <HeaderStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="#4A7184"></HeaderStyle>
    8.                             <Columns>
    9. '---------- REMOVED COLUMNS ----------------
    10. <asp:TemplateColumn HeaderText="SCAC" SortExpression="ABCD">
    11.     <HeaderStyle HorizontalAlign="Center" Width="75px"></HeaderStyle>
    12.     <ItemStyle Font-Size="X-Small" HorizontalAlign="Center"></ItemStyle>
    13.     <ItemTemplate>
    14.         <asp:Label id= "lblABCD" runat= "server" Text= '<%# Container.DataItem("ABCD") %>'>
    15.         </asp:Label>
    16.     </ItemTemplate>
    17.     <EditItemTemplate>                            
    18.         <asp: dropDownList runat="server" id="ddlABCD_edit" DataValueField="CarrierID" DataTextField="ABCD" DataSource='<%# GetABCD() %>' />
    19.     </EditItemTemplate>
    20.     <FooterTemplate>
    21.         <asp: dropDownList id="ddlABCD_add" runat="server"></asp: dropDownList>
    22.     </FooterTemplate>
    23. </asp:TemplateColumn>
    24. '---------- REMOVED COLUMNS ----------------
    25. <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Updt" HeaderText="Edit" ButtonType="PushButton">
    26.                                     <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    27.                                 </asp:EditCommandColumn>
    28. </Columns>
    29. </asp:datagrid>

    Code from the codebehind
    VB Code:
    1. 'Create a connection
    2. Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("company.DB"))
    3. Dim ddlDataSet As DataSet = New DataSet()
    4.  
    5. Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    6.         dgrdAccountInfo.ShowFooter = False
    7.         dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex
    8.         sbFillDataGrid()
    9. End Sub
    10.  
    11. Function GetABCD() As DataSet
    12.     'Populate the ddlDataSet
    13.     Const sSQL As String = "SELECT CarrierID, ABCD FROM tblTABLENAME ORDER BY ABCD"
    14.  
    15.    Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(sSQL, myConnection)
    16.  
    17.    myDataAdapter.Fill(ddlDataSet, "tblTABLENAME")
    18.  
    19.    Return ddlDataSet
    20. End Function

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    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:
    1. Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    2.         dgrdAccountInfo.ShowFooter = False
    3.         dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex
    4.         sbFillDataGrid()
    5.  
    6.         Dim ddlCarrierName As DropDownList
    7.         ddlCarrierName = e.Item.FindControl("ddlCarrierName_edit")
    8.         'Dim ListItem li = ddlCarrierName_add.FindByString("value you want seleted')
    9.         Dim li As ListItem
    10.         li = ddlCarrierName.Items.FindByText("Green")
    11.         li.Selected = True
    12.     End Sub
    Last edited by lleemon; Aug 30th, 2006 at 10:37 AM. Reason: Got initial error resolved

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    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:
    1. Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    2.         dgrdAccountInfo.ShowFooter = False
    3.         dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex
    4.         sbFillDataGrid()
    5.  
    6.         Dim ddlCarrierName As DropDownList
    7.         ddlCarrierName = e.Item.FindControl("ddlCarrierName_edit")
    8.         'Dim ListItem li = ddlCarrierName_add.FindByString("value you want seleted')
    9.         Dim li As ListItem
    10.         li = ddlCarrierName.Items.FindByText("Green")
    11.         li.Selected = True
    12.     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")

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    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:
    1. Private Sub dgrdAccountInfo_ItemDataBound(ByVal sender As System.Object, ByVal e As DataGridItemEventArgs) Handles dgrdAccountInfo.ItemDataBound
    2.         If e.Item.ItemType = ListItemType.EditItem Then
    3.             Dim ddlCarrierName As DropDownList
    4.             ddlCarrierName = e.Item.FindControl("ddlCarrierName_edit")
    5.             Dim li As ListItem
    6.             li = ddlCarrierName.Items.FindByText("Green")
    7.             li.Selected = True
    8.         End If
    9.     End Sub

    If I have
    Red
    Green
    Black

    and the item editing is Red it currently only sets it to Green.

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width