Microsoft SQL Server と Visual Studio-C# 2010 Ultimate を使用しています。ListView とその中にいくつかのアイテムがあります。アイテムを選択してボタンをクリックしたときにアイテムを削除したいのですが、SqlCommandtext を記述できず、ListView の select イベントが見つかりませんでした。
5875 次
5 に答える
1
Deleting selected data from database using 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);
}
}
于 2014-06-05T13:58:58.770 に答える
0
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);
}
于 2016-03-02T09:03:30.337 に答える
0
ListView で使用できる SelectedIndex プロパティがあります。ボタンをクリックしてこのインデックスを渡すと、Sql クエリは delete from Products where ProductID = 'obj.ID' のようになり、obj は listView.SelectedIndex から取得されます。
于 2012-07-30T12:31:03.170 に答える
0
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
}
}
于 2012-07-30T12:35:08.950 に答える
-1
探しているイベントは ListView.ItemSelectionChanged で、eventArgs には選択したアイテムが含まれています。
于 2012-07-30T12:27:41.970 に答える