C# 报表控件能不能使用list作为数据源?可以的话给个实际的例子
2个回答
2015-03-06
展开全部
BindingList<Student> list;
public Form1()
{
InitializeComponent();
list = new BindingList<Student>();
ComboBox sex = new ComboBox();
sex.Items.Add("男");
sex.Items.Add("女");
list.Add(new Student("1", "Ray", 22, sex));
list.Add(new Student("2", "Lily", 36, sex));
list.Add(new Student("3", "Lucy", 25, sex));
list.Add(new Student("4", "Ran", 19, sex));
list.Add(new Student("5", "Jermy", 32, sex));
list.Add(new Student("6", "Dem", 46, sex));
list.Add(new Student("7", "Jack", 34, sex));
list.Add(new Student("8", "Done", 28, sex));
this.dataGridView1.DataSource = list;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.AllowUserToResizeRows = false;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
Random indexR = new Random();
Random ageR = new Random();
int index = indexR.Next(0, 8);
int age = ageR.Next(15, 80);
list[index].Age = age;
this.dataGridView1.Refresh();
}
}
public class Student
{
private string id;
private string name;
private int age;
private ComboBox sex;
public Student(string id, string name, int age,ComboBox sex)
{
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public string ID
{
set
{
this.id = value;
}
get
{
return this.id;
}
}
public string Name
{
set
{
this.name = value;
}
get
{
return this.name;
}
}
public int Age
{
set
{
this.age = value;
}
get
{
return this.age;
}
}
public ComboBox Sex
{
set
{
this.sex = value;
}
get
{
return this.sex;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询