求助:这段代码错在哪?
zq2it 2011-12-05 09:06:20 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string sqlconnstr = @"Data Source=.;Initial Catalog=test;User ID=sa; Pwd=****;";
private List<Sound> ListBox1Data;
private List<Sound> ListBox2Data;
private void Form1_Load(object sender, EventArgs e)
{
initlist();
binddata();
}
private void initlist()
{
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlconn;
sqlcmd.CommandText = "select * from sound where category = 0 order by sort";
sqlconn.Open();
SqlDataReader sdr = sqlcmd.ExecuteReader();
ListBox1Data = new List<Sound>();
ListBox2Data = new List<Sound>();
while (sdr.Read())
{
Sound s = new Sound();
s.Sid = Convert.ToInt32(sdr["sid"]);
s.Sname = sdr["sname"].ToString();
s.category = Convert.ToInt32(sdr["category"]);
ListBox1Data.Add(s);
}
sdr.Close();
binddata();
SqlCommand sqlcmd2 = new SqlCommand("select * from sound where category = 1 order by sort", sqlconn);
SqlDataReader sdr2 = sqlcmd2.ExecuteReader(CommandBehavior.CloseConnection);
while (sdr2.Read())
{
Sound s = new Sound();
s.Sid = Convert.ToInt32(sdr2["sid"]);
s.Sname = sdr2["sname"].ToString();
s.category = Convert.ToInt32(sdr2["category"]);
ListBox2Data.Add(s);
}
}
private void binddata()
{
this.listBox1.DataSource = ListBox1Data;
this.listBox1.DisplayMember = "sname";
this.listBox1.ValueMember = "sid";
this.listBox2.DataSource = ListBox2Data;
this.listBox2.DisplayMember = "sname";
this.listBox2.ValueMember = "sid";
}
private void button1_Click(object sender, EventArgs e)
{
Sound item = this.listBox1.SelectedItem as Sound;
ListBox1Data.Remove(item);
ListBox2Data.Add(item);
binddata();
}
}
}
两个集合,从数据库取数据到集合中,然后两个ListBox分别绑定集合,按钮点击后,将数据从一个集合中转移到另一个集合,接着重新绑定,可是为什么我点击了按钮.界面上的数据还是没变,我跟踪调试发现集合的Count属性是变了的.