DataGrid中的删除和修改

博客展示了ASP中DataGrid控件的使用,包括列的设置,如隐藏主键列、超链接列等。还介绍了为DataGrid添加事件处理函数,根据CommandName实现删除和修改操作,同时提醒在page_load里添加isPostBack。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
       <Columns>
        <asp:BoundColumn Visible="False" DataField="id" DataFormatString="{0}"></asp:BoundColumn>
        <asp:HyperLinkColumn HeaderText="物资名称" DataNavigateUrlField="id" DataNavigateUrlFormatString="./ProductDetail.aspx?productID={0}"
         DataTextField="name" DataTextFormatString="{0}"></asp:HyperLinkColumn>
        <asp:BoundColumn HeaderText="型号" DataField="type" DataFormatString="{0}"></asp:BoundColumn>
        <asp:BoundColumn HeaderText="数量" DataField="quantity" DataFormatString="{0}"></asp:BoundColumn>
        <asp:BoundColumn HeaderText="单位" DataField="unit" DataFormatString="{0}"></asp:BoundColumn>
        <asp:BoundColumn HeaderText="截止日期" DataField="expirationDate" DataFormatString="{0}"></asp:BoundColumn>
        <asp:ButtonColumn HeaderText="删除" ButtonType="PushButton" DataTextField="id" CommandName="del" DataTextFormatString="删除{0}">
         <ItemStyle Font-Size="Smaller" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Aqua"
          VerticalAlign="Middle" BackColor="Black"></ItemStyle>
        </asp:ButtonColumn>
        <asp:ButtonColumn HeaderText="修改" ButtonType="PushButton" DataTextField="id" CommandName="update"
         DataTextFormatString="修改{0}">
         <ItemStyle Font-Size="Smaller" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Aqua"
          VerticalAlign="Middle" BackColor="Black"></ItemStyle>
        </asp:ButtonColumn>
       </Columns>
      </asp:DataGrid>

//第一列不显示,但是它记载着整个grid的纪录的主键,删除修改根据它来找到相应的纪录

给grid添加事件处理函数

   this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.ExcuteItemCommand);

相应的处理如下


  private void ExcuteItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string ItemID = e.Item.Cells[0].Text;
   if(e.CommandName=="del"){
    ProductManager.DeleteProduct(ItemID);
    this.Response.Redirect("./MyProductList.aspx?IsBuy="+IsBuy);
   }
   else if(e.CommandName=="update"){
    this.Response.Redirect("./UpdateProduct.aspx?productID="+ItemID+"&IsBuy="+IsBuy);
   }
  }

CommandName是从

<asp:ButtonColumn HeaderText="修改" ButtonType="PushButton" DataTextField="id" CommandName="update"
         DataTextFormatString="修改{0}">
过来的。

//需要注意的是,在page_load里面要价上一句,isPostBack

在WPF中,DataGrid是一种用于显示表格数据的强大控件。对于行数据的删除修改操作,通常涉及以下几个步骤: 1. **设置`SelectionMode`**: DataGrid默认的`SelectionMode`可能是单选或多选,你需要设置为“Multiple”以便支持多行选择。这可以通过设置`SelectionMode="Multiple"`在XAML中完成。 ```xml <DataGrid SelectionMode="Multiple"> ``` 2. **响应`SelectionChanged`事件**: 添加一个`SelectionChanged`事件处理程序,检查当前选中的行,并提供相应的操作。 ```csharp private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0) { var selectedRows = dataGrid.SelectedItems.Cast<DataRowView>(); foreach (var row in selectedRows) { // 删除操作 dataGrid.Items.Remove(row.Row); // 修改操作,假设有一个TextBlock用于编辑 var textBlock = row.ItemContainerGenerator.ContainerFromIndex(row.ItemIndex).FindName("YourTextBoxName") as TextBlock; if (textBlock != null) { // 获取并更新文本 string newValue = textBlock.Text; // 这里替换为你实际的修改逻辑 textBlock.Text = newValue; } } } } ``` 在上述代码中,`dataGrid.SelectedItems`获取的是选中的行,然后你可以遍历它们执行删除修改操作。注意,实际修改时需要替换`textBlock.Text = newValue;`为实际的数据更改逻辑。 3. **提供确认提示**: 在删除前,可以添加一个确认对话框或者使用`MessageBox.Show`来让用户确认是否真的要删除。 4. **验证持久化数据**: 如果数据需要保存到数据库或其他存储,记得在操作完成后做相应的同步工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值