Go Premium for a chance to win a PS4. Enter to Win

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

how to display this in a gridview or table

Hello All,
I am trying to display my search results in Gridview. it works but the problem is i want it to display in this format
itemNo            desc                size          price
AZ100          White Shoes        8            $15
                                                  9            $20
                                                  10          $22
                                                 11-13     $30
AZ101         Black Shoes          8            $15
                                                 12          $30
but right now it display like this
AZ100       White Shoes      8               $15
AZ100       White Shoes      9              $20
AZ100        White Shoes     10          $22
AZ100       White Shoes     11-13       $30

AZ101        Black Shoes        8       $15
AZ101        Black Shoes        12    $30

how can i display in first way. can some one please help me out. i am stuck
i am posting my complete code
Thanks a lot i really appreciate it.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GVsearch.aspx.cs" Inherits="MySite.GVsearch" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>SQL and GV seach page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox id="txtSearch" runat="server" />
 
<asp:Button id="btnSearch" runat="server"
    Text    = "Search"
    OnClick = "btnSearch_OnClick" />
    <br />
        <asp:Panel ID="SearchPanel" runat="server" Visible="false">
       
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySiteDBConnectionString %>"
            SelectCommand="SELECT [ItemNumber], [Title], [Price], [SizeName],[Description],[ShortTitle],[ShortDescription],[TitleImage],[ThumbnailImage] FROM [SearchTable] WHERE (([Title] LIKE '%' + replace(@Title, ' ', '%') + '%') OR ([ShortDescription] LIKE '%' + replace(@ShortDescription, ' ', '%') + '%') OR ([ShortTitle] LIKE '%' + replace(@ShortTitle, ' ', '%') + '%') OR ([Description] LIKE '%' + replace(@Description, ' ', '%') + '%') OR ([ItemNumber] LIKE '%' + replace(@ItemNumber, ' ', '%') + '%') OR ([SizeName] LIKE '%' + replace(@SizeName, ' ', '%') + '%'))" OnSelecting="SqlDataSource1_Selecting" >
            <SelectParameters>
                <asp:Parameter  Name="Title"  Type="String" />
                <asp:Parameter  Name="ShortDescription" Type="String" />
                <asp:Parameter  Name="ShortTitle" Type="String" />
                <asp:Parameter  Name="Description" Type="String" />
                <asp:Parameter  Name="ItemNumber"  Type="String" />
                <asp:Parameter  Name="SizeName" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    <br />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            CellPadding="4" DataKeyNames="ItemNumber" DataSourceID="SqlDataSource1" ForeColor="#333333"
            GridLines="None" AllowSorting="True">
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="Item" InsertVisible="False" SortExpression="ItemNumber">
                   <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ItemNumber") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Title" HeaderText="Product Name" SortExpression="Title" HtmlEncode="False" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price"  DataFormatString="{0:c}" HtmlEncode="False" />
                <asp:BoundField DataField="SizeName" HeaderText="Sizes" SortExpression=SizeName HtmlEncode="False" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" HtmlEncode="False" />
                <asp:TemplateField HeaderText="Image Preview">
                <ItemTemplate>
                <asp:Image ID="ImgThumb"  runat="server" ImageUrl='<%# Bind("ThumbnailImage","images/products/S/{0}") %>'   />
                </ItemTemplate>
                  </asp:TemplateField>
</Columns>
            <RowStyle BackColor="#E3EAEB" />
            <EditRowStyle BackColor="#7C6F57" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        
 </asp:Panel>   
    
    </div>
    
    </form>
</body>
</html>
-------------------------CODE BEHIND---------------------------------
namespace MySite{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using WoodenSoldier.catalog;
 
