我尝试从Active Directory检索的数据中加载网格。我已经将GridView设置如下。问题是正在创建记录但数据未显示
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" AllowPaging="True" Visible="false"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
style ="z-index: 2; left: 65px; top:355px; position: absolute; height: 221px; width: 1158px">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Employee No">
<ItemTemplate>
<asp:Label ID="EmployeeNo" runat="server" style="color:blue"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="DisplayName" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="Department" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<asp:Label ID="JobTitle" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manager">
<ItemTemplate>
<asp:Label ID="Manager" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="Name" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
我在收集活动数据之前运行以下功能
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("EmployeeNo", typeof(string)));
dt.Columns.Add(new DataColumn("DisplayName", typeof(string)));
dt.Columns.Add(new DataColumn("Department", typeof(string)));
dt.Columns.Add(new DataColumn("JobTitle", typeof(string)));
dt.Columns.Add(new DataColumn("Manager", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["EmployeeNo"] = string.Empty;
dr["DisplayName"] = string.Empty;
dr["Department"] = string.Empty;
dr["JobTitle"] = string.Empty;
dr["Manager"] = string.Empty;
dr["Name"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
然后我像这样宣布我的DataTable
SetInitialRow();
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
然后我遍历活动数据记录
for (int i = 0; i < MyVars.NoOfADRecords; i++)
{
Label empNo = (Label)Gridview1.Rows[i].Cells[1].FindControl("EmployeeNo");
Label displayName =(Label)Gridview1.Rows[i].Cells[2].FindControl("DisplayName");
Label dept =(Label)Gridview1.Rows[i].Cells[3].FindControl("Department");
Label jobTitle = (Label)Gridview1.Rows[i].Cells[1].FindControl("JobTitle");
Label manager = (Label)Gridview1.Rows[i].Cells[2].FindControl("Manager");
Label name = (Label)Gridview1.Rows[i].Cells[3].FindControl("Name");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i;
empNo.Text = MyVars.ADEmployeeID[i];
displayName.Text = MyVars.ADName[i];
dept.Text = MyVars.ADDepartment[i];
jobTitle.Text = MyVars.ADJobtitle[i];
manager.Text = MyVars.ADManager[i];
name.Text = MyVars.ADName[i];
dtCurrentTable.Rows[i]["EmployeeNo"] = empNo.Text;
dtCurrentTable.Rows[i]["DisplayName"] = displayName.Text;
dtCurrentTable.Rows[i]["Department"] = dept.Text;
dtCurrentTable.Rows[i]["JobTitle"] = jobTitle.Text;
dtCurrentTable.Rows[i]["Manager"] = manager.Text;
dtCurrentTable.Rows[i]["Name"] = name.Text;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
}
现在它在第一个循环中崩溃,如果我将i替换为0,就像这样
(Label)Gridview1.Rows[0].Cells[1].FindControl("EmployeeNo");
虽然有效,但所有记录都显示没有数据。当我在调试empNo.text等时执行代码时,所有都有值,所以我做错了什么?
答案 0 :(得分:0)
您正在将空行添加到datatable中。替换下面的代码以将行添加到数据表中
drCurrentRow["EmployeeNo"] = empNo.Text;
drCurrentRow["DisplayName"] = displayName.Text;
drCurrentRow["Department"] = dept.Text;
drCurrentRow["JobTitle"] = jobTitle.Text;
drCurrentRow["Manager"] = manager.Text;
drCurrentRow["Name"] = name.Text;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
而不是你的代码
dtCurrentTable.Rows[i]["EmployeeNo"] = empNo.Text;
dtCurrentTable.Rows[i]["DisplayName"] = displayName.Text;
dtCurrentTable.Rows[i]["Department"] = dept.Text;
dtCurrentTable.Rows[i]["JobTitle"] = jobTitle.Text;
dtCurrentTable.Rows[i]["Manager"] = manager.Text;
dtCurrentTable.Rows[i]["Name"] = name.Text;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
答案 1 :(得分:0)
您正在使用SetInitialRow
方法绑定DataTable。当你在那里绑定时,它只将一个空行添加到GridView。
您应该在循环后进行数据绑定以获取所有用户...
其次,您尝试在GridView中找到没有的控件,然后转换为Label
,从MyVars.NoOfADRecords
设置Label文本并将其添加到DataTable。最好将值直接添加到DataTable,如下面的代码段所示。
for (int i = 0; i < MyVars.NoOfADRecords; i++)
{
drCurrentRow["EmployeeNo"] = MyVars.ADEmployeeID[i];
drCurrentRow["DisplayName"] = MyVars.ADName[i];
dtCurrentTable.Rows.Add(drCurrentRow);
}
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
ViewState["CurrentTable"] = dtCurrentTable;
但是你想要摆脱许多不必要的代码,只需这样做:
Gridview1.DataSource = MyVars.NoOfADRecords;
Gridview1.DataBind();
<ItemTemplate>
<%# Eval("EmployeeNo") %>
</ItemTemplate>
答案 2 :(得分:0)
我认为您的问题可能只是您过早调用GridView1.DataBind()。在完成将所有行添加到dtCurrentTable后,尝试调用GridView1.DataBind() last 。