为什么会出现"无法找到列0"
//将Datareader内容写入datatable
private void filldate(string gj,string lx)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn( "pid ", typeof(Int32)));
dt.Columns.Add(new DataColumn( "ptitle ",typeof(string)));
dt.Columns.Add(new DataColumn( "ptime ",typeof(string)));
dt.Columns.Add(new DataColumn( "phit ", typeof(Int32)));
sdr=tool.psearch(gj,lx, "psearch ");//sdr是通过Datareader读取数据
while(sdr.Read())
{
DataRow dr=dt.NewRow();
dr[0]=Int32.Parse(sdr[ "pid "].ToString());
dr[1]=sdr[ "ptitle "].ToString();
dr[2]=Convert.ToDateTime(sdr[ "ptime "].ToString());
dr[3]=Int32.Parse(sdr[ "phit "].ToString());
dt.Rows.Add(dr);
}
sdr.Close();
this.Session[ "searchtable "]=dt;
}
//创建数据源
private ICollection CreateDataSource()
{
DataRow dr;
DataTable dtnew=new DataTable( "currenttable ");
DataTable dt=(DataTable)this.Session[ "searchtable "];
for (int i = startIndex; i < (startIndex + this.DataGrid2.PageSize); i++)
{
dr = dtnew.NewRow();
dr[0] = Int32.Parse(dt.Rows[i][0].ToString());//这一行每次都报错!!!!!!!!!!!
dr[1] = dt.Rows[i][1].ToString();
dr[2] = Convert.ToDateTime(dt.Rows[i][2].ToString());
dr[3] = Int32.Parse(dt.Rows[i][3].ToString());
dtnew.Rows.Add(dr);
}
DataView dv = new DataView(dtnew);
return dv;
}
------解决方案--------------------新的datatable没有创建列
private ICollection CreateDataSource()
{
DataRow dr;
DataTable dtnew=new DataTable( "currenttable ");
dtnew.Columns.Add(new DataColumn( "pid ", typeof(Int32)));
dtnew.Columns.Add(new DataColumn( "ptitle ",typeof(string)));
dtnew.Columns.Add(new DataColumn( "ptime ",typeof(string)));
dtnew.Columns.Add(new DataColumn( "phit ", typeof(Int32)));
DataTable dt=(DataTable)this.Session[ "searchtable "];
for (int i = startIndex; i < (startIndex + this.DataGrid2.PageSize); i++)
{
dr = dtnew.NewRow();
dr[0] = Int32.Parse(dt.Rows[i][0].ToString());//这一行每次都报错!!!!!!!!!!!
dr[1] = dt.Rows[i][1].ToString();
dr[2] = Convert.ToDateTime(dt.Rows[i][2].ToString());
dr[3] = Int32.Parse(dt.Rows[i][3].ToString());
dtnew.Rows.Add(dr);
}
DataView dv = new DataView(dtnew);
return dv;
}