动态gridview列无法显示
前台:
后台:
列表中的数据无法显示,帮我看看这是为啥?
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="adminContent">
<asp:GridView ID="gvSchedule" runat="server" AutoGenerateColumns="False" Width="100%"
PageSize="15">
<RowStyle Height="50" />
<Columns>
<asp:BoundField DataField="peopleSchedule" />
</Columns>
</asp:GridView>
</table>
<div>
<asp:Button runat="server" ID="btnBefore" Text="<<" OnClick="btnBefore_Click" />
<asp:Button runat="server" ID="btnNext" Text=">>" OnClick="btnNext_Click" />
</div>
</div>
</form>
</body>
</html>后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
namespace CaLendar_project
{
public partial class calendarWeb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataBind();
}
}
/// <summary>
/// 行事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvSchedule_RowCommand(object sender, GridViewCommandEventArgs e)
{
//如果有工作任务则显示工作任务
//如果没有工作任务则显示无
//布置完工作任务重新刷新画面
}
/// <summary>
/// 绑定grid
/// </summary>
private void DataBind()
{
//动态生成行需要的数据
var strPeople = new string[] { "中井", "九鬼", "依藤", "左藤"};
//动态生成列需要的数据(列名数据来源)
var strToday = DateTime.Today;
var strDateBegin = strToday.AddDays(-(Int32)strToday.DayOfWeek);
var strDays = new ArrayList();
for (int i = 0; i < 7; i++)
{
strDays.Add(strDateBegin.AddDays(i).ToString("yyyy" + "年" + "MM" + "月" + "dd" + "日"));
}
//动态生成列需要的数据
DataTable dt = new DataTable();
dt.Columns.Add("peopleSchedule", typeof(string));
//动态生成行
for (int i = 0; i < strPeople.Length; i++)
{
DataRow dr = dt.NewRow();
dr["peopleSchedule"] = strPeople[i].ToString();
dt.Rows.Add(dr);
}
//如果人员数据则填充列表
if (strPeople.Length > 0)
{
//清空动态列
int tempCount = gvSchedule.Columns.Count - 1;
for (int i = 0; i < tempCount; i++)
{
gvSchedule.Columns.RemoveAt(1);
}
//动态添加列,列名为日期
foreach (var item in strDays)
{
dt.Columns.Add(item.ToString());
if (gvSchedule.Columns.Count != strDays.Count + 1)
{
BoundField bf = new BoundField();
bf.HeaderText = item.ToString();
gvSchedule.Columns.Add(bf);
}
}
//根据人员和日期填充工作列表
for (int j = 0; j < dt.Rows.Count; j++)
{
for (int i = 0; i < strDays.Count; i++)
{
dt.Rows[j][i + 1] = "test";
}
}
}
else
{
dt = null;
}
this.gvSchedule.DataSource = dt;
this.gvSchedule.DataBind();
//SetGridViewWidth(gvOrderView);
}
}
}列表中的数据无法显示,帮我看看这是为啥?
...全文


