I have Form with DataGridView and Button. DataGridView shows data from ArrayList, and I want to add elements to ArrayList by DataGridView. So, I'm trying this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = Student.students;
}
private void button1_Click(object sender, EventArgs e)
{
Student st = new Student();
Student.students.Add(st);
}
}
But this doesn't work… How can I add new row to this DataGridView? Thanks.
List does not implement IBindingList so the grid does not know about your new items.
Bind your DataGridView to a BindingList instead.
here is the example
Your class ,
class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
}
In code behind,
BindingList<Student> STUDENTS;
public Form1()
{
InitializeComponent();
STUDENTS = new BindingList<Student>();
dataGridView1.DataSource = STUDENTS;
}
private void button1_Click(object sender, EventArgs e)
{
STUDENTS.Add(new Student { ID =1 , Name ="test" });
dataGridView1.DataSource = STUDENTS;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.