[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3193
  • Last Modified:

Specifically databinding a dropdownlist within a gridview

I have an ASP.NET web form with a gridview being dynamically populated by a datasource. There are three columns in the gridview, the first Column1, is an ID column which is hidden from view (ProgramID). The second, Column2, contains a name, which when in edit mode, converts to a textbox (ProgramName). The third (and the piece I need help with), Column3,  lists a Name (SiteName), and when in edit mode, produces a dropdownlist. The selected value of this dropdownlist is bound to the ID of this name (not the name itself - this is SiteID). All of this comes out of the stored proc tied to the select function for the gridview.

Now here is where I need help. Column3, when edited, turns into a drop down list. This drop down list needs to display Site Names and IDs, which are contained in a different stored procedure. So, while the overall gridview is filling from one stored procedure, along with the actual current valuie of Column3, I want the user to be able to edit that 3rd column, and have their list of available choices come from the filling of that dropdownlist from another stored procedure. In less complicated terms, **can you individually bind elements within an edittemplate to stored procedures that are not the primary select procedure**? The IDs that would populate this dropdown list would obviously correspond to the update ID that would go in the update procedure, I dont need to fire a separate stored proc for that.

Below is my code, hopefully it makes sense. Everything works EXCEPT the dropdown list for column 3 populating. (this is ddlSiteAssociation)

<asp:GridView ID="gvCurrentPrograms" runat="server"  AutoGenerateColumns="False" visible="true"
          ShowHeader="True" BorderColor="#000000" BorderStyle="solid" BorderWidth="1px" Width="500px"
           DataSourceID="dsPrograms" DataKeyNames="ProgramID, SiteID" RowStyle-BackColor="#def5f7"
            AlternatingRowStyle-BackColor="#c5edf8" HeaderStyle-BackColor="#93b4bd" CssClass="gv">
        <EmptyDataTemplate>
       <table width="498px"><tr><td style="background-color:White;"><img src="images/caution.gif" /> No programs currently set for that site.</td></tr></table>
        </EmptyDataTemplate>
        <Columns>
            <asp:CommandField ShowEditButton="true" ButtonType="Button" />
            <asp:BoundField DataField="ProgramID" HeaderText="ID" ReadOnly="true" Visible="false"/>
              <asp:TemplateField HeaderText="Program Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProgramName" runat="server" Text='<%#Bind("ProgramName") %>'></asp:TextBox>
                    </EditItemTemplate>
                        <ItemTemplate>
                        <asp:Label ID="lblProgramName" runat="server" Text='<%# Eval("ProgramName") %>' />
                    </ItemTemplate>
               </asp:TemplateField>
            <asp:TemplateField HeaderText="Site Association">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlSiteAssociation" runat="server" SelectedValue='<%#Bind("SiteID") %>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                        <ItemTemplate>
                        <asp:Label ID="lblSiteAssociation" runat="server" Text='<%# Eval("SiteName") %>' />
                    </ItemTemplate>
               </asp:TemplateField>
        </Columns>
        </asp:GridView>
        <asp:sqlDataSource ID="dsPrograms"
        SelectCommandType="StoredProcedure"
        SelectCommand = "uspGetCurrentPrograms"
        UpdateCommandType="StoredProcedure"
        UpdateCommand = "uspUpdateProgram"
        ConnectionString="<%$ ConnectionStrings:Conn %>"
        RunAt="server">  
        <SelectParameters>
        <asp:Parameter Name="SiteID" Type="Int32" DefaultValue="" />
        </SelectParameters>
</asp:sqlDataSource>

0
nuvium
Asked:
nuvium
  • 2
  • 2
  • 2
1 Solution
 
Bob LearnedCommented:
In the RowDataBound or RowCreated, you could get a reference to the DropDownListControl, and data-bind it from there.

Bob
0
 
RejojohnyCommented:
try this

<asp:TemplateField HeaderText="Site Association">
                    <EditItemTemplate>
<asp:DropDownList ID="ddlSiteAssociation" runat="server" SelectedValue='<%#Bind("SiteID") %>'
DataSource='<%# GetSites()%>' DataValueField="SiteID" DataTextField="SiteName">


in the .vb page

protected function GetSites() as datatable
    SqlDataAdapter SqlDa = new SqlDataAdapter("Select SiteId, SiteName from YourSiteTable", YourSqlCon)
    DataSet ds = new DataSet()
    SqlDa.Fill(ds, "SiteList")
    return ds.Tables("SiteList")
End Sub


Rejo
0
 
Bob LearnedCommented:
Realize, though, that that method will be called for every single DropDownList in every row, so it wouldn't be optimized.

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
nuviumAuthor Commented:
I see where you are going with this, and I understand why its going to call it on each row. Is there a way to sort of call the fill procedure once, and then repeat the outcome per row? With datasets or something? I'm a bit new to programming this way, so if what I said didnt make sense, let me know.
0
 
RejojohnyCommented:
no, I do not think it will be called for every single row .. its within the edititemtemplate and so will processed and appears only for the row been edited i.e when the "edit" is clicked .. so processed for one row per edit ..
nuvium, you can validate this by putting a breakpoint in GetSites and verify when and how many times the function is called ..

>> Is there a way to sort of call the fill procedure once
Even though the code will be called only for the edit column, this would be called everytime the user clicks on the edit button or when multiple users use the same page.. if you want to optimise the code so that the database is not hit everytime the user opts to edit, you can cache the "SiteList" datatable and read from the cache instead of the database ..

Rejo
0
 
nuviumAuthor Commented:
Rejo -

I modified my code per your instrucitons, and so far it LOOKS like its working fine. I am going to test out a few things and make sure its working with my update statement correctly, but I believe you helped solve my problem. And you are right, its only firing that GetSites query on the row thats being updated.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now