C# 将数据绑定到Gridview固定行、可变列

C# 将数据绑定到Gridview固定行、可变列,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试编写一个web应用程序来收集用户计数,以便进行审计。我有一个Oracle查询,我在SQL Developer中运行它,但我试图通过web提供此报告,供超级用户自行计算,而不是依赖于我。在gridview中显示数据时遇到问题 具有硬编码固定用户数的Oracle查询: select * from ( select ad.display_name, ev.creator from auditdefinition ad, event ev where ad.event_class_id =

我正在尝试编写一个web应用程序来收集用户计数,以便进行审计。我有一个Oracle查询,我在SQL Developer中运行它,但我试图通过web提供此报告,供超级用户自行计算,而不是依赖于我。在gridview中显示数据时遇到问题

具有硬编码固定用户数的Oracle查询:

select * from (
select ad.display_name, ev.creator from auditdefinition ad, event ev 
where ad.event_class_id = ev.object_class_id and ev.create_date >= to_date('01-OCT-2014','dd-MON-yyyy') and ev.create_date < to_date('01-DEC-2014','dd-MON-yyyy')
)
pivot 
(
count(creator)
for creator in ('user1','user2','user3','user4')
)
order by display_name;
我知道如何在列被修复时执行此操作,我使用不同的查询创建了另一个表,但因为我不知道将有多少userX,我不知道如何在web中动态构建并在网格中显示它

GridView的aspx代码:

<asp:GridView AutoGenerateColumns="False" CellPadding="2" CellSpacing="2" GridLines="none" HorizontalAlign="Left" ID="GridView2" runat="server">
        </asp:GridView>

如果可以从表中读取user1、user2和usern,比如tb_users,这将使脚本更简单

select * from (
select ad.display_name, ev.creator from auditdefinition ad, event ev 
where ad.event_class_id = ev.object_class_id and 
ev.create_date >= to_date('01-OCT-2014','dd-MON-yyyy') and 
ev.create_date < to_date('01-DEC-2014','dd-MON-yyyy'))
pivot 
(
count(creator)
for creator in (select username from tb_users)
)
order by display_name;
如果执行此操作,请在web表单上创建一个下拉列表,并将其绑定到表tb_users

在此下拉列表的选择事件中,您可以将gridview与针对用户选择的数据绑定。

每当您在gridview控件中说AutoGenerateColumns=False时,您需要使用所有列及其各自的数据值指定Columns标记。既然您说您不知道将有多少userX,让GridView控件为您绑定数据,只需将您希望在GridView中看到的数据正确传递到DataTable\DataSet,并将AutoGenerateColumns属性设置为true:-


有什么原因不能将AutoGenerateColumns设置为True吗?我已经编辑了您的标题。请看,如果共识是否定的,他们就不应该。该死,新手犯了错误……我甚至没有注意到AutoGenerateColumns属性。我从我的第一个gridview复制了我的gridview,它有固定的列数,但我没有注意到。谢谢你的提示!当我不知道此人正在输入多少用户时,如何为DataTable动态创建列?我的用户不存在于单独的表中。当用户执行某个操作时,会对其进行审核,并在“我的事件表”中创建一条记录,该记录将用户名存储在“创建者”列中。感谢您的提示!我将其更改为true,但在DataTable从查询中为用户创建列时仍然遇到问题。我还没有弄清楚如何动态执行此操作,因为它给我的输入数组比表中的列数长。我很确定这是因为DataTable。@Jayarikahs-如果列不固定,为什么要手动填充?您可以使用DataAdapter来填充它。我必须将SqlDataAdapter与Oracle切换,因为这是我的后端。我做了测试,它的工作!删除DataTable并仅使用DataSet就解决了这个问题。再次感谢你为我指明了正确的方向。
DataSet ds = new DataSet();

DataTable dt = ds.Tables.Add("Amount");
//I would normally add my columns here if they were fixed but because it it dynamic, I am not sure



...

GridView2.DataSource = dt; //beginning to bind my DataTable
GridView2.DataBind();
select * from (
select ad.display_name, ev.creator from auditdefinition ad, event ev 
where ad.event_class_id = ev.object_class_id and 
ev.create_date >= to_date('01-OCT-2014','dd-MON-yyyy') and 
ev.create_date < to_date('01-DEC-2014','dd-MON-yyyy'))
pivot 
(
count(creator)
for creator in (select username from tb_users)
)
order by display_name;
<asp:GridView AutoGenerateColumns="True" CellPadding="2" CellSpacing="2" GridLines="none" HorizontalAlign="Left" ID="GridView2" runat="server">
</asp:GridView>
string CS = "Your connection string";
using (SqlConnection conn = new SqlConnection(CS))
{
    SqlDataAdapter da = new SqlDataAdapter("SPName", conn);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;
    da.SelectCommand.Parameters.Add("@yourParam", SqlDbType.Int/*Your datatype*/).Value = 123;
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView2.DataSource = ds;
    GridView2.DataBind();
}