c#listboxt如何显示数据库的内容?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyWindows
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
LoadListBox();
}
/// <summary>
/// 加载listbox
/// </summary>
private void LoadListBox()
{
IList<Student> list = GetData();
if (list.Count > 0) listBox1.Items.Clear();//如果集合数据大于0条,清除原有的listbox项
foreach (Student s in list)
{
listBox1.Items.Add(s);//此处加的虽然是实体对象,但是咱们已经重写的tostring方法,所以显示的还是为姓名
}
}
/// <summary>
/// 获取数据,此处模拟从数据库拿到得数据
/// </summary>
private IList<Student> GetData()
{
IList<Student> list = new List<Student>();
Student s1 = new Student(1, "张三", true);
Student s2 = new Student(2, "李四", true);
Student s3 = new Student(3, "王五", false);
Student s4 = new Student(4, "赵六", true);
Student s5 = new Student(5, "侯七", false);
list.Add(s1);
list.Add(s2);
list.Add(s3);
list.Add(s4);
list.Add(s5);
return list;
}
}
public class Student
{
/// <summary>
/// 学生ID
/// </summary>
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 学生姓名
/// </summary>
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// 学生性别
/// </summary>
private bool gender;
public bool Gender
{
get { return gender; }
set { gender = value; }
}
public Student(int id, string name, bool gender)
{
this.id = id;
this.name = name;
this.gender = gender;
}
/// <summary>
/// 重写ToString方法,返回学生姓名
/// </summary>
/// <returns></returns>
public override string ToString()
{
return name;
}
}
}
这样写的好处在于,当选中某项时,实际拿到的是实体对象本身。
listBox1.DataSource = 数据源;
这样也可以加载,不过灵活性不高
自己理解下。希望对你有用
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyWindows
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
LoadListBox();
}
/// <summary>
/// 加载listbox
/// </summary>
private void LoadListBox()
{
IList<Student> list = GetData();
if (list.Count > 0) listBox1.Items.Clear();//如果集合数据大于0条,清除原有的listbox项
foreach (Student s in list)
{
listBox1.Items.Add(s);//此处加的虽然是实体对象,但是咱们已经重写的tostring方法,所以显示的还是为姓名
}
}
/// <summary>
/// 获取数据,此处模拟从数据库拿到得数据
/// </summary>
private IList<Student> GetData()
{
IList<Student> list = new List<Student>();
Student s1 = new Student(1, "张三", true);
Student s2 = new Student(2, "李四", true);
Student s3 = new Student(3, "王五", false);
Student s4 = new Student(4, "赵六", true);
Student s5 = new Student(5, "侯七", false);
list.Add(s1);
list.Add(s2);
list.Add(s3);
list.Add(s4);
list.Add(s5);
return list;
}
}
public class Student
{
/// <summary>
/// 学生ID
/// </summary>
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 学生姓名
/// </summary>
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// 学生性别
/// </summary>
private bool gender;
public bool Gender
{
get { return gender; }
set { gender = value; }
}
public Student(int id, string name, bool gender)
{
this.id = id;
this.name = name;
this.gender = gender;
}
/// <summary>
/// 重写ToString方法,返回学生姓名
/// </summary>
/// <returns></returns>
public override string ToString()
{
return name;
}
}
}
这样写的好处在于,当选中某项时,实际拿到的是实体对象本身。
listBox1.DataSource = 数据源;
这样也可以加载,不过灵活性不高
自己理解下。希望对你有用
展开全部
大致上和list一样的,都是指定数据源(DataSource),然后绑定数据源(DataBind).
更多追问追答
追问
能不能具体一些?
追答
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//数据库链接
string connectionStr = "server=.;database=demo;uid=sa;pwd=sa";//链接数据库的字符串
SqlConnection connection = new SqlConnection(connectionStr);//创建数据库链接
//从数据中读取数据
SqlDataAdapter sda = new SqlDataAdapter("select name from item",connection);
DataSet ds = new DataSet("proc");
sda.Fill(ds,"proc");//将数据源指向dataset中
//绑定数据源
listBox1.DataSource = ds;
}
}
}
有时间的话 我们QQ聊:1366195575
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//数据库链接
SqlConnection con =new SqlConnection( "server=localhost;database=db;uid=sa;pwd=");
//从数据中读取数据
SqlDataAdapter sda = new SqlDataAdapter("select name from stu",con);
DataSet ds = new DataSet();
sda.Fill(ds);//将数据源指向dataset中
//绑定数据源
listBox1.DataSource = ds.DefaultView;
listBox1.DataBind();
}
}
}
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//数据库链接
SqlConnection con =new SqlConnection( "server=localhost;database=db;uid=sa;pwd=");
//从数据中读取数据
SqlDataAdapter sda = new SqlDataAdapter("select name from stu",con);
DataSet ds = new DataSet();
sda.Fill(ds);//将数据源指向dataset中
//绑定数据源
listBox1.DataSource = ds.DefaultView;
listBox1.DataBind();
}
}
}
追问
这个和上面的也是差不多呀,感谢!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询