DataGrid能否直接绑定IList[]?

lyb_abiandbel 2006-02-20 12:44:33
我想用datagrid绑定得到的IList[],但是始终提醒我“选定数据源上未能找到名称为“Product_id”的字段或属性。”

class RowData
{
public string Product_id;
public string Product_nm;

public RowData(string id,string des)
{
this.Product_id=id;
this.Product_nm=des;
}
}

class RowDataCollection:CollectionBase
{
public int Add(RowData rd)
{
return List.Add(rd);
}
}

class Test
{
private RowDataCollection m_RowDataCollection;
public RowDataCollection RowCollection
{
set{m_RowDataCollection=value;}
get{return m_RowDataCollection;}
}

public void LoadData()
{
this.RowCollection=new RowDataCollection();
this.RowCollection.Add(new RowData("7200","aaaaaa",1111,2222,33,4444,5555,66,7777,88));
this.RowCollection.Add(new RowData("7200","aaaaaa",1111,2222,33,4444,5555,66,7777,88));
this.RowCollection.Add(new RowData("7200","aaaaaa",1111,2222,33,4444,5555,66,7777,88));
}
}


private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Test t=new Test();
t.LoadData();
IList[] l=new IList[] {t.RowCollection};
this.DataGrid1.DataSource=l;
this.DataGrid1.DataBind();
}

<asp:DataGrid id="DataGrid1" runat="server" Width="529px" Height="208px" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn HeaderText="a1" DataField="Product_id"></asp:BoundColumn>
<asp:BoundColumn HeaderText="a2" DataField="Product_nm"></asp:BoundColumn>

</Columns>
</asp:DataGrid>

难道我一定要把IList[]转换成dataset中的表,然后把这个dataset作为数据源吗?
...全文
701 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
lengfeng8866 2006-03-04
  • 打赏
  • 举报
回复
看完通帖,对CollectionBase的了解又深入了一步。谢谢楼主及各位的分析和讨论,我要先收藏了。
zeusvenus 2006-02-21
  • 打赏
  • 举报
回复
除了上面提到的之外,还应注意Ilist中如果有一行数据未赋值可能会出现错误,一般要在绑定之前提取出IList中有数据的部分再绑定到DataGrid中。
lyb_abiandbel 2006-02-21
  • 打赏
  • 举报
回复
实现IEnumerable :公开枚举数,该枚举数支持在集合上进行简单迭代。

实现IList 接口:表示可按照索引单独访问的一组对象。

IEnumerable -->ICollection-->IList -->CollectionBase,

所以:CollectionBase 是为强类型集合提供抽象基类。
woxihuanbohe 2006-02-21
  • 打赏
  • 举报
回复
关键还是这段代码 对field需要实现get 访问器
class RowData
{
public string Product_id;//只是公开public并不能用这个变量名字来做邦定的字段
public string Product_nm;

public RowData(string id,string des)
{
this.Product_id=id;
this.Product_nm=des;
}
}
lyb_abiandbel 2006-02-20
  • 打赏
  • 举报
回复
CollectionBase 是为强类型集合提供抽象基类, 看来没有理解CollectionBase啊!

比如RowData rd=t.RowCollection[1];可以由索引取得对象的值,其他的操作也可以啊!
明白了,多谢各位!
lyb_abiandbel 2006-02-20
  • 打赏
  • 举报
回复
我把代码整理一下:

public class RowData
{
public string product_id;
public string product_nm;
public string Product_id
{
get{return product_id;}
set{product_id = value;}
}
public string Product_nm
{
get{return product_nm;}
set{product_nm = value;}
}

public RowData(string id,string des)
{
this.Product_id=id;
this.Product_nm=des;
}
}

