简体   繁体   English

C# 关于删除数据网格的问题

[英]C# Question About Delete Data Grid

I have binded a data grid to an array.我已将数据网格绑定到数组。 Also, there is a button there to delete the row.此外,还有一个按钮可以删除该行。 The problem is that I am not sure how to implement it since the data source is an array.问题是我不确定如何实现它,因为数据源是一个数组。

See below见下文

  <Columns>    
     <asp:TemplateColumn>        
            <ItemTemplate>          
       <asp:Label ID="lblItems" runat="server" Text='<%# Container.DataItem>' />         
       </ItemTemplate>    
     </asp:TemplateColumn>     
        <asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete">
          </asp:ButtonColumn>
                 </Columns> 

and here I would like to implement it..在这里我想实现它..

   private void DataGrid1_DeleteCommand(object source,
                  System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        int rowToDelete = e.Item.ItemIndex;

        myDataGrid.DataBind();
    }

In the code for the deletion, how can I access the index of my array based on the button clicked (per row)?在删除代码中,如何根据单击的按钮(每行)访问数组的索引?

Here is an example这是一个例子

Markup.标记。

<asp:DataGrid ID="DataGrid1" runat="server"
        AutoGenerateColumns="False" 
        OnDeleteCommand="DataGrid1_DeleteCommand">
    <Columns>
        <asp:TemplateColumn HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblItems" runat="server" 
                    Text='<%# Container.DataItem %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:ButtonColumn ButtonType="PushButton" 
                CommandName="Delete" 
                HeaderText="Actions" 
                Text="Delete">
        </asp:ButtonColumn>
    </Columns>
</asp:DataGrid>

Code-behind.代码隐藏。

private static string[] names = new string[] { "Matt", "Joanne", "Robert" };
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindGrid();
    }
}
private void BindGrid()
{
    DataGrid1.DataSource = names;
    DataGrid1.DataBind();
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
    string deletedItem = ((Label) DataGrid1.Items[e.Item.ItemIndex].FindControl("lblItems")).Text;
    names = names.Where(val => val != deletedItem).ToArray();
    BindGrid();
}

Hope this helps.希望这可以帮助。

Have you tried to access your src object using the method below (notice this is 'RowDeleting' event on gridview)?您是否尝试过使用以下方法访问您的 src object(注意这是 gridview 上的“RowDeleting”事件)?

    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        MyObject o = (MyObject)gv.Rows[e.RowIndex].DataItem;
    }

There are also other tricks like storing an id in a hiddenfield on the row and then on the delete command you search for the control and pluck your value.还有其他技巧,例如将 id 存储在行的隐藏字段中,然后在删除命令上搜索控件并提取值。 My preference is the method above but really depends on what your goal is.我的偏好是上述方法,但实际上取决于您的目标是什么。

    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        HiddenField field = (HiddenField)gv.Rows[e.RowIndex].FindControl("myHiddenField");
        string myValue = field.Value;

        // delete it and rebind
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM