添加行自定义类和gridview [英] Add row custom class and gridview

查看:100
本文介绍了添加行自定义类和gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题一直困扰着我一段时间,经过一番搜索之后,我只是要问这个问题.

我有一个自定义类,并按如下所示在gridView中启动并运行它(在Windows窗体中,而不是WPF中):

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.

推荐答案

是否有您不能使用BindingList<TestData> ?绑定列表< T>实现IBindingList接口,该接口使观察者收到列表修改事件(添加,删除,移动和更新)的通知.

如果您无法使用或覆盖BindingList< T> ;,那么您必须自己实现该接口,但这有点痛苦,我看不出为什么BindingList< TestData>不会解决您的问题.
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.


您可能需要一个 Static列表.这是一个例子.

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();
 }



警告:此静态列表将保留在服务器中.因此,您可能需要运行清理线程并删除不存在的会话ID的对象.或使用后将其显式删除



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


如果要添加新数据,应清除网格视图中的第一个数据.您可以尝试以下代码示例
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;


最好的问候,
Theingi Win


Best Regard,
Theingi Win


这篇关于添加行自定义类和gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