从列表框和数据源c#中删除项目
本文关键字:删除项目 数据源 列表 | 更新日期: 2023-09-27 17:58:19
我试图从列表框和数据表中删除一个项目,但我很难做到。我现在可以从列表框中删除它,但我不知道如何从表中删除它。
这是我的代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if this is the first time th page is displayed
if (IsPostBack == false)
{
//update the list box
DisplayToDoList();
}
}
//this function populates the list box with to do list items
//it returns the number of records in the list
Int32 DisplayToDoList()
{
//var to store the record count
Int32 RecordCount;
//create an instance of the todo class
clsToDoList MyList = new clsToDoList();
//get the count of records
RecordCount = MyList.Count;
//var to store the index
Int32 Index = 0;
//var to store the primary key
Int32 ToDoNo;
//var to store the to do text
String ToDo;
//clear the list
lstToDo.Items.Clear();
//loop through each record
while (Index < RecordCount)
{
//get the primary key
ToDoNo = MyList.List[Index].ToDoNo;
//get the text
ToDo = MyList.List[Index].ToDo;
//create a new entry for the list
ListItem NewEntry = new ListItem(ToDo, Convert.ToString(ToDoNo));
//add the entry to the list box
lstToDo.Items.Add(NewEntry);
//increment the indez
Index++;
}
//return the number of records
return RecordCount;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//add value from textbox to listbox
lstToDo.Items.Add(txtToDo.Text);
}
protected void btnDelete_Click(object sender, EventArgs e)
{
if (this.lstToDo.SelectedIndex >= 0)
this.lstToDo.Items.RemoveAt(this.lstToDo.SelectedIndex);
}
clsToDoList 的代码
公共类clsToDoList{公用Int32计数{收到{Int32 RecordCount;clsDataConnection MyConnection=新的clsDataConnection[();MyConnection.Execute("sproc_tblToDo_SelectAll");RecordCount=MyConnection.Count;return RecordCount;
}
}
public List<clsToDoItem>List
{
get
{
List<clsToDoItem> NewList = new List<clsToDoItem>();
Int32 RecordCount;
Int32 Index = 0;
clsDataConnection MyConnection = new clsDataConnection();
MyConnection.Execute("sproc_tblToDo_SelectAll");
RecordCount = MyConnection.Count;
while (Index < RecordCount)
{
clsToDoItem BlankPage = new clsToDoItem();
BlankPage.ToDoNo = Convert.ToInt32(MyConnection.DataTable.Rows[Index]["ToDoNo"]);
BlankPage.ToDo = Convert.ToString(MyConnection.DataTable.Rows[Index]["ToDo"]);
NewList.Add(BlankPage);
Index++;
}
return NewList;
}
}
}
创建一个变量,并将值设置为等于所选项目的值。
string _itemToDelete
protected void btnDelete_Click(object sender, EventArgs e)
{
if (this.lstToDo.SelectedIndex >= 0)
{
_itemToDelete = this.1stToDo.items.SelectedIndex.ToString() // or text cant remember
this.lstToDo.Items.RemoveAt(this.lstToDo.SelectedIndex);
connect to SQL Table
"DELETE FROM table WHERE column_name LIKE" + "_itemToDelete"
}
}
类似这样的东西:
public static void Delete(int ID)
{
using (SqlConnection conn = new SqlConnection())
{
conn.connectionString = "";
conn.open();
sqlcommand sc = new sqlcommand();
sc.connection = conn;
sc.commandText = "DELETE FROM TABLE WHERE ID = @ID";
sc.Parameters.AddWithValue("@ID", ID);
sc.ExecuteNonQuery();
conn.close()
}
}
ViewModel:
public MyList Items { get; private set; }
public ICommand Delete { get; private set; }
private void delete(object sender)
{
var item = sender as ItemType;
if (item == null)
return;
MyList.RemoveOnSubmit(item);
YourDataContextSQL.SubmitChanges();
}
视图:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="Delete"
Command="{Binding Delete}"
CommandParameter="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>