【问题标题】:How Do I Identify DataGrid Row for Deletion?如何识别要删除的 DataGrid 行?
【发布时间】:2020-01-08 08:46:59
【问题描述】:

我目前正在用 C# 重写我的一个旧的 VB.net 脚本。我在每一行都有一个 DataGrid 和一个 Delete 按钮。我想将给定的 Id (KeyphraseToBrandId) 传递给 OnDeleteCommand,以删除数据库中的该行。我无法识别此行。

这是我的 MyDataGrid_DeleteCommand,它显示了我尝试过但没有奏效的所有东西,如 cmets:

protected void MyDataGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
{

    String connStr = ConfigurationSettings.AppSettings["ConnectionString"];

    // Define db connection
    MySqlConnection conn = new MySqlConnection(connStr);

    String DeleteSql = "DELETE from KeyphrasesToBrands WHERE KeyphraseToBrandId=?KeyphraseToBrandId";

    MySqlCommand DeleteCommand = new MySqlCommand(DeleteSql, conn);

    // DeleteCommand.Parameters.Add("?KeyphraseToBrandId", MyDataGrid.DataKeys(CInt(E.Item.ItemIndex)));

// This works for Id 8, so it's not the rest of the code        
// DeleteCommand.Parameters.Add("?KeyphraseToBrandId", "8");

    // DeleteCommand.Parameters.Add("?KeyphraseToBrandId", MyDataGrid.SelectedDataKey.Value.ToString());

    // DeleteCommand.Parameters.Add("?KeyphraseToBrandId", MyDataGrid.DataKeys.Equals(e.Item.ItemIndex));

    // DeleteCommand.Parameters.Add("?KeyphraseToBrandId", e.Item.ItemIndex.ToString());

    // DeleteCommand.Parameters.Add("?KeyphraseToBrandId", e.Item.ItemIndex);

        // DeleteCommand.Parameters.Add("?KeyphraseToBrandId", e.CommandArgument.ToString());

        // DeleteCommand.Parameters.Add("?KeyphraseToBrandId", e.Item.Cells[0].ToString());

        DeleteCommand.Parameters.Add("?KeyphraseToBrandId", e.CommandArgument);

        DeleteCommand.Connection.Open();

        DeleteCommand.ExecuteNonQuery();

        BindGrid();

        DeleteCommand.Connection.Close();

    }

...这是我的 DataGrid:

<asp:DataGrid ID="MyDataGrid" runat="server" DataKeyField="KeyphraseToBrandId" OnCancelCommand="MyDataGrid_CancelCommand" OnDeleteCommand="MyDataGrid_DeleteCommand" OnSelectedIndexChanged="MyDataGrid_SelectedIndexChanged" AutoGenerateColumns="false">

    <Columns>

            <asp:BoundColumn DataField="KeyphraseText" HeaderText="Keyphrase Text"></asp:BoundColumn> 

            <asp:BoundColumn DataField="BrandName" HeaderText="Brand Name"></asp:BoundColumn>

            <asp:BoundColumn DataField="KeyphraseToBrandId" HeaderText="Keyphrase To Brand Id"></asp:BoundColumn>

            <asp:ButtonColumn ButtonType="PushButton" Text="Delete" CommandName="Delete" HeaderText="Delete?" />

    </Columns>

</asp:DataGrid>

如何将 KeyphraseBrandId 传递给删除命令,以识别该行以在数据库中删除?

谢谢

【问题讨论】:

    标签: c# datagrid crud


    【解决方案1】:

    如果你想批量删除选定的行,那么你可以这样做

    foreach (DataGridViewRow row  in yourDataGridView.SelectedRows)
    {    
         yourDataGridView.Rows.RemoveAt(row.Index);
    }
    

    这是查找索引(DataGrid 中列出的行的标识)的工作。它临时删除 Rows,如果您想从数据库本身中删除数据,您需要从 Row 获取身份 ID 并根据您的要求将其删除或更改活动状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多