如何添加数据到GridView?
问题描述:
我想通过下面的代码添加数据:如何添加数据到GridView?
protected void gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Session["BranchingCode"] != null)
{
List<CourseDetail> studentList = Course_Detail.GetStudentListOfBranch(Session["BranchingCode"].ToString(), Session["CurrentSession"].ToString());
if (studentList != null)
{
for (int i = 0; i < studentList.Count(); i++)
{
e.Row.Cells[0].Text = studentList[i].UserNameRoll;
e.Row.Cells[1].Text = studentList[i].StudentName;
}
}
}
GridView1.DataBind();
}
}
但因为没有datasource
附着的GridView,此事件不火。 请告诉我该怎么办? 有没有办法强行启动这个事件或者做其他事情&在其他地方输入数据..?
答
你滥用这个事件,你不应该打电话,如果有力。
在Page_Load事件的所有地方首先加载数据并将其绑定到网格:
if (Session["BranchingCode"] != null)
{
List<CourseDetail> studentList = Course_Detail.GetStudentListOfBranch(Session["BranchingCode"].ToString(), Session["CurrentSession"].ToString());
if (studentList != null)
{
GridView1.DataSource = studentList;
GridView1.DataBind();
}
}
这将绑定你的学生名单电网。现在,我们必须处理上显示网格数据,有超过一个的方式来做到这一点,但是这应该给你足够:
在你的HTML,xxx.aspx页面,您可以声明你的GridView做到这一点:
<asp:GridView ID="GridView1" runat="server" ...... >
<Columns>
<asp:BoundField HeaderText="User Name Roll" DataField="UserNameRoll" />
<asp:BoundField HeaderText="Student Name" DataField="StudentName" />
</Columns>
</asp:GridView>
我不能添加它..它说gridview1不在当前上下文页面加载.. – 2013-02-09 08:30:28