我想遍历每一行,每一列和所有单元格。然后用列表填充每一列。我有一个受雇对象列表,每个受雇者都有一个ID名称和一个城市。我想用受雇者列表填充gridview。第一列应该是所有雇员的身份证,第二列应该是每个雇员的名字,第三列应该是每个雇员的城市。我无法使之工作,将不胜感激。
这是我前端的样子。
<asp:GridView ID="GridView1" AllowPaging="true" ShowFooter="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing"
runat="server" CellPadding="3" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lblname" CssClass="gridTextbox" Text="" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtName" Text="" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtNameFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="city">
<ItemTemplate>
<asp:Label ID="lblcity" CssClass="gridTextbox" Text="" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtCity" Text="" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtCityFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我后端的样子。
protected List<Emp> GetEmpList()
{
List<Emp> lEmp = new List<Emp>();
Emp oemp = new Emp(1234, "Upendra", "Noida");
lEmp.Add(oemp);
oemp = new Emp(1234, "Upendra", "Noida");
lEmp.Add(oemp);
oemp = new Emp(1374, "Vishal", "Noida");
lEmp.Add(oemp);
oemp = new Emp(1934, "Rahul", "Noida");
lEmp.Add(oemp);
return lEmp;
}
protected void BindGridList()
{
for (int i = 0; i < GridView1.Columns.Count - 1; i++)
{
foreach (GridViewRow row in GridView1.Rows)
{
row.Cells[i].Text = GetEmpList()[i].ID.ToString();
}
}
}
}
public class Emp
{
public int ID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public Emp(int id, string name, string city)
{
this.ID = id;
this.Name = name;
this.City = city;
}
}
答案 0 :(得分:0)
我找到了一个解决方案,但是在下一页上不起作用。
protected void BindGridList()
{
for (int i = 0; i < GridView1.Columns.Count - 1; i++)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (i == 0)
{
row.Cells[i].Text = GetEmpList()[row.RowIndex].ID.ToString();
}
else if (i == 1)
{
row.Cells[i].Text = GetEmpList()[row.RowIndex].Name.ToString();
}
//....
}
}
}