Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
4.00/5 (1 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:
C#
//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

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.
 
Share this answer
 
v2
Comments
Albin Abel 24-Mar-11 0:57am    
Binding list is a good option for winforms. My 5
You may need to have a Static list. Here is an example.

C#
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
C#
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
 
Share this answer
 
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
If you want to add new data,you should clear first data in your grid View.you try the following code sample
XML
TestData td1 = new TestData("New Data");
            data.Add(td1);
            dataGridView1.DataSource = new List<TestData>();
            dataGridView1.DataSource = this.data;

Best Regard,
Theingi Win
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900