我有以下代码在gridview列中显示为行:
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
gvOrdersProcessed.DataSource = FlipDataTable(BindOrdersProcessed());
gvOrdersProcessed.DataBind();
}
public DataTable BindOrdersProcessed()
{
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WO_StatusConnectionString"].ToString());
SqlCommand Cmd = new SqlCommand("sp_B2B_GetOrdersProcessed", Conn);
Cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ada = new SqlDataAdapter(Cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}
public DataTable FlipDataTable(DataTable dt)
{
DataTable table = new DataTable();
//Get all the rows and change into columns
for (int i = 0; i <= dt.Rows.Count; i++)
table.Columns.Add(Convert.ToString(i));
DataRow dr;
//get all the columns and make it as rows
for (int j = 0; j < dt.Columns.Count; j++)
{
dr = table.NewRow();
dr[0] = dt.Columns[j].ToString();
for (int k = 1; k <= dt.Rows.Count; k++)
dr[k] = dt.Rows[k - 1][j];
table.Rows.Add(dr);
}
return table;
}
在我的html中:
<div id="GridOrdersProcessed">
<asp:GridView ID="gvOrdersProcessed" runat="server">
</asp:GridView>
</div>
现在当运行上面我有数字作为标题:
0 1 2 3 4 5 6
DateCreate 02/11/2013 02/19/2013 02/21/2013 02/25/2013 02/26/2013 02/27/2013
OrdersPendingInvoice 0 0 0 0 0 0
OrdersPendingPickUp 1 1 2 1 7 9
为什么会这样?
另外,如何添加一个新列,其中包含如下总订单:
DateCreate Total 02/11/2013 02/19/2013 02/21/2013 02/25/2013 02/26/2013 02/27/2013
OrdersPendingInvoice 0 0 0 0 0 0 0
OrdersPendingPickUp 21 1 1 2 1 7 9