Question

I am looking to populate a (sorted) GridView (called SearchResultsGid) without the use of a data source control. I did some research and I am guessing I may need to use a data set and datable to successfully bind the the grid, but I am not entirely sure how to do it. I'm more used to using a data source control....

The grid view uses Eval statements, which I would like to keep if possible because it allows for the fields to be hyperlinks to other pages. Although, if there is another way, I would be willing to try it...

The point of the SearchResultsGrid is that, when the user types in something in a text field then hits the "search" button, a grid pops up with all relevant results from the database. There is a repeater which displays all database items that the user has added for viewing, but it is not relevant to me at the moment (it works just fine anyway).

Any help would be appreciated! :)

Here is the code:

<table cellpadding="4" cellspacing="0" class="inputForm" width="100%">
    <tr><td width="100%"><p align="justify">Select the products that should have this option.</p></td></tr>
    <tr>
        <td valign="top">
           <asp:Label ID="errorLbl" ForeColor="Red" Font-Bold="true" runat="server" Text=""></asp:Label>
            <asp:UpdatePanel ID="MainContentAjax" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <div class="section">
                        <div class="header"><h2><asp:Localize ID="FindProductsCaption" runat="server" Text="Find Products" /></h2></div>
                        <asp:Panel ID="SearchFormPanel" runat="server" CssClass="content" DefaultButton="SearchButton">
                            <table class="inputForm">
                                <tr>
                                    <th class="rowHeader"><cb:ToolTipLabel ID="SearchNameLabel" runat="server" Text="Product Name:" ToolTip="Enter all or part of a product name.  Wildcard characters * and ? are accepted." /></th>
                                    <td><asp:TextBox ID="SearchName" runat="server" Text="" /></td>
                                    <th class="rowHeader"><cb:ToolTipLabel ID="ShowImagesLabel" runat="server" Text="Show Thumbnails:" ToolTip="When checked, product images will be displayed in the search results." /></th>
                                    <td><asp:CheckBox ID="ShowImages" runat="server" /></td>
                                    <td><asp:LinkButton ID="SearchButton" runat="server" Text="Search" SkinID="Button" OnClick="SearchButton_Click" /></td>
                                </tr>
                            </table>

                            <cb:SortedGridView ID="SearchResultsGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductId" GridLines="None" SkinID="PagedList" Width="100%" AllowPaging="true" Visible="false" DefaultSortExpression="Name" EnableSortingAndPagingCallbacks="True" PageIndex="0" AllowSorting="True">

                                <Columns>
                                    <asp:TemplateField HeaderText="Thumbnail">
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:HyperLink ID="NodeImageLink" runat="server" NavigateUrl='<%# UrlGenerator.GetBrowseUrl((int)Eval("ProductId"), CatalogNodeType.Product, (string)Eval("Name")) %>'> <asp:Image ID="NodeImage" runat="server" ImageUrl='<%# Eval("ThumbnailUrl") %>' Visible='<%# !string.IsNullOrEmpty((string)Eval("ThumbnailUrl")) %>' AlternateText='<%# Eval("Name") %>' /> </asp:HyperLink>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="SKU" SortExpression="Sku">
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemTemplate>
                                            <asp:HyperLink ID="ProductSku" runat="server" Text='<%#Eval("Sku")%>' SkinID="FieldHeader" /><br />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Item" SortExpression="Name" > 
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemTemplate>
                                            <asp:HyperLink ID="ProductName" runat="server" Text='<%#Eval("Name")%>' SkinID="FieldHeader" /><br />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Assigned">
                                        <ItemStyle Width="50px" HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:ImageButton ID="AttachButton" runat="server" CommandArgument='<%#Container.DataItemIndex%>' AlternateText="Add" ToolTip="Add" SkinID="AddIcon" OnClientClick="this.visible=false" OnClick="AttachButton_Click" Visible='<%#!IsProductLinked(((Product)Container.DataItem).ProductId)%>' />
                                            <asp:ImageButton ID="RemoveButton" runat="server" CommandArgument='<%#Container.DataItemIndex%>' AlternateText="Remove" ToolTip="Remove" SkinID="DeleteIcon" OnClientClick="return confirm('Are you sure you want to remove this item from this option?')" OnClick="RemoveButton_Click" Visible='<%#IsProductLinked(((Product)Container.DataItem).ProductId)%>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <EmptyDataTemplate>
                                    There are no products that match the search text.
                                </EmptyDataTemplate>
                            </cb:SortedGridView>
                        </asp:Panel>
                    </div>
                    <div class="section">
                        <div class="header"><h2>Assigned Products</h2></div>
                        <div class="content">
                            <asp:Repeater ID="RelatedProductGrid" runat="server" SkinID="PagedList" OnItemCommand="RelatedProductGrid_RowDeleting">
                                <HeaderTemplate>
                                    <table width="100%" class="pagedList" cellpadding="0" cellspacing="0">
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <tr class="oddRow">
                                        <td>
                                            <asp:Label ID="ProdID" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
                                            <asp:HyperLink ID="ProductName2" runat="server" Text='<%#Eval("Name")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
                                        </td>
                                        <td><asp:ImageButton ID="RemoveButton2" runat="server" SkinID="DeleteIcon" CommandName="Delete" CommandArgument='<%#Eval("ProductId") %>' AlternateText="Remove" ToolTip="Remove" OnClientClick="return confirm('Are you sure you want to remove this item from this option?')" /></td>
                                    </tr>
                                </ItemTemplate>
                                <AlternatingItemTemplate>
                                    <tr class="evenRow">
                                       <%--  <td>
                                            <asp:Label ID="ProdID2" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
                                            <asp:HyperLink ID="ProductSku" runat="server" Text='<%#Eval("Sku")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
                                        </td>--%>
                                        <td>
                                            <asp:Label ID="ProdID" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
                                            <asp:HyperLink ID="ProductName2" runat="server" Text='<%#Eval("Name")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
                                        </td>
                                        <td><asp:ImageButton ID="RemoveButton2" runat="server" SkinID="DeleteIcon" CommandName="Delete" CommandArgument='<%#Eval("ProductId") %>' AlternateText="Remove" ToolTip="Remove" /></td>
                                    </tr>
                                </AlternatingItemTemplate>
                                <FooterTemplate>
                                    </table>
                                </FooterTemplate>
                            </asp:Repeater>
                        </div>
                    </div>
                    <asp:Button ID="FinishButton" runat="server" Text="Finish" OnClick="FinishButton_Click" /><br />
                    <br />

                </ContentTemplate>
            </asp:UpdatePanel>
        </td>
    </tr>
