【发布时间】:2014-07-25 17:31:00
【问题描述】:
由于某种原因,我无法在我的页面上显示 Gridview。
<asp:GridView ID="gvCustomers" runat="server"
CssClass="auto-style2" Width="1057px" AutoGenerateColumns="true"
ViewStateMode="Enabled">
</asp:GridView>
代码背后
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
if (Session["DirtyData"] != null)
{
Dirty dirty = Session["DirtyData"] as Dirty;
char delimiter = '\t';
DataTable customer = new DataTable();
#region customer
string[] customerCol1 = dirty.Customer[0].Split(delimiter);
foreach (string col in customerCol1)
{
DataColumn column = new DataColumn();
column.ColumnName = col;
customer.Columns.Add(column);
Response.Write(string.Format("Adding Customer Column: {0}<br />", col));
}
for (int i = 1; i < dirty.Customer.Count; i++)
{
DataRow row = customer.NewRow();
string[] split = dirty.Customer[i].Split(delimiter);
for (int x = 0; x < split.Length; x++)
{
row[i] = split[x];
}
customer.Rows.Add(row);
}
Response.Write("binding customer<br />");
gvCustomers.DataSource = customer;
gvCustomers.DataBind();
gvCustomers.Visible = true;
#endregion
Session["DirtyData"] = null;
}
else
{
Response.Redirect("Login.aspx");
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
输出显示正在添加到网格的列,因此我知道数据表中有数据,但页面上没有显示网格。
我看不出我在这里缺少什么。
【问题讨论】:
-
显示您与数据绑定的事件/方法......可能为时已晚。
-
只是一个实验,尝试在
gvCustomers上设置EmptyDataText="No data found"。 -
@mason 如果没有行,列不会显示?我看到显示“未找到数据”。
-
是的,完全正确。如果没有行,则没有理由显示列。查看您绑定到 GridView 的数据并确保它不为空。
-
@mason 好的,我不知道这一点。是的,我知道目前只有列,没有行。