我有一个dataGridView,它应该使用一个列表作为dataSource。运行应用程序时,它会显示带有列的网格(自动生成)。 当创建一个新的Person-object并将其添加到名为Persons的列表中时,它只会添加一个空行。在此之后添加新人时,不会进行任何更改。
系统在MainForm中使用一个名为Project的属性,它存储用于网格的列表。
public partial class MainForm : Form
{
public static DataProject Project { get; set; }
public MainForm()
{
InitializeComponent();
//show a dialog, creating the Project-object
BindingSource bs = new BindingSource();
bs.DataSource = Project.Persons;
dataGridView1.DataSource = bs;
}
private void buttonAddPerson_Click(object sender, EventArgs e)
{
//show a dialog, creating a person and adding him to Project.Person (list).
dataGridView1.Refresh();
dataGridView1.Update();
}
}
这是DataProject对象:
public class DataProject
{
public string Name { get; set; }
public List<Person> Persons { get; set; }
public DataProject()
{
Persons = new List<Person>();
}
}
答案 0 :(得分:1)
为BindingSource创建一个全局变量
def numbergenerator(level):
return random.randint(1, level)
并点击按钮,只需重置此绑定源
BindingSource bs = new BindingSource();
答案 1 :(得分:0)
dataGridView1.DataBind();最后添加这个。我曾经遇到过同样的问题,它对我有用!
public MainForm()
{
InitializeComponent();
//show a dialog, creating the Project-object
BindingSource bs = new BindingSource();
bs.DataSource = Project.Persons;
dataGridView1.DataSource = bs;
dataGridView1.DataBind();
}