求助:这段代码错在哪?

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属性是变了的.
...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
maysaber 2011-12-05
  • 打赏
  • 举报
回复
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";
}
//将原先的listbox的数据源置为null就可以试试
this.listBox1.DataSource = null;
this.listBox2.DataSource = null;
Code従業員 2011-12-05
  • 打赏
  • 举报
回复
太长,如果是语法错误请贴出异常部分,如果是逻辑错误。。。那就算了。。。。
MKing0412 2011-12-05
  • 打赏
  • 举报
回复
两个集合,从数据库取数据到集合中,然后两个ListBox分别绑定集合,按钮点击后,将数据从一个集合中转移到另一个集合,接着重新绑定,可是为什么我点击了按钮.界面上的数据还是没变,我跟踪调试发现集合的Count属性是变了的.

according to this

界面问题
初始的时候绑定过没?

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