如何在选择列表框的特定项目时删除列表框中的元素 [英] how elements which is in listbox can be deleted on selecting particular item of listbox
本文介绍了如何在选择列表框的特定项目时删除列表框中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用C#.NET和SQL SERVER 2008制作桌面应用程序.我已经采取了一个列表框的形式,其中包含从数据库中获取的元素.现在我希望在选择列表框中存在的项目时应该将其删除我已经按下了删除按钮
我有一个包含名称的列表框名称listbox1和要删除的btndelete
以下是我尝试过的编码
I am making desktop application using C#.NET and SQL SERVER 2008.i have taken a listbox in a form containing elements which is fetched from database.Now i want that on selecting the item which is present in the listbox should get delete I have taken a delete button
i have listbox name listbox1 containing items and btndelete to delete
below is the coding that i have tried
private void btndeleteresources_Click(object sender, EventArgs e)
{
SqlDataAdapter adp = new SqlDataAdapter("delete from Resources_Required where Resources=''"+listBox1.SelectedIndex+"''", "Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True");
DataSet ds = new DataSet();
adp.Fill(ds, "Resources_Required");
listBox1.DataSource = ds.Tables[0];
listBox1.DisplayMember = "Resources";
listBox1.ValueMember = "Resources";
cboresources.Focus();
}
推荐答案
使用这个
这里"conn"是您的连接
Hi,
Use this
Here "conn" is your connection
private void btndeleteresources_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("delete from Resources_Required where Resources='"+listBox1.SelectedItems[0].ToString() +"'", conn);
cmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter("Select * from Resources_Required","Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True" );
DataSet ds = new DataSet();
adp.Fill(ds, "Resources_Required");
listBox1.DataSource = ds.Tables[0];
listBox1.DisplayMember = "Resources";
listBox1.ValueMember = "Resources";
cboresources.Focus();
}
试试吧.
问候
AR
Try it.
Regards
AR
我不确定此代码是否会删除.
试试这个,
I''m not sure this code will delete or not.
Try this,
string selCmdString = "SELECT *FROM Resources_Required";
SqlCommand selCommand = new SqlCommand(selCmdString, conn);
string delCmdString = "DELETE FROM Resources_Required WHERE Resources=@Res";
SqlCommand delCommand = new SqlCommand(delCmdString, conn);
SqlDataAdapter adp = new SqlDataAdapter(selCommand);
adp.DeleteCommand = delCommand;
//Adding one more thing, try this
DataRowView curRowView = (DataRowView)listBox1.SelectedItem;
String resources = curRowView["Resources"].ToString(); // Will return the current value in selected row of Column Resources
//Use SqlParameters to avoid SQL injections. Keep it as practice.
SqlParameter param = New SqlParameter();
param.ParameterName = "@Res";
//param.Value = listBox1.SelectedItem.ToString();
param.Value = resources;
param.SqlDbType = SqlDbType.SmallInt;
delCommand.Parameters.Add(param);
DataSet ds = new DataSet();
adp.Fill(ds,"Resources_Required");
DataTable dt = ds.Tables["Resources_Required"];
foreach (DataRow ro in dt.Rows)
{
if (ro["Resources"].ToString() == resources)
{
ro.Delete();
break;
}
}
adp.Update(ds,"Resources_Required");
listBox1.DataSource = ds.Tables[0];
listBox1.DisplayMember = "Resources";
listBox1.ValueMember = "Resources";
cboresources.Focus();
立即尝试.
Try now.
这篇关于如何在选择列表框的特定项目时删除列表框中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文