class RowDataCollection:CollectionBase
{
public RowData this[ int index ]
{
get
{
return( (RowData) List[index] );
}
set
{
List[index] = value;
}
}

public int Add( RowData value )
{
return( List.Add( value ) );
}

public int IndexOf( RowData value )
{
return( List.IndexOf( value ) );
}

public void Insert( int index, RowData value )
{
List.Insert( index, value );
}

public void Remove( RowData value )
{
List.Remove( value );
}

public bool Contains( RowData value )
{
// If value is not of type RowData, this will return false.
return( List.Contains( value ) );
}

protected override void OnInsert( int index, Object value )
{
if ( value.GetType() != Type.GetType("WebApplication1.RowData") )
throw new ArgumentException( "value must be of type RowData.", "value" );
}

protected override void OnRemove( int index, Object value )
{
if ( value.GetType() != Type.GetType("WebApplication1.RowData") )
throw new ArgumentException( "value must be of type RowData.", "value" );
}

protected override void OnSet( int index, Object oldValue, Object newValue )
{
if ( newValue.GetType() != Type.GetType("WebApplication1.RowData") )
throw new ArgumentException( "newValue must be of type RowData.", "newValue" );
}

protected override void OnValidate( Object value )
{
if ( value.GetType() != Type.GetType("WebApplication1.RowData") )
throw new ArgumentException( "value must be of type RowData." );
}

}

class Test
{
private RowDataCollection m_RowDataCollection;
public RowDataCollection RowCollection
{
set{m_RowDataCollection=value;}
get{return m_RowDataCollection;}
}

public void LoadData()
{
this.RowCollection=new RowDataCollection();
this.RowCollection.Add(new RowData("7201","aaaaaa"));
this.RowCollection.Add(new RowData("7202","aaaaaa"));
this.RowCollection.Add(new RowData("7203","aaaaaa"));
this.RowCollection.Add(new RowData("7204","aaaaaa"));
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Test t=new Test();
t.LoadData();
RowData rd=t.RowCollection[1];
this.DataGrid1.DataSource=t.RowCollection;
this.DataGrid1.DataBind();
}



<asp:DataGrid id="DataGrid1" runat="server" Width="529px" Height="208px" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn HeaderText="a1" DataField="product_id"></asp:BoundColumn>
<asp:BoundColumn HeaderText="a2" DataField="product_nm"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
lyb_abiandbel 2006-02-20
  • 打赏
  • 举报
回复
to:chnking(kent)

你说的非常正确.我把RowData的字段注释掉,就会报同样的错.
hdt(倦怠) 说的也很对,就是因为RowData中没有该属性,反射不到该字段的值.




chnking 2006-02-20
  • 打赏
  • 举报
回复
RowDataCollection本身是从CollectionBase继承,已经实现了IEnumerable接口,就可以被列表控件进行绑定,再用IList[]是多此一举了
chnking 2006-02-20
  • 打赏
  • 举报
回复
下面代码经测试通过(cs部分的代码,aspx页面没有改动):
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
RowDataCollection m_RowDataCollection;

m_RowDataCollection=new RowDataCollection();
m_RowDataCollection.Add(new RowData("7201","aaaaaa"));
m_RowDataCollection.Add(new RowData("7202","bbbbbb"));
m_RowDataCollection.Add(new RowData("7203","cccccc"));
this.DataGrid1.DataSource=m_RowDataCollection;
this.DataGrid1.DataBind();
}
public class RowData
{
public string product_id;
public string product_nm;
public string Product_id
{
get{return product_id;}
set{product_id = value;}
}
public string Product_nm
{
get{return product_nm;}
set{product_nm = value;}
}

public RowData(string id,string des)
{
this.Product_id=id;
this.Product_nm=des;
}
}

class RowDataCollection:CollectionBase
{
public int Add( RowData value )
{
return( List.Add( value ) );
}
}
liwei820825 2006-02-20
  • 打赏
  • 举报
回复
想办法弄成 ArrayList或者其他的基于IList的对象都可以的
解决方法 类似于楼上,弄一个datarow之后循环遍历将数据读到ArrayList中,呵呵
之后就直接邦定就可以了
真相重于对错 2006-02-20
  • 打赏
  • 举报
回复
class RowDataCollection:CollectionBase
{
public int Add(RowData rd)
{
return List.Add(rd);
}
public object Item( int nIndex)
{
get{
//这里可以做个手脚
比如可以返回一个datarow , 或自定义类
}
}
}
lyb_abiandbel 2006-02-20
  • 打赏
  • 举报
