Set inital value of dropdown in a datagrid

Last post 05-12-2008 10:47 AM by Kriswd40. 5 replies.

Sort Posts:

  • Set inital value of dropdown in a datagrid

    05-07-2008, 10:34 AM
    • Loading...
    • Kriswd40
    • Joined on 06-05-2007, 9:44 AM
    • Posts 218

    I have a dropdownlist in a datagrid and want to set the initial value of said droplist. I populate the elements in the dropdownlist in the ItemDataBound event.

    <asp:DataGrid ID="IssuesDataGrid" runat="server">
     <Columns>  


        <asp:TemplateColumn HeaderText="Resolution">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlResolution"
                                                skinID="smallDropDown"
                                                runat="server"
                                                Enabled="true"
                                                
                                                />
                                        </ItemTemplate>
                                    </asp:TemplateColumn>

     </Columns>
    </asp:DataGrid>

    I am wondering how to go about setting the initial selected value of the datagrid. I want to set it to the value of resolutionID, which is in the query that populates the datagrid.

    Is there any easy way to put that right into the aspx page where I create the dropdownlist?

    Or is there some way in my ItemDataBound event to access the value of resolutionID, which is coming from the database?

    My third option would be to add a bound column to the datagrid with this initial value... but I'd like to find a way to do this without this extra column if possible.

  • Re: Set inital value of dropdown in a datagrid

    05-08-2008, 4:51 AM
    • Loading...
    • DigiMortal
    • Joined on 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 308
    You have to handle RowDataBound or RowCreated event in your code. There you can find current row's ddlresolution, populate it with data and assign selectedvalue.
    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: Set inital value of dropdown in a datagrid

    05-08-2008, 9:13 AM
    • Loading...
    • Kriswd40
    • Joined on 06-05-2007, 9:44 AM
    • Posts 218

    I know how to set the values from itemDataBound, but how do I get the value to set it from there???

    I don't want to have to write another function that grabs it from the database, is there a way to access this value from the event without doing either another database access or having to use a hidden column in my datagrid?

  • Re: Set inital value of dropdown in a datagrid

    05-09-2008, 4:43 PM
    • Loading...
    • Kriswd40
    • Joined on 06-05-2007, 9:44 AM
    • Posts 218

    I added a hidden column to my datagrid and then just pulled the value from there. I would think there would be a cleaner way to do it, but it works at least. Let me know if anyone has any better solutions!

  • Re: Set inital value of dropdown in a datagrid

    05-10-2008, 7:49 AM
    • Loading...
    • DigiMortal
    • Joined on 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 308

    To populate your DropDownLIst you have to get data from somewhere. You may ask DropDownList data before binding grid to data. Just ask DataTable or DataSet and hold it in some attribute of your Page or UserControl class. This way you can also bind more than one DropDownList with data and you don't need to ask this data from database for each row.

    When grid is creating rows you can catch events and ask for data object that is bounded to current row. If you define data key for grid you can ask current row's data key and set is as DropDownList's selected value.

    This is the way how you can do it correctly.

    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: Set inital value of dropdown in a datagrid

    05-12-2008, 10:47 AM
    • Loading...
    • Kriswd40
    • Joined on 06-05-2007, 9:44 AM
    • Posts 218

    DigiMortal:

    To populate your DropDownLIst you have to get data from somewhere. You may ask DropDownList data before binding grid to data. Just ask DataTable or DataSet and hold it in some attribute of your Page or UserControl class. This way you can also bind more than one DropDownList with data and you don't need to ask this data from database for each row.

    That is what I am doing now, I load all my data into a class on page load and then populate the dropdownlists with this class.

    When grid is creating rows you can catch events and ask for data object that is bounded to current row. If you define data key for grid you can ask current row's data key and set is as DropDownList's selected value.

    This is the way how you can do it correctly.

    I ussually like to set the dataKey as the unique key for that particular row, which wouldn't be the same as the selected value for my dropdown. I guess if the only way to access the value is this way, I feel better about using the extra columns as I have been doing, as you can only save one dataKey and I would rather have that set as the unique key in the event i do use it, otherwise I'll end up confusing myself if I come back to this app later :)

Page 1 of 1 (6 items)