Reputation: 28586
I need to add a variable pair list in a form (Name-Value). I decided to set it in a datagridview, and use simple binging to manage it (.NET 2):
public class EventParameter
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string value;
public string Value
{
get { 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 I launch the application, I see that 2 columns, Name and Value are autogenerated, and the grid is empty.
But when I click on the Add parameter button, nothing happens... Where is the error?
Upvotes: 1
Views: 285
Reputation: 5480
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
this.dataGridView1.DataSource = bs;
}
private void button1_Click(object sender, EventArgs e)
{
//eventGridParams.Add(new EventParameter() { Name="a", Value = "a"}); //object initializer is only available for c# 3.0
EventParameter eventParam = new EventParameter();
eventParam.Name = "a";
eventParam.Value = "a";
eventGridParams.Add(eventParam);
bs.ResetBindings(false);
}
}
Upvotes: 2