Improve company productivity with a Business Account.Sign Up

x
?
Solved

how to display this in a gridview or table

Posted on 2008-10-06
8
Medium Priority
?
271 Views
Last Modified: 2013-11-26
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
Comment
Question by:niceoneishere
  • 5
  • 3
8 Comments
 
LVL 18

Assisted Solution

by:Priest04
Priest04 earned 2000 total points
ID: 22651431
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 22653694
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
 
LVL 18

Assisted Solution

by:Priest04
Priest04 earned 2000 total points
ID: 22653872
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
LVL 2

Author Comment

by:niceoneishere
ID: 22654249
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
 
LVL 18

Assisted Solution

by:Priest04
Priest04 earned 2000 total points
ID: 22654406
This should be done after you have populated DataTable, and before you have binded GridView to DataTable.
0
 
LVL 2

Author Comment

by:niceoneishere
ID: 22668160
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 22668800
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
 
LVL 2

Accepted Solution

by:
niceoneishere earned 0 total points
ID: 22687083
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question