ASP.NET 查询数据处理后动态创建DataTable并添加数据,绑定Repeater

tring sqlStr2 = "select top 6 * from T_signInfo";
        DataTable dt2 = DataBase.GetTable(sqlStr2);
        //创建DataTable,形成其主框架
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn("phone", typeof(string)));
        dt.Columns.Add(new DataColumn("Brand", typeof(string)));
        dt.Columns.Add(new DataColumn("Model", typeof(string)));
        for (int i = 0; i < dt2.Rows.Count; i++)
        {
            //为创建的DataTable添加列
            DataRow dr = dt.NewRow();
            if (dt2.Rows[i]["sex"].ToString() == "女")
            {
                dr["Name"] = BaseHandle.GetContentString(dt2.Rows[i]["Name"].ToString(), 1, false) + "女士";
            }
            if (dt2.Rows[i]["sex"].ToString() == "男")
            {
                dr["Name"] = BaseHandle.GetContentString(dt2.Rows[i]["Name"].ToString(), 1, false) + "先生";
            }
            dr["phone"] = BaseHandle.GetContentString(dt2.Rows[i]["PhoneNum"].ToString(), 3, false)+"********";
            dr["Brand"] = dt2.Rows[i]["Brand"].ToString();
            dr["Model"] = dt2.Rows[i]["Model"].ToString();
            dt.Rows.Add(dr);
        }
        Repeater2.DataSource = dt;
        Repeater2.DataBind();

红色标识为处理数据方法

你可能感兴趣的:(Datatable)