【问题标题】:Update gridview from code behind in asp.net从 asp.net 中的代码更新 gridview
【发布时间】:2010-05-29 09:59:08
【问题描述】:

我的 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 时,我变得空了。

我做错了吗?

请帮帮我。

提前致谢。

【问题讨论】:

  • 请帮忙,我找不到解决办法
  • 我得到你的 cConatiner 总是空的。是你的问题吗?您是否检查了从 DB/(其他来源)获取的数据?
  • 我正在从数据集中绑定我的gridview,并且gridview 显示9 列和16 行,我无法获取单元格的值。

标签: c# asp.net gridview gridviewrow


【解决方案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] 不需要您设置的单元格,请适当更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    相关资源
    最近更新 更多