日期:2014-05-18  浏览次数:20946 次

dataset添加新表、新行、新列遇到的奇怪的问题.列""不属于表dt;
C# code
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("remark ", 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='javasc