0

次のコードでデータを追加しようとしています:

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();
  }
}

ただし、datasourceGridview にはアタッチされていないため、このイベントは発生しません。何をすべきか教えてください。とにかく、このイベントを強制的に発生させるか、何か他のことをして別の場所にデータを入力することはありますか..?

4

1 に答える 1

2

このイベントを悪用し、強制的に呼び出すべきではありません。

まず、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();
   }
}

これにより、学生リストがグリッドにバインドされます。次に、グリッドにデータを表示する処理を行う必要があります。これを行うには複数の方法がありますが、これで十分です。

GridView を宣言している html の xxx.aspx ページで、次の操作を行います。

<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>
于 2013-02-09T09:34:47.407 に答える