ListBox操作(绑定、添加、删除、上移、下移、添加全部、删除……)

本文介绍如何在C#中通过绑定DataSet到ListBox,并实现数据的选择、添加、删除、上移、下移及添加全部、删除清空等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   //绑定ListBox1
    private void ListBox1Bind()
    {
        .....
        DataSet ds = new DataSet();
        ListBox1.DataSource = ds.Tables[0].DefaultView; //ds 为 DataSet 对象
        ListBox1.DataTextField = "textFld"; //textFld 为字段名称
        ListBox1.DataValueField = "valueFld"; //valueFld 为字段名称
        ListBox1.DataBind();
    }   

    //添加
    protected void Addbtn_Click(object sender, EventArgs e)
    {
        int i = 0;
        while (i < ListBox1.Items.Count)
        {
            if (ListBox1.Items[i].Selected == true)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
            else
                i += 1;
        }
    }

    //删除
    protected void Deletebtn_Click(object sender, EventArgs e)
    {
        int i = 0;
        while (i < ListBox2.Items.Count)
        {
            if (ListBox2.Items[i].Selected == true)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
            else
                i += 1;
        }
    }

    //上移
    protected void Upbtn_Click(object sender, EventArgs e)
    {
        //若不是第一行则上移
        if (ListBox2.SelectedIndex > 0)
        {
            string name = ListBox2.SelectedItem.Text;
            string ID = ListBox2.SelectedItem.Value;
            int index = ListBox2.SelectedIndex;
            ListBox2.SelectedItem.Text = ListBox2.Items[index - 1].Text;
            ListBox2.SelectedItem.Value = ListBox2.Items[index - 1].Value;
            ListBox2.Items[index - 1].Text = name;
            ListBox2.Items[index - 1].Value = ID;
            ListBox2.SelectedIndex--;
        }
    }

    //下移
    protected void Downbtn_Click(object sender, EventArgs e)
    {
        //若不是最后一行则下移
        if (ListBox2.SelectedIndex >= 0 && ListBox2.SelectedIndex < ListBox2.Items.Count - 1)
        {
            string name = ListBox2.SelectedItem.Text;
            string ID = ListBox2.SelectedItem.Value;
            int index = ListBox2.SelectedIndex;
            ListBox2.SelectedItem.Text = ListBox2.Items[index + 1].Text;
            ListBox2.SelectedItem.Value = ListBox2.Items[index + 1].Value;
            ListBox2.Items[index + 1].Text = name;
            ListBox2.Items[index + 1].Value = ID;
            ListBox2.SelectedIndex++;
        }
    }
  //添加全部、删除清空
  protected void Button5_Click(object sender, EventArgs e)
        {
            int i = 0;
            while (i < ListBox2.Items.Count)
            {
               ListBox1.Items.Add(ListBox2.Items[i]);
               i++;
            }
            ListBox2.Items.Clear();
        }

 

转地址:http://hi.baidu.com/lzg1018/blog/item/6c0d4acd335d3f560eb345b7.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值