【问题标题】:Deleting an item from a listbox and data source c#从列表框和数据源 c# 中删除项目
【发布时间】:2014-04-15 12:01:11
【问题描述】:

我试图从列表框和数据表中删除一个项目,但我正在努力做到这一点。我目前可以将其从列表框中删除,但我不知道如何将其从表中删除。

这是我的代码

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 记录计数; clsDataConnection MyConnection = 新的 clsDataConnection(); MyConnection.Execute("sproc_tblToDo_SelectAll"); RecordCount = MyConnection.Count; 返回记录计数;

    }


    }


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;
    }


    }
}

【问题讨论】:

  • 我们在说什么表? SQL 表或其他一些数据源?
  • 创建一个方法,该方法将访问您的数据库并删除您的行,然后更新列表。你会在你的btnDelete_Click方法中调用这个方法
  • 你能显示你班级的代码MyList。您可以从那里获得帮助。

标签: c# sql webforms


【解决方案1】:

创建一个变量并将值设置为等于选择的任何项目。

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"
}
}

【讨论】:

    【解决方案2】:

    类似这样的:

    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()
    }
    }
    

    【讨论】:

      【解决方案3】:

      视图模型:

          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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        • 1970-01-01
        • 2013-04-20
        • 2020-12-07
        • 2010-11-29
        相关资源
        最近更新 更多