【问题标题】:How to delete from database selected item in ListView in C#如何从 C# 中 ListView 中的数据库中删除选定项
【发布时间】:2012-07-28 02:03:28
【问题描述】:

我正在使用 Microsoft SQL Server 和 Visual Studio-C# 2010 Ultimate。 我有一个 ListView 和其中的一些项目。当我选择一个项目并单击按钮时,我想删除它,但我无法编写 SqlCommandtext,也找不到 ListView 的选择事件。

【问题讨论】:

  • 您有 onItemDeleting 事件,您可以在其中删除行
  • 感谢您的回答。你能举个例子吗?例如 SqlCommand cmd = new SqlCommand("delete from Products where ProductID='" ???"'", connection);我会写什么???部分?
  • 您是在谈论使用 WPF 和 MVVM 吗?还是您在谈论 ASP.NET 列表视图?

标签: c# sql-server listview select


【解决方案1】:

使用listview c#从数据库中删除选定的数据

private void btnlvdeleterow_Click(object sender, EventArgs e)
{
      foreach (int i in Listview2.SelectedIndices)
        {
            string test = Listview2.Items[i].Text;
            Listview2.Items.Remove(Listview2.Items[i]);
            SQLiteCommand conn = new SQLiteCommand();
            conn.Connection = DbClass1.GetConnection();
            string del = "delete from UserData where UserName='" + test + "'";
            int result=dbclass1.ExecuteAndReturn(del);
        }
}

【讨论】:

    【解决方案2】:

    ListView 有一个可用的 SelectedIndex 属性。当您单击按钮时,传递此索引,然后您的 Sql 查询将类似于 从 ProductID = 'obj.ID' 的 Products 中删除,其中 obj 是从 listView.SelectedIndex 获得的

    【讨论】:

      【解决方案3】:
       protected void listview1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
          {
           //This retrieves the selected row 
           ListViewItem  item= listview1.Items [e.ItemIndex];
      
           //  Fetch the control for ProductId using findControl
            int productId=int.Parse((item.Findcontrol("ProductID") as TextBox).Text);
      
           //then use this column value in your sqlcommand
      
         using( SqlCommand cmd = new SqlCommand
              ("delete from Products where ProductID=@ProductId", connection  ))
          {
          command.Parameters.Add(new SqlParameter("ProductId", productId));
           //Then execute the query
          }
          }
      

      【讨论】:

        【解决方案4】:
        try
                    {
                        for (int j = 0; j <= listView2.Items.Count - 1; j++)
                        {
                            string test = listView2.SelectedItems[j].SubItems[1].Text;
                            string MyConnection2 = "datasource=localhost;port=3306;username=root;password=root";
                            string Query = "delete from TABLE_NAME where COL_NAME='" + test + "'";
                            MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                            MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                            MySqlDataReader MyReader2;
                            MyConn2.Open();
                            MyReader2 = MyCommand2.ExecuteReader();
        
                            while (MyReader2.Read())
                            {
                            }
                            MyConn2.Close();
                            MessageBox.Show("Data Deleted");
                           // txtCustomerName.Text = test;
                            //listView2.Items.Remove(listView2.Items[i]);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
        

        【讨论】:

        • 格式化有点麻烦,你的代码的一些内部会很好。
        【解决方案5】:

        您要查找的事件应该是 ListView.ItemSelectionChanged 其中 eventArgs 包含所选项目

        【讨论】:

        • 问题表明删除是在单击按钮时完成的,而不是在更改所选项目时完成。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        • 2017-12-11
        • 2017-02-12
        • 1970-01-01
        相关资源
        最近更新 更多