簡體   English   中英

如何將所選項目從列表框移動到另一個

[英]how to move selected item from listbox to another

我創建兩個 class 列表並從兩個列表中填充它們和我的數據源代碼列表框,例如下面的代碼

  class Classgreat
{
    public int machid { get; set; }
    public string lm { get; set; }
    public int idline { get; set; }
}
  class Classskill
{
    public int machid { get; set; }
    public string lm { get; set; }
    public int idline { get; set; }
}
 public void fill_per_skill()
    {
        SqlConnection cs = new SqlConnection("");
        SqlDataAdapter adapter = new SqlDataAdapter("", cs);

        DataTable dt = new DataTable();
        adapter.Fill(dt);
        skill = (from DataRow dr in dt.Rows
            select new Classskill()
            {
                machid = Convert.ToInt32(dr["machid"]),
                lm = dr["lm"].ToString(),
                idline = Convert.ToInt32(dr["idline"]),
            }).ToList();
        listBoxrole.DataSource = null;
        listBoxrole.Items.Clear();
        this.listBoxrole.DataSource =skill ;
        this.listBoxrole.DisplayMember = "lm";
        this.listBoxrole.ValueMember = "machid";
    }
 public void fill_per_great()
        {            
             SqlConnection cs = new SqlConnection("");
            SqlDataAdapter adapter = new SqlDataAdapter("", cs);

            DataTable dt = new DataTable();
            adapter.Fill(dt);
            great = (from DataRow dr in dt.Rows
                select new Classgreat()
                {
                    machid = Convert.ToInt32(dr["machid"]),
                    lm = dr["lm"].ToString(),
                    idline = Convert.ToInt32(dr["idline"]),
                }).ToList();
            listBoxgreat.DataSource = null;
            listBoxgreat.Items.Clear();
            this.listBoxgreat.DataSource =great ;
            this.listBoxgreat.DisplayMember = "lm";
            this.listBoxgreat.ValueMember = "machid";
        }

這工作正常但是當我想在兩個列表(技能和偉大)之間移動項目時給出錯誤無法將 class 偉大轉換為 class 技能並且不起作用我該怎么做

你的類在數據方面是相同的,所以為什么有兩個......?

相反,我可能會考慮將您的數據留在數據表中,因為 c# 中已經有很多功能可以過濾它們

    public void FillAll()
    {            
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT machid, lm, idline, 'g' as what FROM ...", "conn str here");

        DataTable dt = new DataTable();
        adapter.Fill(dt); // Fill greats 

        adapter.SelectCommand.CommandText = "SELECT machid, lm, idline, 's' as what FROM ..."; 

        adapter.Fill(dt); // Fill skills into same table with different value of What column
        
        this.listBoxgreat.DisplayMember = "lm";
        this.listBoxgreat.ValueMember = "machid";
        this.listBoxgreat.DataSource = new DataView(dt) { RowFilter = "[what]='g'" };

        this.listBoxSkill.DisplayMember = "lm";
        this.listBoxSkill.ValueMember = "machid";
        this.listBoxSkill.DataSource = new DataView(dt) { RowFilter = "[what]='s'" };
    }

將所有數據填充到同一個表中,並使用一列來區分它們。 使用環繞數據表並提供過濾功能的 DataView,僅將 What=s 過濾到 Skill 列表中,將 What=g 過濾到 Great 列表中

現在,如果您直接在數據表中更改內容,它將導致項目移動列表

(listboxSkill.SelectedItem as DataRowView).Row["what"] = "g"; // it will move from skill to great

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM