views:

25

answers:

1

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?

+2  A: 
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);
        }
    }
Lee Sy En
ahh.. My bad. I get to used to WebForm's DataBind(). You need BindingSource to get it work. I have edited my post. My apologize.
Lee Sy En
cool. I even don't need button add anymore...
serhio