当前位置: 代码迷 >> ASP.NET >> datagrid的有关问题
  详细解决方案

datagrid的有关问题

热度:7191   发布时间:2013-02-26 00:00:00.0
datagrid的问题
一个页面当中有多个datagrid要绑定,绑定的方法是差不多的,不同的只有每一个DATAGRID绑定的SELECT语句中的ID值不一样。这样要怎么解决?有没有一种简便的方法,不用每一个都要写?

------解决方案--------------------------------------------------------
DataGrid[] grids = {grid1,grid2,grid3};
int[] ids = {2,4,6}
for(int i=0; i <grids.Length; i++)
{
DataGrid grid = grids[i];
grid.DataSource = getDataSource(ids[i]);
grid.DataBind();
}
------解决方案--------------------------------------------------------
先将整个表的数据读出来,然后分类筛选绑定不同的数据源
DataTable mytable=new DataTable();
mytable=数据源;
DataRow[] ds1=mytable.Select( "ID=1 ");
this.DataGrid1.DataSource=ds1;
this.DataGrid1.DataBind();
DataRow[] ds2=mytable.Select( "ID=2 ");
this.DataGrid2.DataSource=ds1;
this.DataGrid2.DataBind();
....