从asp.net中的代码更新gridview

时间:2010-05-29 09:59:09

标签: c# asp.net gridview gridviewrow

我的asp.net 3.5应用程序[C#]中有gridview。看起来像这样:

<asp:GridView CssClass="grid_table" ID="GridView1" AllowPaging="true" PageSize="10" 
        AutoGenerateEditButton="true" ShowHeader="true"   
        AutoGenerateDeleteButton="true" DataKeyNames="studentId" runat="server" 
        OnRowEditing="GridView1_RowEditing" 
        OnRowCancelingEdit="GridView1_RowCancelingEdit" 
        OnRowDeleting="GridView1_RowDeleting" 
        OnRowUpdating="GridView1_RowUpdating" 
        onpageindexchanging="GridView1_PageIndexChanging" onrowupdated="GridView1_RowUpdated" 
        >

  <EmptyDataTemplate>
      <asp:Label ID="lblNoRecord" runat="server" Text="No Record Found" ForeColor="Red"> </asp:Label>
  </EmptyDataTemplate>

</asp:GridView>

现在,在rowUpdating事件中,我正在编写以下代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int mytext = Convert.ToInt16(GridView1.Rows[e.RowIndex].Cells[1].Text);
    string cConatiner = GridView1.Rows[e.RowIndex].Cells[4].Text;
}

在这里,myText给了我正确的值,即第一列,但当我将单元格值更改为1,2,3,4,5,6时,我变空了。

我做错了吗?

请帮帮我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我没有看到您使用mytext的值设置单元格值的位置。我将假设您尝试在下面的代码中设置Cell [4]的值:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int mytext = Convert.ToInt16(GridView1.Rows[e.RowIndex].Cells[1].Text);
    string cConatiner = GridView1.Rows[e.RowIndex].Cells[4].Text;

    GridView1.Rows[e.RowIndex].Cells[4].Text = mytext;
}

如果Cell [4]不想要您正在设置的单元格,请进行相应更改。