【问题标题】:GridView in OnRowDeleting no rows are foundOnRowDeleting 中的 GridView 未找到任何行
【发布时间】:2016-04-20 23:51:43
【问题描述】:

我在页面上有几个 GridView,它们都有问题。当 OnRowDeleting 函数被触发时,GridView(和发件人)找不到任何行。我通过以下方式创建了 GridView:

        <asp:GridView ID="gridview9" runat="server" 
        DataKeyNames="id" 
        OnRowDeleting="deleteNumberFrom9" 
        AutoGenerateColumns="false"
        CssClass="mGrid"
        AlternatingRowStyle="alt">
        <Columns>
            <asp:boundfield datafield="product_key" headertext="PRODUCT KEY"  ItemStyle-HorizontalAlign="Center" />
            <asp:boundfield datafield="first_name" headertext="FIRST NAME"/>
            <asp:boundfield datafield="last_name" headertext="LAST NAME"/>
            <asp:boundfield datafield="email" headertext="EMAIL"/>
            <asp:boundfield datafield="phone_number" headertext="PHONE NUMBER"  ItemStyle-HorizontalAlign="Center" />
            <asp:CommandField HeaderText="DELETE NUMBER" ShowDeleteButton="True"  ItemStyle-HorizontalAlign="Center" DeleteText="Delete" />
        </Columns>
    </asp:GridView>   

网格由 MySQL 数据库填充:

    GridView gv = gridview9;
    String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ToString();
    MySqlConnection connection = new MySqlConnection(connectionString);
    connection.Open();
    MySqlCommand command = new MySqlCommand(queryString, connection);
    if (addPhone)       command.Parameters.AddWithValue("@phone_number", "%" + phoneNumber + "%");
    if (addEmail)       command.Parameters.AddWithValue("@email", "%" + email + "%");
    if (addFirstName)   command.Parameters.AddWithValue("@first_name", "%" + firstName + "%");
    if (addLastName)    command.Parameters.AddWithValue("@last_name", "%" + lastName + "%");
    if (addProductKey)  command.Parameters.AddWithValue("@product_key", "%" + productKey + "%");
    MySqlDataAdapter adapter = new MySqlDataAdapter(command);
    System.Data.DataSet ds = new System.Data.DataSet();
    adapter.Fill(ds);
    gv.DataSource = ds;
    gv.DataBind();
    gv.Visible = true;

最后,onRowDeleting函数是:

    protected void deleteNumberFrom9(object sender, GridViewDeleteEventArgs e)
    {
        int SID = Convert.ToInt32(gridview9.DataKeys[e.RowIndex].Value);
        String phone_number = e.Values[4].ToString();
        deleteNumber(SID, phone_number, 9);
    }

当我运行页面时,gridview9 正确返回 2 行,但是当我单击其中一行中的删除并且 deleteNumberFrom9 被触发时,gridview9.Rows.Count 是 0 而不是 2。当我将发件人添加到监视列表并查看行数也是0。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    删除数据库中的记录后,必须重新绑定数据到GridView:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    
    protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
    {
        ...
        deleteNumber(SID, phone_number, 9);
        BindData();
    }
    
    private void BindData()
    {
        ...
        gv.DataSource = ds;
        gv.DataBind();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      相关资源
      最近更新 更多