动态生成DataTable

 DataSet ds;//声明DataSet
            DataTable dt;//声明Datatable
            DataColumn dc;//声明DataColumn
            DataRow dr;//声明DataRow
            //1得到所有吨位的信息
            SqlParameter[] para_tong= new SqlParameter[]
            {
                new SqlParameter("@TypeId","41")
            };
            ds = SQLHelper.GetDataSet("P_PublicDataBase_SelectByType", para_tong);
            //如果吨位表中没有信心就不用构建临时表啦
            if (ds.Tables[0].Rows.Count == 0) return null;
            //2查数据
            SqlParameter[] para_Detail = new SqlParameter[]
            {
            new SqlParameter("@TranQuoteMainKeyId",accountTranQuoteMain_In.KeyId)
            };
            ds = SQLHelper.GetDataSet("P_AccountTranQuoteDetail_In_selectByMainKeyId", para_Detail);
            //如果填充的数据没有,同样不用构建临时表了
            if (ds.Tables[0].Rows.Count == 0) return null;
            //建临时表
            dt = new DataTable();
            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String ");
            dc.ColumnName = "BeginAddress ";
            dt.Columns.Add(dc);

            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String ");
            dc.ColumnName = "EndAddress";
            dt.Columns.Add(dc);
            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dc = new DataColumn();
                    dc.DataType = System.Type.GetType("System.String ");
                    dc.ColumnName = ds.Tables[0].Rows[i]["Name"].ToString();
                    dt.Columns.Add(dc);
                }
            }

            //赋值
            //在赋值时
            //1:首先查看原来dtable中是否应经存在和此条信息有相同的起始地,目的地
            //2: 如果有了,就修改此条数据
            //3:如果没有,add一条
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //遍历此临时表中的所有数据
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    //判断是否添加新的一条
                    if (ds.Tables[0].Rows[i]["BeginAddress"] == dt.Rows[j]["EndAddress"] && ds.Tables[0].Rows[i]["EndAddress"] == dt.Rows[j]["EndAddress"])
                    {   
                        //遍历所有的列
                        for (int m = 0; m < dt.Columns.Count; m++)
                        {
                            //如果列明和数据的吨位一致,则把价格填充到此列下
                            if (dt.Columns[m].ColumnName == ds.Tables[0].Rows[i]["VehicleType"])
                            {
                                dt.Rows[j][m] = ds.Tables[0].Rows[i]["Price"];
                            }
                        }
                    }
                    //新加一条数据
                    else
                    {
                        dr = dt.NewRow();
                        dr["BeginAddress "] = ds.Tables[0].Rows[i]["BeginAddress"];
                        dr["EndAddress "] = ds.Tables[0].Rows[i]["EndAddress"];
                        //遍历所有的列
                        for (int m = 0; m < dt.Columns.Count; m++)
                        {
                            //如果列明和数据的吨位一致,则把价格填充到此列下
                            if (dt.Columns[m].ColumnName == ds.Tables[0].Rows[i]["VehicleType"])
                            {
                                dr[m] = ds.Tables[0].Rows[i]["Price"];
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }
              
            }
            ds.Tables.Add(dt);//最终把dtable添加到ds中
            return ds;

你可能感兴趣的:(null,dataset)