DataTable 改变列的值类型 和列的内容

Ds = famous.shop_select_bychecked(0);
        Dt = Ds.Tables[0].Clone();

        foreach (DataColumn Dc in Dt.Columns)
        {
            if (Dc.ColumnName == "BID" || Dc.ColumnName == "SID")
            {
                Dc.DataType=typeof(string);
            }
        }

        foreach (DataRow Dr in Ds.Tables[0].Rows)
        {
            DataRow dr =Dt.NewRow();
            dr["SHOPID"] = Dr["SHOPID"];
            dr["SNAME"] = Dr["SNAME"];
            dr["TEL"] = Dr["TEL"];
            dr["BID"] = Dr["BID"];
            dr["SID"] = Dr["SID"];
            dr["STARTTIME"] = Dr["STARTTIME"];
            Dt.Rows.Add(dr);
        }

       
        for (int i = 0; i < Dt.Rows.Count; i++)
        {
            for (int j = 0; j < BtypeDs.Tables[0].Rows.Count; j++)
            {
                if (Dt.Rows[i][3].ToString() == BtypeDs.Tables[0].Rows[j]["BID"].ToString())
                {
                    Dt.Rows[i][3] = BtypeDs.Tables[0].Rows[j]["BNAME"].ToString();
                }
            }

            for (int j = 0; j < StypeDs.Tables[0].Rows.Count; j++)
            {
                if (Dt.Rows[i][4].ToString() == StypeDs.Tables[0].Rows[j]["SID"].ToString())
                {
                    Dt.Rows[i][4] = StypeDs.Tables[0].Rows[j]["SNAME"].ToString();
                }
            }
        }

        GVinfo.DataSource = Dt;
        GVinfo.AllowPaging = true;
        GVinfo.PageSize = 20;
        GVinfo.DataBind();

--------------------------------------------------------------------------------------------------

注意:已经填充了数据的DataTable无法进行列类型的转变 可以先通过t = Ds.Tables[0].Clone();克隆原来数据表的结构 正确步骤:1.克隆表结构,2.修改列类型,3.修改记录值,4.返回希望的结果

你可能感兴趣的:(Datatable)