做最好的自己~~

Be My Personal Best!

导航

给表格控件绑定数据库内容的封装

   
  

  #region 给表格控件绑定数据库内容
    /// <summary>
    /// 水平显示每条记录
    /// </summary>
    /// <param name="dst">需要的数据集</param>
    /// <param name="tab">需要的table控件</param>
    /// <param name="SubHead">标头,依次从左向右</param>
    /// <returns></returns>
    public Table TableBd_Horizontal(DataSet dst, Table tab, string[] SubHead)
    {

        int rows = dst.Tables[0].Rows.Count;
        int cell = dst.Tables[0].Columns.Count; ;//读取要设置表格的行和列...
        //显示表格标头
        TableRow head_tr = new TableRow();//新建第一个行;用来显示表格标题
        for (int head_ce = 0; head_ce < cell; head_ce++)
        {
            TableCell head_tc = new TableCell();
            head_tc.Controls.Add(new LiteralControl(SubHead[head_ce].ToString()));//奖标题加入新列
            head_tr.Cells.Add(head_tc);//将新列加入标题行
        }

        //将标题行加入表格
        tab.Rows.Add(head_tr);

        //显示表格内容
        for (int ro = 0; ro < rows; ro++)
        {
            TableRow tr = new TableRow();//新建行...

            for (int ce = 0; ce < cell; ce++)
            {
                TableCell tc = new TableCell();//新建列....

                tc.Controls.Add(new LiteralControl(dst.Tables[0].Rows[ro][ce].ToString()));//给每个单元格填加文字.....

                tr.Cells.Add(tc);//将建的列加入到行中去..
            }
            tab.Rows.Add(tr);//将其它行加到表格中去..
        }
        return tab;
    }

    /// <summary>
    /// 垂直显示每条记录
    /// </summary>
    /// <param name="dst"></param>
    /// <param name="tab"></param>
    /// <param name="SubHead"></param>
    /// <returns></returns>
    public Table TableBd_Vertical(DataSet dst, Table tab, string[] SubHead)
    {
        int rows = dst.Tables[0].Rows.Count;
        int cell = dst.Tables[0].Columns.Count; ;//读取要设置表格的行和列...
        for (int ce = 0; ce < cell; ce++)
        {
            TableRow tr = new TableRow();//新建行...

            TableCell head_tc = new TableCell();//新建一列...
            head_tc.Controls.Add(new LiteralControl(SubHead[ce].ToString()));//将标题加入新列
            tr.Cells.Add(head_tc);//将新列加入此行
            for (int ro = 0; ro < rows; ro++)
            {
                TableCell tc = new TableCell();//新建列....

                tc.Controls.Add(new LiteralControl(dst.Tables[0].Rows[ro][ce].ToString()));//给每个单元格填加文字.....

                tr.Cells.Add(tc);//将建的列加入到行中去..
            }
            tab.Rows.Add(tr);//将建的行加到表中去..
        }
        return tab;
    }
    #endregion

posted on 2008-05-08 21:03  阿万  阅读(616)  评论(0编辑  收藏  举报