日期:2014-05-17  浏览次数:20530 次

DataTable拆分成2个DataTable分别绑定到GridView的问题,求进。。。
页面上有两个GridView:gv1,gv2
现在有一个DataTable:dt

需求是dt里每隔4条数据分别绑定到gv1和gv2

比如:dt现在有13条数据,那gv1绑定其中的1,2,3,4,9,10,11,12 gv2绑定5,6,7,8,13 

  不知道我表达清楚没。。。

------解决方案--------------------
我的想法和1楼差不多 方法大概这样子
C# code

public DataTable tableda1()
        {
            DataTable da = new DataTable();
            da.Columns.Add("ID");
            return da;
        }
public DataTable tableda2()
        {
            DataTable da = new DataTable();
            da.Columns.Add("ID");
            return da;
        }

 public vid ID()
        {
            string strSql = "";
            DataSet ds =数据库操作; 
            DataTable dt1 = tableda1();
            DataTable dt2 = tableda2();

            for(int i=0;i<ds.Tables[0].Rows.Count;i++)
            {
                DataRow row1 = dt1.NewRow();
                DataRow row2 = dt2.NewRow();

                if (判断dt1,dt2里面行数)
                {
                    row["ID"] = ds.Tables[0].Rows[i]["ID"] ;   
                }
                else 
                {
                    row["ID"] =;     
                }
                dt1.Rows.Add(row1);
                dt2.Rows.Add(row2);

            }
        }

------解决方案--------------------
若id连续,这样是可以的,
C# code
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    System.Data.DataTable dt = new System.Data.DataTable();
    if (!Page.IsPostBack)
    {
      dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
      dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
      System.Random rd = new System.Random();
      for (int i = 1; i < 22; i++)
      {
        dt.Rows.Add(new Object[] { i, "孟" + i.ToString() });
      }
      System.Data.DataView dv = dt.DefaultView;
      dv.RowFilter = "(id-1)%8 < 4 ";
      GridView1.DataSource = dt;
      GridView1.DataBind();

      dv.RowFilter = "(id-1)%8 >= 4 ";
      GridView2.DataSource = dt;
      GridView2.DataBind();
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <table cellspacing="10">
    <tr valign="top"><td>
      <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>
    </td><td>
      <asp:GridView ID="GridView2" runat="server">
      </asp:GridView>
    </td></tr>
  </table>
  </form>
</body>
</html>

------解决方案--------------------
加一个自动编号id(从零开始)就很好办了
 private void bind()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("txt", typeof(string));
for (int i = 0; i < 100; i++)
{
dt.Rows.Add();
dt.Rows[i]["id"] = i;
dt.Rows[i]["txt"] = i.ToString();
}
DataView myDataView = dt.DefaultView;
myDataView.RowFilter = "id%8<4"; //过滤条件
GridView1.DataSource = myDataView;
GridView1.DataBind();
myDataView.RowFilter = "id%8>=4"; //过滤条件
GridView2.DataSource = myDataView;
G