记录下---datatable添加列 绑定数据---新手勿喷--笔记

string sql1 = "select distinct RightAns from procrtest";
        if(!IsPostBack)
        {
            //datatable
            DataTable dt = DbManager.ExecuteQuery(sql1);
            dt.Columns.Add("ksrs", Type.GetType("System.String"));
            dt.Columns.Add("dcrs", Type.GetType("System.String"));
            dt.Columns.Add("cwv", Type.GetType("System.String"));
            string s="";
            string s1="";
            string s2="";
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                string s10 = dt.Rows[j][0].ToString();
               
                s=DbManager.ExecuteScalar("select count(*) from usertest where RightAns='" + dt.Rows[j][0].ToString() + "'").ToString();
                dt.Rows[j][1]=s;
               
                s1 = DbManager.ExecuteScalar("select count(*) from usertest where RightAns='" + dt.Rows[j][0].ToString() + "' and RightAns<>UserAns").ToString();
                dt.Rows[j][2] = s1;
                if (Convert.ToDouble(dt.Rows[j][1]) == 0)
                {
                    s2 = "0%";
                }
                else
                {
                    s2 = Math.Round((Convert.ToDouble(dt.Rows[j][2]) / Convert.ToDouble(dt.Rows[j][1])) * 100, 2).ToString() + "%";
                }
                dt.Rows[j][3] = s2;
            }
            dt.DefaultView.Sort = "cwv DESC";
            datalist1.DataSource = dt.DefaultView;
            datalist1.DataBind();
         }

你可能感兴趣的:(记录下---datatable添加列 绑定数据---新手勿喷--笔记)