50分求教!请问如何选择datatable中的前10行记录?
一个datatable,有20行记录,想选取前10行记录
我的思路是删除后台10行,但是出错
int rowsCount = dt.rows.count;
for(int i=9;i <rowsCount;i++)
{
dt.rows.RemoveAt(i);
}
提示,在位置 10 处没有任何行。
------解决方案--------------------int rowsCount = dt.rows.count;
int i = 1;
while(i <=10)
{
if(dt.rows.count ==0) break;
dt.rows.RemoveAt(0);
i++;
}
------解决方案--------------------for(int i=rowsCount-1;i <10;i++)
{
dt.rows.RemoveAt(i);
}
------解决方案--------------------int rowsCount = dt.rows.count;
for(int i=0;i <=9;i++)
{
if(dt.rows.count ==0) break;
dt.rows.RemoveAt(0);
}
------解决方案--------------------for(int i=rowsCount-1;i> 9;i=rowsCount)
{
dt.rows.RemoveAt(i);
}
------解决方案--------------------我刚写的没问题
public void SetDataBind()
{
SQLExe.SQLExe myExe=new SQLExe.SQLExe();
//DataSet ds = new DataSet();
string str = "select * from test ";
this.ds = myExe.SQLstr(str);
ViewState[ "ds "] = this.ds.Tables[0];
this.DataGrid1.DataSource = this.ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable tempds = (DataTable)ViewState[ "ds "];
int count = tempds.Rows.Count;
for (int i = 0; i < 3; i++)
{
tempds.Rows.RemoveAt(i);
}
DataSet temp = new DataSet();
temp.Tables.Add(tempds);
this.DataGrid1.DataSource = temp;
this.DataGrid1.DataBind();
}