winforms - C#, add row to DataGridView -
i have form datagridview , button. datagridview shows data arraylist, , want add elements arraylist 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 doesn’t work… how can add new row datagridview? thanks.
list not implement ibindinglist grid not know new items.
bind datagridview bindinglist instead.
here 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; }
Comments
Post a Comment