    public partial class GVsearch : MySitePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            SearchPanel.Visible = true;
            GridView1.DataBind();
        }
 
 
 
        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@Title"].Value = txtSearch.Text;
            e.Command.Parameters["@ShortDescription"].Value = txtSearch.Text;
            e.Command.Parameters["@ShortTitle"].Value = txtSearch.Text;
            e.Command.Parameters["@Description"].Value = txtSearch.Text;
            e.Command.Parameters["@ItemNumber"].Value = txtSearch.Text;
            e.Command.Parameters["@SizeName"].Value = txtSearch.Text;
           
        }
 
        
 
    }
}

Open in new window

0
niceoneishere
Asked:
niceoneishere
  • 5
  • 3
4 Solutions
 
Priest04Commented:
MS GridView doesnt support merging cells. What could be done behind the scenes, is this

1) check the item nu,mber the current record in datatable (start from send record).
2) If its the same as the previous, then replace it with null value

same for description. if you rae showing grid lines, then they will be visible for every cell, no matter if it contains null value.

Goran
0
 
niceoneishereAuthor Commented:
Hello Sir,
is it possible that you can post an example or an reference like an tutorial or an article for what you have suggested. i really appreciate it.

Thanks a lot .
0
 
Priest04Commented:
well, this is some to get you going

for (int i = 1; i < dt.Rows.Count; i++) // we start from 1, we want to skip first record
{
    if (dt.Rows[i]["ItemNo"].ToString() == dt.rows[i - 1]["ItemNo"].ToString()
    {
        dt.Rows[i]["ItemNo"] = "";
    }
    if (dt.Rows[i]["desc"].ToString() == dt.rows[i - 1]["desc"].ToString()
    {
        dt.Rows[i]["desc"] = "";
    }
}
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
niceoneishereAuthor Commented:
Hello Sir,
Thanks for the tip but one question where would i use this , is it RowDataBound of the GridView.
i appreciate it sir
Thanks
0
 
Priest04Commented:
This should be done after you have populated DataTable, and before you have binded GridView to DataTable.
0
 
niceoneishereAuthor Commented:
Hello Sir,
Thanks a lot for your help, but i was wondering if i want to display the all the sizes for a particular ItemNo in DropDownList, inside the GridView. is that possible sir, can you please advice me or  give an example.

once again i really appreciate your patience and help. i am still in the learning process.
Thanks
0
 
niceoneishereAuthor Commented:
Hello Sir,
here is my code with the Drop Down List in the Grid. but my problem is instead of display only the sizes for a particular Item No. it display all the sizes available in Database. what i mean say for example if itemNo A1001 comes in size 8,9,10,12 i want the Drop Down List to display only those sizes but right now it displays all sizes from 6, 7,8,9,10,11,12,13,14 etc.
can you please advice how to achieve what i am looking for sir. i am attaching my code.
Thanks once again i appreciate it.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DDLandGV.aspx.cs" Inherits="Menu_DDLandGV" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LocalDBConnectionString %>" 
        SelectCommand="SELECT DISTINCT [ItemNumber], [Title], [Price], [Description] FROM [SearchTable]"></asp:SqlDataSource>
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" CellPadding="4"
            DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="ItemNumber" HeaderText="ItemNumber" SortExpression="ItemNumber" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" DataFormatString="{0:c}" HtmlEncode="False" />
                <asp:TemplateField HeaderText="Available Sizes">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server"
                     DataSourceID="SqlDS2" DataTextField="SizeName"
                      DataValueField="ItemNumber" SelectedValue='<%# Bind("ItemNumber") %>'>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDS2" runat="server" ConnectionString="<%$ ConnectionStrings:LocalDBConnectionString %>" 
                    SelectCommand="SELECT [ItemNumber], [SizeName] FROM [SearchTable]" >
                    </asp:SqlDataSource> 
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

Open in new window

0
 
niceoneishereAuthor Commented:
Hello Sir,
i figured it out , i added a control parameter to SqlDS2 and it works like i charm. thanks a lot for your help in this regard. i have awarded you the points.
Thanks one again
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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