.net - DataGridView sample binding issue -
i need add variable pair list in form (name-value). decided set in datagridview, , use simple binging manage (.net 2):
public class eventparameter { private string name; public string name { { return name; } set { name = value; } } private string value; public string value { { return this.value; } set { this.value = value; } } } ///////////////////// user control initialization private list<eventparameter> eventgridparams; public grideventsender() { initializecomponent(); eventgridparams = new list<eventparameter>(); this.datagridview1.autogeneratecolumns = true; this.datagridview1.datasource = eventgridparams; } ///////////////////// add parameter button private void btnaddparam_click(object sender, eventargs e) { eventgridparams.add(new eventparameter()); }
when launch application, see 2 columns, name , value autogenerated, , grid empty.
but when click on add parameter button, nothing happens... error?
public partial class frmgridview : form { private list<eventparameter> eventgridparams; private bindingsource bs; public frmgridview() { initializecomponent(); eventgridparams = new list<eventparameter>(); bs = new bindingsource(); bs.datasource = eventgridparams; //this.datagridview1.autogeneratecolumns = true; //you don't need this.datagridview1.datasource = bs; } private void button1_click(object sender, eventargs e) { //eventgridparams.add(new eventparameter() { name="a", value = "a"}); //object initializer available c# 3.0 eventparameter eventparam = new eventparameter(); eventparam.name = "a"; eventparam.value = "a"; eventgridparams.add(eventparam); bs.resetbindings(false); } }
Comments
Post a Comment