回复
to:hdt(倦怠)

我想我明白你的意思了,那我有没有方法实现

Test t=new Test();
t.LoadData(); //填入RowCollection数据
this.DataGrid1.DataSource=t.RowCollection;
this.DataGrid1.DataBind();

这样做呢?
lyb_abiandbel 2006-02-20
  • 打赏
  • 举报
回复
我们得到的数据都是RowCollection这种方式,所以我想直接绑定它,不想转换(转换成dataset当然知道怎么做了)。
真相重于对错 2006-02-20
  • 打赏
  • 举报
回复
不要用绑定列,使用模板列试一下看
===================================
一样的 绑定列只不过通过反射自动来发现public 字段来绑定
模版列示人为的指定绑定字段,最后还是要同过反射来获取数据



DataGrid 的数据源可以使用除了数据集合外的其他,如果数祖 List 等
但是你还是得将 数租等转为 table 吧,要不前台页面 显示什么字段名呢?
===============================================
看了上边的解释,应该明白了
mostice 2006-02-20
  • 打赏
  • 举报
回复
DataGrid 的数据源可以使用除了数据集合外的其他,如果数祖 List 等
但是你还是得将 数租等转为 table 吧,要不前台页面 显示什么字段名呢?
chnking 2006-02-20
  • 打赏
  • 举报
回复
不要用绑定列,使用模板列试一下看
真相重于对错 2006-02-20
  • 打赏
  • 举报
回复
我说的有点乱,不知你听明白没有?

DataGrid的datasoure要求实现ilist,但不要求每一条数据必须实现ilist,但每条数据必须能够通过DataBinder.Eval( object , string )(也就是反射来找到数据)
真相重于对错 2006-02-20
  • 打赏
  • 举报
回复
IList[]其实是Array
DataGrid要帮定的话
必须要让DataBinder.Eval 能够通过反射发现你的对象
《Eval 已重载。在运行时使用反射来分析和计算对象的数据绑定表达式。此方法允许 RAD 设计器(如 Visual Studio .NET)轻松地生成和分析数据绑定语法。该方法也可通过声明方式在 Web 窗体页上使用,以简化类型之间的转换。 》以上摘自msdn
所以直接用IList作为一条数据,似乎有些重复,只要这条数据能够通过DataBinder.Eval来获得数据即可

lulei 2006-02-20
  • 打赏
  • 举报
回复
“选定数据源上未能找到名称为“Product_id”

是说你的数据集合中没有Product_id列
lyb_abiandbel 2006-02-20
  • 打赏
  • 举报
回复
class RowDataCollection:CollectionBase
{
public RowData this[ int index ]
{
get
{
return( (RowData) List[index] );
}
set
{
List[index] = value;
}
}

public int Add( RowData value )
{
return( List.Add( value ) );
}

public int IndexOf( RowData value )
{
return( List.IndexOf( value ) );
}

public void Insert( int index, RowData value )
{
List.Insert( index, value );
}

public void Remove( RowData value )
{
List.Remove( value );
}

public bool Contains( RowData value )
{
// If value is not of type RowData, this will return false.
return( List.Contains( value ) );
}

protected override void OnInsert( int index, Object value )
{
if ( value.GetType() != Type.GetType("IListData.RowData") )
throw new ArgumentException( "value must be of type RowData.", "value" );
}

protected override void OnRemove( int index, Object value )
{
if ( value.GetType() != Type.GetType("IListData.RowData") )
throw new ArgumentException( "value must be of type RowData.", "value" );
}

protected override void OnSet( int index, Object oldValue, Object newValue )
{
if ( newValue.GetType() != Type.GetType("IListData.RowData") )
throw new ArgumentException( "newValue must be of type RowData.", "newValue" );
}

protected override void OnValidate( Object value )
{
if ( value.GetType() != Type.GetType("IListData.RowData") )
throw new ArgumentException( "value must be of type RowData." );
}
}

我把RowDataCollection改写成上面的,但是还是出现一样的错误。

但是我查了msdn,CollectionBase类实现了IList接口啊!
请各位帮忙啊!
加载更多回复(9)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