怎么从cs页给aspx页的GridView传值(代码写到cs页)
GridView的列是动态生成的,所以GridView要显示的东西也是变的,怎么在cs页控制GridView的显示内容
(怎么从cs页给aspx页的GridView传值(代码写到cs页))
[最优解释]
前台:
<asp:GridView ID="gv" runat="server">
<Columns>
</Columns>
</asp:GridView>
后台:
DataRow row;
DataTable dtTpQty = new DataTable();
dtTpQty.Columns.Add("ID");
dtTpQty.Columns.Add("type");
dtTpQty.Columns.Add("qty");
row = dtTpQty.NewRow();
row["ID"] = "0001";
row["type"] = "0";
row["qty"] = "2500";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0001";
row["type"] = "1";
row["qty"] = "3100";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0001";
row["type"] = "2";
row["qty"] = "2600";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0001";
row["type"] = "3";
row["qty"] = "1500";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0002";
row["type"] = "0";
row["qty"] = "1100";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0002";
row["type"] = "1";
row["qty"] = "4200";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0002";
row["type"] = "2";
row["qty"] = "3200";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0002";
row["type"] = "3";
row["qty"] = "1500";
dtTpQty.Rows.Add(row);
row = dtTpQty.NewRow();
row["ID"] = "0003";
row["type"] = "3";
row["qty"] = "5100";
dtTpQty.Rows.Add(row);
DataTable dtShow = new DataTable();
dtShow.Columns.Add("ID");
DataColumn[] cols = new DataColumn[1];
cols[0] = dtShow.Columns["ID"];
dtShow.PrimaryKey = cols;
string strType="";
foreach (DataRow rowTpQty in dtTpQty.Rows)
{
strType = "type" + rowTpQty["type"].ToString();
if (dtShow.Columns.IndexOf(strType) < 0)
{
dtShow.Columns.Add(strType);
}
if (!dtShow.Rows.Contains(rowTpQty["ID"].ToString()))
{
row = dtShow.NewRow();
row["ID"] = rowTpQty["ID"].ToString();
dtShow.Rows.Add(row);
}
else
{
row = dtShow.Rows.Find(rowTpQty["ID"].ToString());
}
row[strType] = rowTpQty["qty"].ToString();
}
BoundField bf = null;
DataColumn col = null;
for (int i = 0; i < dtShow.Columns.Count; i++)
{
col = dtShow.Columns[i];
bf = new BoundField();
bf.DataField = col.ColumnName;
bf.HeaderText = col.ColumnName;
gv.Columns.Add(bf);
}
gv.DataSource = dtShow;
gv.DataBind();