2014-11-21 33 views
0

我正在尝试编写一个Web应用程序来收集用户数以用于审计目的。我有一个可以运行的Oracle查询(我在SQL Developer中运行它),但是我正在尝试通过web为超级用户提供此报告来取代他们自己的计数而不依赖于我。我无法在gridview中显示数据。将数据绑定到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; 

结果显示是这样的:

------------------------------------------ 
|  | user1 | user2 | user3 | user4 | 
------------------------------------------ 
| Create | 5  | 2  | 8  | 15 | 
------------------------------------------ 
| Delete | 9  | 6  | 10 | 1  | 
------------------------------------------ 
| View | 123 | 461 | 84 | 89 | 
------------------------------------------ 
***note: Create, Delete, View are from "display_name" from the query 

我知道该怎么做时,“列”是固定的(我用另一个查询做了另一个表),但是因为我不知道会有多少“userX”,所以我不知道如何在Web中动态构建它并将其显示在网格中。为GridView控件

ASPX代码:

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

aspx.cs后台代码:

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(); 
+1

是否有一个原因你不能设置'AutoGenerateColumns =“True”'? – entropic 2014-11-21 23:42:37

+0

我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 – 2014-11-21 23:48:22

+0

Dang,菜鸟错误...我甚至没有注意到AutoGenerateColumns属性。我已经从我的第一个具有固定列数的GridView复制了我的GridView,但没有注意到它。谢谢你的提示! – Jayarikahs 2014-11-23 03:01:55

回答

0

无论您在GridView控件中如何声明AutoGenerateColumns="False",都需要指定Columns标签,其中包含所有列及其各自的数据值。既然你说你不知道有多少“userX”会在那里,让GridView控件为你绑定数据,只需将你希望在GridView中看到的数据正确地传递给DataTable \ DataSet并将AutoGenerateColumns属性设置为true: -

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

更新: -

您可以获取并填写直接使用dataadapter数据集: -

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(); 
} 
+0

感谢您的提示!我将它更改为“true”,但我仍然无法使用DataTable为查询中的用户创建“列”。我还没有想出如何动态地做到这一点,因为它给了我“输入数组比这个表中的列数多。”我怀疑(很确定)这是因为DataTable。 – Jayarikahs 2014-11-23 03:06:55

+0

@Jayarikahs - 如果你的列不固定,为什么你要手动填充它?你可以使用'DataAdapter'来为你填充它。 – 2014-11-23 03:13:28

+0

@Jayarikahs - 请检查我的更新,看看是否有帮助。 – 2014-11-23 03:26:00

0

如果用户1,用户2和用户N可以从表中读取说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有关于用户选择的数据绑定的选择事件。

+0

我的用户不在单独的表格中。当用户执行某个操作时,会对其进行审核,并在我的“事件”表中创建一条记录,该表将用户名存储在“创建者”列中。 – Jayarikahs 2014-11-23 03:04:30