苯苯的博客

新随笔 联系 管理
  21 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

将List<T>转换为BindingList<T>,然后设置DataGridView的DataSource为BindingList<T>!!
代码:

DataGridView.DataSource = new BindingList<T>(List<T>);

将绑定BindingList<T>的DataSource转化为List<T>,同理
代码:
List<T> modelList=new List<T>((BindingList<T>)this.DataGridView.DataSource);


说明:BindingList<T>和List<T>都有个构造函数,参数是IEnumerable<T>,既然他们俩个都是继承IEnumerable,当然能相互转换。

下面是这个构造函数的执行过程:

public List(IEnumerable<T> collection)
{
    
if (collection == null)
    
{
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
    }

    ICollection
<T> is2 = collection as ICollection<T>;
    
if (is2 != null)
    
{
        
int count = is2.Count;
        
this._items = new T[count];
        is2.CopyTo(
this._items, 0);
        
this._size = count;
    }

    
else
    
{
        
this._size = 0;
        
this._items = new T[4];
        
using (IEnumerator<T> enumerator = collection.GetEnumerator())
        
{
            
while (enumerator.MoveNext())
            
{
                
this.Add(enumerator.Current);
            }

        }

    }

}


posted on 2008-03-26 06:50 one 阅读(441) 评论(3)  编辑 收藏 网摘 所属分类: 工作

Feedback

datakey?
  回复  引用    

#2楼  2008-06-12 15:36 ylhyh      
不错,刚好遇到了此问题
  回复  引用  查看    

#3楼  2008-07-03 07:11 TKer [未注册用户]
有是有了.不过一点的话报错“集合是只读的”.......
  回复  引用    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-11-06 11:03 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接: