datatable 怎样循环遍历读取数据时只读取最后一条? 5
后台:protectedvoidPage_Load(objectsender,EventArgse){DataTabletable=Data.selectAll();fo...
后台:
protected void Page_Load(object sender, EventArgs e) { DataTable table = Data.selectAll(); foreach (DataRow row in table.Rows) { id.Text = row["ID"].ToString(); username.Text = row["UserName"].ToString(); phone.Text = row["Phone"].ToString(); address.Text = row["Address"].ToString();
}
前台:
<tr> <td> <asp:Label runat="server" ID="id" /> </td> <td> <asp:Label runat="server" ID="username" /> </td> <td> <asp:Label runat="server" ID="phone" /> </td> <td> <asp:Label runat="server" ID="address" /> </td> </tr> 展开
protected void Page_Load(object sender, EventArgs e) { DataTable table = Data.selectAll(); foreach (DataRow row in table.Rows) { id.Text = row["ID"].ToString(); username.Text = row["UserName"].ToString(); phone.Text = row["Phone"].ToString(); address.Text = row["Address"].ToString();
}
前台:
<tr> <td> <asp:Label runat="server" ID="id" /> </td> <td> <asp:Label runat="server" ID="username" /> </td> <td> <asp:Label runat="server" ID="phone" /> </td> <td> <asp:Label runat="server" ID="address" /> </td> </tr> 展开
2个回答
展开全部
把循环遍历改成for才可以呀。你前面是想要也填充空值还是什么,如果不是那根本就不需要循环语句呀。直接取最后一条呀,如 table.Rows[table.Rows.count-1]["UserName"].Tostring();
for(int i=0;i<table.Rows.count;i++)
{
if(i = table.Rows.count-1)
{
id.Text = row["ID"].ToString();
username.Text = row["UserName"].ToString();
phone.Text = row["Phone"].ToString();
address.Text = row["Address"].ToString();
return;
}
id.Text = "";
username.Text = "";
phone.Text = "";
address.Text = "";
}
//呵呵我猜你是要这样的结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询