Click here to Skip to main content
14,667,947 members
Rate this:
Please Sign up or sign in to vote.
A question has been bugging me for a while, and after some searching I'm just going to ask it.

I have a custom class and got it up and running in a gridView (in a Windows Form, not WPF) as follows:
//class
public class TestData
{
 public string Name { get; set; } //bad practice, I know.
 public TestData(string name)
 {
  this.Name = name;
 }
}
//in my form
List<TestData> data = new List<TestData>();
OnLoad(...)
{
 this.gridView1.DataSource = this.data;
}

This shows all the objects in the list perfectly,
but how can I allow it to add new ones?
Do I need some sort of collection/data source class or just an interface?

Thanks in advance.
Posted
Updated 22-Mar-11 23:11pm
v4

Rate this:
Please Sign up or sign in to vote.

Solution 3

Is there a reason you can't use a BindingList<TestData>? BindingList<T> implements the IBindingList interface which causes observers to be notified of list modification events (add, remove, move and update).

If you can't use or override BindingList<T> then you have to implement the interface yourself, but that's a bit of a pain and I don't see any reason why BindingList<TestData> wouldn't solve your problem.
   
v2
Comments
Albin Abel 24-Mar-11 0:57am
   
Binding list is a good option for winforms. My 5
Rate this:
Please Sign up or sign in to vote.

Solution 1

You may need to have a Static list. Here is an example.

public class TestData
{
    public string Name { get; set; } //bad practice, I know.
    public TestData(string name)
    {
        this.Name = name;
    }
}
public class TestDataList
{
   public static IDictionary<string,IList<TestData>> dataList=new Dictionary<string,IList<TestData>>();

}


Then in your form
protected void Page_Load(object sender, EventArgs e)
 {
     if (TestDataList.dataList.ContainsKey(Session.SessionID) == false)
     {
         TestDataList.dataList[Session.SessionID] = new List<TestData>();
     }

     TestData data1 = new TestData("Test1");
     TestData data2 = new TestData("Test2");
     TestDataList.dataList[Session.SessionID].Add(data1);
     TestDataList.dataList[Session.SessionID].Add(data2);

     GridView1.DataSource = TestDataList.dataList[Session.SessionID];
     GridView1.DataBind();
 }


Caution: This static list persists in the server. So you may need to have clean up thread running and remove the objects of non existing session ids. Or after use remove it explicitly
   
Comments
Groulien 23-Mar-11 5:08am
   
Can't test this at the moment, will do it asap though.

But looking at Page_Load, I think we've got a small misunderstanding (I'm to blame) because I'm using a WinForms application and not WPF/ASP.Net (Page_Load is ASP, I think).

The trick might still work though, I'll let you know.
Albin Abel 24-Mar-11 0:56am
   
I think no worry about page load. In winforms the static list available anywhere in the assembly. can use in any event. Let me know after testing. Thanks nbgangsta
Rate this:
Please Sign up or sign in to vote.

Solution 2

If you want to add new data,you should clear first data in your grid View.you try the following code sample
TestData td1 = new TestData("New Data");
            data.Add(td1);
            dataGridView1.DataSource = new List<TestData>();
            dataGridView1.DataSource = this.data;

Best Regard,
Theingi Win
   
Comments
Groulien 23-Mar-11 5:09am
   
I meant to allow the user to add a row.
Theingi Win 24-Mar-11 0:58am
   
Ok nbgangsta!
First you create column in your datagrid and then you should bind your data like this
for (int i = 0; i < data.Count; i++) {
dataGridView1.Rows.Add(+1);
dataGridView1.Rows[i].Cells[0].Value = data[i].Name.ToString();
}
will be ok.Try Again! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100