</table>

I started work on the function to populate the SearchResultsGrid, and the SQL command is how I want to be in terms of getting the correct information on the button click. I plan to call this function inside the button click.

protected void PopulateSearchGrid()
{
    try 
    {
        System.Data.SqlClient.SqlDataReader dr = null;

        DataSet ds = new DataSet();
        DataTable resultsTbl = ds.Tables.Add("resultsTbl");
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter("SELECT OptionID, ac_Products.Name, Sku FROM ac_Options, ac_Products WHERE OptionID = @OptionID AND ac_Products.Name = @Name", cn))
            {
               ///and this is where I'm getting confused on what to do :(
            }
        }

    }
    catch { }
}
Was it helpful?

Solution

You may use a DataTable object, populating it with a SQL query, and assign it to the datasource of the grid. Use your DataAdapter to fill your DataTable with your SQL query (mind to match column names with those on your GridView)

DataTable dt = new DataTable();
//use your DataAdapter to fill the data table
sda.Fill(dt);
SearchResultsGrid.DataSource = dt;
SearchResultsGrid.DataBind();

Your query is using parameters, so you might want to use a SqlCommand instead of specifying the SQL directly on your SqlDataAdapter.
SqlCommand cmd = new SqlCommand("Your Query",cn);
cmd.Parameters.Add("Your first parameter", your param value);
cmd.Parameters.Add("Your second parameter", your param value);
... new SqlDataAdapter(cmd)...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top