private void BindData()
    {
        //利用存储过程得到该表
        DataSet myds = PSBB.GetListAspNetPager(this.AspNetPager.PageSize, this.AspNetPager.CurrentPageIndex, 0, "mangeReview=0 or riskRiview=0", "1");
        //声明一个新的dataset
        DataSet GridViewDS = new DataSet();
        //声明新的datatable
        DataTable dt = new DataTable("dt");
       
        dt.Columns.Add("proSaleBillID ", typeof(string));
        dt.Columns.Add("proSaleBillCode ", typeof(string));
        dt.Columns.Add("productName ", typeof(string));
        dt.Columns.Add("productStorName ", typeof(string));
        dt.Columns.Add("quantity ", typeof(decimal));
        dt.Columns.Add("perPrice ", typeof(decimal));
        dt.Columns.Add("unitName ", typeof(string));
        dt.Columns.Add("BusiName ", typeof(string));
        dt.Columns.Add("departmentName ", typeof(string));
        dt.Columns.Add("madeBillDate ", typeof(DateTime));
        dt.Columns.Add("madeBillUser ", typeof(string));
        dt.Columns.Add("whoNotAgree ", typeof(string));
        dt.Columns.Add("re ", typeof(string));        GridViewDS.Tables.Add(dt);        foreach(DataRow row in myds.Tables[0].Rows)
        {
            int i = 0;            DataRow newRow = dt.NewRow();
            //直接这么用的话会提示:列"proSaleBillID"不在表dt中.用索引的话则没有这样的问题
            //newRow["proSaleBillID"] = row["proSaleBillID"];
            newRow[0] = row["proSaleBillID"];
            newRow[1] = row["proSaleBillCode"];
            newRow[2] = row["productName"];
            newRow[3] = row["productStorName"];
            newRow[4] = row["quantity"];
            newRow[5] = row["perPrice"];
            newRow[6] = row["unitName"];
            newRow[7] = row["BusiName"];
            newRow[8] = row["departmentName"];
            newRow[9] = row["madeBillDate"];
            newRow[10] = row["madeBillUser"];
            
            //经理未同意
            if (myds.Tables[0].Rows[i][17].ToString() == "false")
            {
                newRow[11] = "经理不同意";
                newRow[12] = myds.Tables[0].Rows[i][24];//设置不通过的原因为经理不通过的原因            }
            //风险办不同意
            else
            {
                newRow[11] = "风险办不同意";
                newRow[12] = myds.Tables[0].Rows[i][25];//设置不通过的原因为风险办不通过的原因
            }            dt.Rows.Add(newRow);   
            //GridViewDS.Tables[0].ImportRow(newRow);
        }        string xml = GridViewDS.GetXml();
        //DataSet dsTEM = PSBB.proSaleBill_Query_byCode(PSBM);
        string aa = myds.Tables[0].Rows[0][17].ToString();        //绑定到gridview
        if (myds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = GridViewDS.Tables[0];
            GridView1.DataKeyNames = new string[] { "proSaleBillID" };//主键
            GridView1.DataBind();
        }
        else
        {
            myds.Tables[0].Rows.Add(myds.Tables[0].NewRow());
            GridView1.DataSource = myds;
            GridView1.DataBind();
            int columnCount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "没有数据";
            GridView1.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('无数据!');</script>");
        }
    }
这里有个很奇怪的问题,我后来创建的列是一个个加上去的。但是在后来调试的时候,利用string xml = GridViewDS.GetXml();得到的xml是这样的:
可以看到每一个列名后面都加了后缀:“_x0020_”.
小弟实在是不知道哪里除了问题,为什么会出现这样的情况呢?
另外另一个问题,前面的代码中在foreach语句块中把myds.Tables[0]的数据赋值给新建的表中的数据的时候,每一列我用的都是索引,这里如果我foreach语句块里面这么写:
foreach(DataRow row in myds.Tables[0].Rows)
        {
            int i = 0;            DataRow newRow = dt.NewRow();
            //直接这么用的话会提示:列"proSaleBillID"不在表dt中.用索引的话则没有这样的问题
            newRow["proSaleBillID"] = row["proSaleBillID"];
便会报错:
这个也不应该报这个错误啊,我前面已经给table添加了这个列了。这两个问题都是很奇怪的问题,小弟百思不得其解。恳请各位大侠解答

解决方案 »

  1.   

    添加列修改如下即可
     //声明新的datatable
                DataTable dt = new DataTable("dt");
                dt.Columns.Add(new DataColumn("proSaleBillID", typeof(string)));
                dt.Columns.Add(new DataColumn("productName ", typeof(string)));
                dt.Columns.Add(new DataColumn("productStorName ", typeof(string)));
                dt.Columns.Add(new DataColumn("quantity ", typeof(decimal)));
                dt.Columns.Add(new DataColumn("perPrice ", typeof(decimal)));
                dt.Columns.Add(new DataColumn("unitName ", typeof(string)));
                dt.Columns.Add(new DataColumn("BusiName ", typeof(string)));
                dt.Columns.Add(new DataColumn("departmentName ", typeof(string)));
                dt.Columns.Add(new DataColumn("madeBillDate ", typeof(DateTime)));
                dt.Columns.Add(new DataColumn("madeBillUser ", typeof(string)));
                dt.Columns.Add(new DataColumn("whoNotAgree ", typeof(string)));
                dt.Columns.Add(new DataColumn("re ", typeof(string)));
      

  2.   

    Try like this
    DataColumn dc = new DataColumn("proSaleBillID", typeof(string));
                dt.Columns.Add(dc);
                 dc = new DataColumn("productName", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("productStorName", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("quantity", typeof(decimal));
                dt.Columns.Add(dc);
                dc = new DataColumn("perPrice", typeof(decimal));
                dt.Columns.Add(dc);
                dc = new DataColumn("unitName", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("BusiName", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("departmentName", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("madeBillDate", typeof(DateTime));
                dt.Columns.Add(dc);
                dc = new DataColumn("madeBillUser", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("whoNotAgree", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("re", typeof(string));
                dt.Columns.Add(dc);