如何使用C#在gridview中将行值显示为列

时间:2015-10-02 10:02:15

标签: c# sql-server

我有一个表,我希望使用C#在gridview中将我的表行显示为列名。请问有人可以建议怎么做吗?

CREATE TABLE [dbo].[duedate]
(
     [duedateid] [int] IDENTITY(1,1) NOT NULL,
     [duedate] [nvarchar](50) NULL
)

SET IDENTITY_INSERT [dbo].[duedate] ON

INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (1, N'Apr')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (2, N'May')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (3, N'Jun')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (4, N'Jul')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (5, N'Aug')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (6, N'Sep')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (7, N'Oct')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (8, N'Nov')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (9, N'Dec')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (10, N'Jan')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (11, N'Feb')
INSERT [dbo].[duedate] ([duedateid], [duedate]) VALUES (12, N'Mar')

SET IDENTITY_INSERT [dbo].[duedate] OFF

我想将此列值显示为列名称,如

Sr.No   Apr   May  Jun  Jul  Aug  Set  Oct  Nov  Dec Jan Fe Mar

我的gridview代码在

之下
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
     CellPadding="4" GridLines="Horizontal">
     <RowStyle BackColor="White" ForeColor="#333333" />
         <Columns>
              <asp:BoundField DataField="duedateid" HeaderText="Fee" />
              <asp:BoundField DataField="duedate" HeaderText="Total" />
         </Columns>
         <FooterStyle BackColor="White" ForeColor="#333333" />
         <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
         <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
         <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>

我的c#代码用于绑定数据库中的gridview数据

     public void bindgvduedate()
    {
        con = new SqlConnection();
        con.ConnectionString =         ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        con.Open();
        DataTable table = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("select *from duedate",con);
        da.Fill(table );
        GridView1.DataSource= table;
        GridView1.DataBind();
        con.Close();

    }

在页面加载时调用函数

 using System;
 using System.Collections;
 using System.Configuration;
 using System.Data;
 using System.Linq;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.HtmlControls;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Xml.Linq;
 using System.Data.SqlClient;
 namespace egurkul
 {
  public partial class feeRecipt : System.Web.UI.Page
   {

    protected void Page_Load(object sender, EventArgs e)
    {

        bindgvduedate();
    }
}

1 个答案:

答案 0 :(得分:0)

此任务似乎与在SQL中使用PIVOT有关。 PIVOT子句可以将行转换为列。在您的情况下,您应该知道将使用哪种聚合。我有这样的解决方案:

SELECT 'max(duedateid)' SrNo, *
FROM
(
    SELECT  d.duedate, d.duedateid
    FROM duedate AS d
) AS s
PIVOT
(
    MAX(duedateid) FOR duedate
    IN(
        [Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec]
    )
) AS pvt