如何在asp.net中以所需格式显示选择查询的结果

时间:2014-02-03 06:11:18

标签: c# asp.net sql-server-2008-r2

我有以下查询来显示数据

Select 
     convert(varchar(10),SR.StockDate,103) as StockDate,SR.PartNo,
     SR.Openingbalance,SR.Received,SR.Issued,SR.Balance,SR.Entrytype,A.MaterialName 
from 
     StockRegister SR 
join LEDProducts A 
     on SR.PartNo=A.PartNo  
where 
     SR.LocationId='"+ ddl_Location.SelectedValue.ToString() + "' and A.AudioEntityId='" 
    + ddl_CountrySR.SelectedValue.ToString() + "' 
order by SR.Sno

它显示以下结果

 S Date       Type  ProductName   PartNo   OBalance  Receipt  Issues  CBalance
 1 26/07/2013  FTE     P16       01110101     500       0          0     500
 2 24/01/2014  EP      P16       01110101     500       0          100   400
 3 19/12/2013  FTE     P7        01110102     1000      0          0     1000

我需要显示如下 产品名称:P16

Date            Type          Receipt  Issues    Balance
26/07/2013      FTE            500      0         500
24/01/2013       EP             0       100       400
Closing Balance :400

产品名称:P7

opening balance :1000
    Date            Type          Receipt  Issues    Balance
    26/07/2013      FTE            1000      0         1000

    Closing Balance :1000

就像我想要动态显示所有产品一样。

2 个答案:

答案 0 :(得分:1)

aspx代码......

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

cs code ...

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[9] { new DataColumn("S"), new DataColumn("Date"), new DataColumn("Type")
                ,new DataColumn("ProductName"), new DataColumn("PartNo"), new DataColumn("OBalance"),
                new DataColumn("Receipt"), new DataColumn("Issues"), new DataColumn("CBalance")});
                dt.Rows.Add("1", "26/07/2013", "FTE", "P16", "01110101", "500", "0", "0", "500");
                dt.Rows.Add("2", "24/01/2014", "EP", "P16", "01110101", "500", "0", "100", "400");
                dt.Rows.Add("3", "19/12/2013", "FTE", "P7", "01110102", "1000", "0", "0", "1000");
                var names = (from DataRow dr in dt.Rows
                             select (string)dr["ProductName"]).Distinct();
                StringBuilder s = new StringBuilder();
                s.Append("<table>");
                foreach (var r in names)
                {
                    s.Append("<tr><td colspan='5'>ProductName:" + r + "</td></tr>");
                    var rows = from DataRow dr in dt.Rows
                               where dr["ProductName"] == r
                               orderby (string)dr["S"]
                               select dr;
                    if (rows.Count() > 0)
                    {
                        s.Append("<tr><td>Date</td><td>Type</td><td>Receipt</td><td>Issues</td><td>Balance</td></tr>");

                        foreach (var r1 in rows)
                        {
                            s.Append("<tr><td>" + (string)r1["Date"] + "</td><td>" + (string)r1["Type"] + "</td><td>" + (string)r1["Receipt"] + "</td><td>" + (string)r1["Issues"] + "</td><td>" + (string)r1["CBalance"] + "</td></tr>");
                        }
                        s.Append("<tr><td colspan='5'>Closing Balance :" + (string)rows.Last()["CBalance"] + "</td></tr>");
                    }
                    s.Append("<tr><td colspan='5'></td></tr>");
                }
                s.Append("</table>");
                Response.Write(s.ToString());
            }
        }
    }
}

尝试制作一个新的演示Web应用程序并实现此代码......

答案 1 :(得分:0)

您可以使用转发器控件在

中使用DataGrid Control执行此操作

设置其数据源属性也可以在转发器中设置ItemTemplate,以便为每个产品显示任何控件

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" EnableModelValidation="True"></asp:GridView>
        <asp:SqlDataSource runat="server" ID="SqlDataSource1"></asp:SqlDataSource>
    </ItemTemplate>
</asp:Repeater>

您可以使用任务面板设置要显示的列 configure the datagrid control using GUI tasks panel