Repeater动态绑定数据

一、我的例子:(见SJTCMS/manage/report)

(1)效果图

Repeater动态绑定数据_第1张图片

(2)前台代码:


            
                
                <%#((DataRowView)Container.DataItem)["Colum"]%>
            
                

(3)后台代码:

/// 
    /// 绑定RptReport
    /// 
    /// 
    private void BindRptReport(string[] strClasses)
    {
        //如果没有选择栏目,则返回
        if (strClasses==null || string.IsNullOrEmpty(strClasses[0]))
        {
            return;
        }
        string strDepartmentId = SJTSoft.CMS.Global.Current.UserInfo.DepartmentId;
        Department dme = dmeOperate.GetDepartment(Convert.ToInt32(strDepartmentId));
        if (!string.IsNullOrEmpty(strDepartmentId))
        {
            DataTable dt = news.GetReportTotalTable(strDepartmentId, strClasses, txtStartDate.Text, txtEndDate.Text);
            if (dt != null)
            {
                dt.Columns.Add("Colum", typeof(string));

                string strchar = "";
                string strHeader = "单位";
                strchar += "";
                strchar += " " + dme.DepartmentName + "";

                for (int i = 0; i < strClasses.Length; i++)
                {
                    if (string.IsNullOrEmpty(strClasses[i]))
                    {
                        break;
                    }
                    string strClassName = news.getClassName(strClasses[i]);
                    strHeader+=""+ strClassName + "报送" +"";
                    strHeader += "" +strClassName + "通过" + "";

                    strchar += "" + dt.Rows[0][strClassName + "报送"] + "";
                    strchar += "" + dt.Rows[0][strClassName + "通过"] + "";

                }
                strchar += "" + dt.Rows[0]["报送总计"] + "";
                strchar += "" + dt.Rows[0]["通过总计"] + "";
                strchar += "";

                strchar += getchildDepartmentList(strDepartmentId, "", strClasses);

                strHeader += "报送总计";
                strHeader += "通过总计";
                strHeader += "";
                dt.Rows[0]["Colum"] = strHeader+strchar;
                
            }
            rptReport.DataSource = dt;
            rptReport.DataBind();
        }
    }

    /// 
    /// 递归
    /// 
    /// 
    /// 
    /// 
    string getchildDepartmentList(string departmentId, string sign, string[] strClasses)
    {
        #region
        string strchar = "";
        DataTable dt = dmeOperate.GetDepartMentContent(departmentId);
        sign += "    ";
        string strImg = " ";
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                dt.Columns.Add("Colum", typeof(string));
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    strchar += "";
                    strchar += "" + sign + dt.Rows[j]["departmentName"] + "";
                    string strDepartmentIdTemp = dt.Rows[j]["departmentId"].ToString();
                    DataTable dtReprot = news.GetReportTotalTable(strDepartmentIdTemp, strClasses, txtStartDate.Text, txtEndDate.Text);
                    for (int i = 0; i < strClasses.Length; i++)
                    {
                        if (string.IsNullOrEmpty(strClasses[i]))
                        {
                            break;
                        }
                        string strClassName = news.getClassName(strClasses[i]);
                        strchar += "" + dtReprot.Rows[0][strClassName + "报送"] + "";
                        strchar += "" + dtReprot.Rows[0][strClassName + "通过"] + "";
                    }
                    strchar += "" + dtReprot.Rows[0]["报送总计"] + "";
                    strchar += "" + dtReprot.Rows[0]["通过总计"] + "";
                    strchar += "";
                    strchar += getchildDepartmentList(strDepartmentIdTemp, sign, strClasses);
                    dt.Rows[j]["Colum"] = strchar;
                }
                dt.Clear();
                dt.Dispose();
            }
        }
        return strchar;
        #endregion
    }

二、网上例子

(1)效果图

Repeater动态绑定数据_第2张图片

(2)前台:

    
      
        <%#((DataRowView)Container.DataItem)["Colum"]%> 
    
        
ID 名称 排序 管理
(3)后台:

public void ListControl_Bind(int n)
{
Cy.BLL.Class bll_Class = new Cy.BLL.Class();
DataTable dt = bll_Class.GetList("followid=0").Tables[0];
dt.Columns.Add("Operate", typeof(string));//操作
dt.Columns.Add("Colum", typeof(String));//在dt中增加字段名为Colum的列
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
string strchar = "";
dr["operate"] = "添加下级分类 编辑 删除 生成";
strchar += "";
strchar += "" + dr["id"].ToString() + " ";
strchar += "" + dr["title"].ToString() + "";
strchar += "上升 下降";
strchar += "" + dr["Operate"].ToString() + "";
strchar += "";
Cy.BLL.Class bll_Classq = new Cy.BLL.Class();
strchar = InitChild(dr, strchar, bll_Classq, 2);
dr["Colum"] = strchar;
}
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
}

private string InitChild(DataRow dr, string strchar, Cy.BLL.Class bll_Classq,int n)
{
DataTable dtt = bll_Classq.GetList("followid='" + dr["id"] + "'").Tables[0];
dtt.Columns.Add("Operate", typeof(string));
dtt.Columns.Add("Colum", typeof(String));
if (dtt.Rows.Count > 0)
{
string s = " ";
for (int j = 1; j <= n; j++)
{
s += " ";
}
for (int k = 0; k < dtt.Rows.Count; k++)
{
DataRow dro = dtt.Rows[k];
string flag = "├";
if (dtt.Rows.Count == 1)
{
flag = "├";
}
else
{
if (k == 0)
{
flag = "┌";
}
if (k == dtt.Rows.Count - 1)
{
flag = "└";
}
}
dro["operate"] = "添加下级分类 编辑 删除 生成";
strchar += "";
strchar += "" + dro["id"].ToString() + " ";
strchar += "" + s + flag + dro["title"].ToString() + "";
strchar += "上升 下降";
strchar += "" + dro["Operate"].ToString() + "";
strchar += "";
strchar = InitChild(dro, strchar, bll_Classq, n + 8);
}

}
return strchar;
}


参考:http://www.cnblogs.com/qiancheng509/articles/1775600.html




你可能感兴趣的:(ASP.NET控件)