【问题标题】:delete button on unbound gridview - C#未绑定的gridview上的删除按钮 - C#
【发布时间】:2011-07-08 12:20:51
【问题描述】:

谁能告诉我如何在未绑定的 gridview 上触发 sql delete 命令? Is 显示 LINQ 搜索的结果,我将 AutoGenerateDeleteButton 设置为 true,但我不确定如何将其链接到删除查询。

谢谢

【问题讨论】:

    标签: c# .net asp.net linq gridview


    【解决方案1】:

    每当点击 Delete 按钮时,GridView 的 RowCommand 事件就会触发,您可以在此处通过命令名称检查它,例如..e.CommandName == "Delete"

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            // Put your Deletion code here.....     
        }
    }
    

    【讨论】:

      【解决方案2】:

      请参阅Insert, Update, and Delete Operations (LINQ to SQL) 主题,了解如何使用 LINQ 实现这些操作。

      【讨论】:

      • 我使用 LINQ 足够舒服,它是通过未绑定的 gridview 上的删除按钮触发它我不确定...
      • 对不起,我好像没听懂你的问题……现在也不懂:-(。你能澄清一下吗?
      • 没问题 - 在用户单击搜索按钮后,我有一个填充了 LINQ 查询的 unboud gridview。我希望gridview上的删除按钮的工作方式与绑定到sql数据源的gridview上的工作方式相同,并指定了更新和删除查询。我已将 AutoGenerateDeleteButton 设置为 true,那么如何将单击此按钮的用户链接到 LINQ 删除查询?
      【解决方案3】:

      这是我的代码 Theresa,希望对您有所帮助。

      private void deleteButton_Click(object sender, RoutedEventArgs e)
         {
             try
             {
                 DBConnDataContext db = new DBConnDataContext();
                 tbWellClassification shortName = TableGrid.SelectedItem as tbWellClassification;
                 var well = (from s in db.tbWellClassifications
                             where s.shortName == shortName.shortName
                             select s).Single();
                 db.tbWellClassifications.DeleteOnSubmit(well);
                 db.SubmitChanges();
      
                 MessageBox.Show("Row Deleted Successfully.");
      
                 txtStatus.Text = "Row Deleted";
      
                 db = null;
                 DBConnDataContext db2 = new DBConnDataContext();
                 TableGrid.ItemsSource = db2.tbWellClassifications;
      
                 TableGrid.Items.Refresh();
             }
             catch
             {
                 MessageBox.Show("Delete Unsuccessful");
             }
      
         }
      

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        • 1970-01-01
        • 2019-04-06
        相关资源
        最近更新 更多