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

大家帮我看一下,代码执行后数据显示不出来
public   DataTable   bb(DataSet   ds)
{
DataRow   dr1;
DataTable   dt   =   new   DataTable();

System.Random   r   =   new   Random();
dt.Columns.Add(new   DataColumn( "id ",typeof(int)));
dt.Columns.Add(new   DataColumn( "name ",typeof(string)));
dt.Columns.Add(new   DataColumn( "money ",typeof(string)));
foreach(DataRow   dr   in   ds.Tables[0].Rows)
{
dr1   =   dt.NewRow();
dr1[ "id "]   =   int.Parse(dr[0].ToString());
dr1[ "name "]   =   dr[1].ToString();
dr1[ "money "]   =   r.Next(0,10000).ToString()+ "¥ ";

}

return   dt;
}

------解决方案--------------------
这只不过是返回个DataTable,楼主想怎么显示啊?
------解决方案--------------------
public DataTable bb(DataSet ds)
{
DataRow dr1;
DataTable dt = new DataTable();

System.Random r = new Random();
dt.Columns.Add(new DataColumn( "id ",typeof(int)));
dt.Columns.Add(new DataColumn( "name ",typeof(string)));
dt.Columns.Add(new DataColumn( "money ",typeof(string)));
foreach(DataRow dr in ds.Tables[0].Rows)
{
dr1 = dt.NewRow();
dr1[ "id "] = int.Parse(dr[0].ToString());
dr1[ "name "] = dr[1].ToString();
dr1[ "money "] = r.Next(0,10000).ToString()+ "¥ ";
dt.Rows.Add(dr1); //Add this line
}

return dt;
}
------解决方案--------------------
缺少语句:应该如下:

foreach(DataRow dr in ds.Tables[0].Rows)
{
dr1 = dt.NewRow();
dr1[ "id "] = int.Parse(dr[0].ToString());
dr1[ "name "] = dr[1].ToString();
dr1[ "money "] = r.Next(0,10000).ToString()+ "¥ ";

// 需要添加的语句
dt.Rows.Add(dr1);

}
------解决方案--------------------
楼上说得没错

需要添加的语句 dt.Rows.Add(dr1);