我准备一份报告,显示旧客户的数量和基于性别的新客户。因此报告的最高级别是性别,然后是客户类型。但问题是在DataTable中,我得到了普通的表数据格式。如何以二维格式格式化DataTable。
数据表格式
必填格式
如何使用DataTable,GridView显示此报告信息?
答案 0 :(得分:1)
试试此代码 这里数据表形状为枢轴网格并绑定到gridview。
<强> ASPX:强>
<asp:GridView ID="gv2" OnDataBound="gv2_DataBound" runat="server" BackColor="White" BorderStyle="Solid"
BorderWidth="1px" AutoGenerateColumns="false">
<HeaderStyle BackColor="#95B3D7"/>
<Columns>
<asp:BoundField DataField="USER" HeaderText="USER" />
<asp:BoundField DataField="MALE_NEW" HeaderText="NEW CUSTOMER COUNT" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="MALE_OLD" HeaderText="OLD CUSTOMER COUNT" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="FEMALE_NEW" HeaderText="NEW CUSTOMER COUNT" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="FEMALE_OLD" HeaderText="OLD CUSTOMER COUNT" ItemStyle-HorizontalAlign="Right"/>
</Columns>
</asp:GridView>
CS:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("tbl");
dt.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "USER" });
dt.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "GENDER" });
dt.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "NCNT" });
dt.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "OCNT" });
dt.Rows.Add("TEST", "MALE", 6, 1);
dt.Rows.Add("TEST1", "MALE", 3);
dt.Rows.Add("TEST2", "MALE", 4);
dt.Rows.Add("TEST", "FEMALE", 1);
dt.Rows.Add("TEST1", "FEMALE", 1);
DataTable dtpivot = new DataTable();
var ik = dt.AsEnumerable().Select(i => i.Field<string>("USER")).Distinct();
int cnt = 0;
foreach (var item in ik)
{
if (dtpivot.Columns["USER"] == null) { dtpivot.Columns.Add("USER"); }
dtpivot.Rows.Add(item);
var il = dt.AsEnumerable().Where(i => i.Field<string>("USER").Equals(item)).Select(j => j.Field<string>("GENDER")).Distinct();
foreach (var item1 in il)
{
var im = dt.AsEnumerable().Where(i => i.Field<string>("GENDER").Equals(item1) && i.Field<string>("USER").Equals(item)).Select(j => new { newcol = j.Field<int?>("NCNT"), oldcol = j.Field<int?>("OCNT") });
foreach (var item2 in im)
{
if (dtpivot.Columns[item1 + "_NEW"] == null) { dtpivot.Columns.Add(item1 + "_NEW"); }
if (dtpivot.Columns[item1 + "_OLD"] == null) { dtpivot.Columns.Add(item1 + "_OLD"); }
dtpivot.Rows[cnt][item1 + "_NEW"] = item2.newcol;
dtpivot.Rows[cnt][item1 + "_OLD"] = item2.oldcol;
}
}
cnt++;
}
gv2.DataSource = dtpivot;
gv2.DataBind();
}
protected void gv2_DataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableHeaderCell cell = new TableHeaderCell();
cell.BackColor = Color.White;
row.Controls.Add(cell);
TableHeaderCell cell1 = new TableHeaderCell();
cell1.Text = "MALE";
cell1.ColumnSpan = 2;
cell1.BackColor = ColorTranslator.FromHtml("#F2DDDC");
row.Controls.Add(cell1);
TableHeaderCell cell2 = new TableHeaderCell();
cell2.Text = "FEMALE";
cell2.ColumnSpan = 2;
cell2.BackColor = ColorTranslator.FromHtml("#F2DDDC");
row.Controls.Add(cell2);
gv2.HeaderRow.Parent.Controls.AddAt(0, row);
}
如果我们想要动态获取列,那么只需删除列和gridview并设置 AutoGenerateColumns =&#34; true&#34; 。