Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on C# webforms GridView change embedded SQL statement


I have an C# web forms project with the following GridView, Is it possible to change the SQL statement on a button click

                                        <asp:TemplateField HeaderText="Broker Follow Up Required" ItemStyle-HorizontalAlign="Left" ItemStyle-Wrap="false" SortExpression="[Broker Follow Up Required]" ControlStyle-Width="80">


                                                     <asp:Label ID="lblBroker_Follow_Up_Required" runat="server" Size="65" Text='<%# Bind("[Broker Follow Up Required]")%>' ToolTip="Type"></asp:Label>


                                               <ItemStyle HorizontalAlign="Left" Wrap="True" />




                                          <EditRowStyle BackColor="#2461BF" />

                                          <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                                          <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                                          <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

                                          <RowStyle BackColor="#EFF3FB" />

                                          <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

                                          <SortedAscendingCellStyle BackColor="#F5F7FB" />

                                          <SortedAscendingHeaderStyle BackColor="#6D95E1" />

                                          <SortedDescendingCellStyle BackColor="#E9EBEF" />

                                          <SortedDescendingHeaderStyle BackColor="#4870BE" />


                                   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"

                                       SelectCommand="SELECT * FROM [Brokers] Where Archived IS NULL" FilterExpression="[Broker Company] LIKE '{0}%' Or [Broker Full Name] LIKE '{0}%'

                                       Or [Email] LIKE '{0}%' Or [Type] LIKE '{0}%'" >


                                                <asp:ControlParameter Name="Search" ControlID="txtSearch" PropertyName="Text" />



Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dfke


Like slightwv mentioned this can be achieved by using the SelectCommand property of the SqlDataSource control and updating its value in the code-behind.

Something like:

SqlDataSource1.SelectCommand = "YOUR NEW SQL STATEMENT";

Open in new window

I guess thanks for posting pretty much the exact same code provided in the link I posted, even down to the SqlDataSource1 and GridView1 object names?
Avatar of Murray Brown