关于gridview前面显示,后台绑定的问题

xyz08085213 2016-03-12 08:58:47
先贴上代码:
前台代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="Pid" HeaderText="Pid" SortExpression="Pid" />
<asp:BoundField DataField="Pname" HeaderText="Pname" SortExpression="Pname" />
<asp:BoundField DataField="Pmode" HeaderText="Pmode" SortExpression="Pmode" />
<asp:BoundField DataField="Ptime" HeaderText="Ptime" SortExpression="Ptime" />
</Columns>
</asp:GridView>
后台绑定代码
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.OleDb;
using System.Web.UI.WebControls;

public partial class Uistme_Usmcon : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string _ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
OleDbConnection conn = new OleDbConnection(_ConnectionString);
conn.Open();

string sql = "select * From [Pinfo] order by id desc";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource =ds;
GridView1.DataBind();
conn.Close();

}
}
运行的总是提示da.Fill(ds);至少 有一个值没有绑定
改成
da.Fill(ds,"Pinfo");
GridView1.DataSource =ds,Pinfo;
问题依然存在。
...全文
240 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dull_a 2016-03-14
  • 打赏
  • 举报
回复
我以前也出现过这样的问题,你在前台设计里面看看里面绑定的是否正确,很多问题都是出在里面的,没问题就看下你sql语句是否正确,一步步来
执手年华 2016-03-14
  • 打赏
  • 举报
回复
楼主,建议你写程序时候,绑定显示数据这块,尽量用DataList!!最好是别用GridView
insus 2016-03-14
  • 打赏
  • 举报
回复
因为GridView有使用SortExpression排序字段,建议GrieView的数据源不要使用ds,ds.Table[0],而是要把DataTable转换为DataView.
正怒月神 版主 2016-03-14
  • 打赏
  • 举报
回复
select * From [Pinfo] order by id desc *号先改成字段名,看看
xyz08085213 2016-03-14
  • 打赏
  • 举报
回复
高手们,说的很轻松,但还是没有解决问题,不管怎么说感谢你们,但也请你们理解下,什么叫新手。
xyz08085213 2016-03-13
  • 打赏
  • 举报
回复
引用 6 楼 sp1234 的回复:
从这个教程学,你贴的代码说明你看的教程有严重的问题 http://blog.csdn.net/weimaomin/article/details/1009685 另外,Page_Load 中要写 if(!IsPostback)判断语句。
谢谢;早上起来已经改了,现在贴上代码 using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Data.OleDb; using System.Web.UI.WebControls; public partial class Uistme_Usmcon : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { BindGridView(); } } private void BindGridView() { string _ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; OleDbConnection conn = new OleDbConnection(_ConnectionString); conn.Open(); string sql = "Select * From Pinfo"; OleDbCommand cmd = new OleDbCommand(sql, conn); OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables["Pinfo"]; GridView1.DataBind(); conn.Close(); } }
  • 打赏
  • 举报
回复
从这个教程学,你贴的代码说明你看的教程有严重的问题 http://blog.csdn.net/weimaomin/article/details/1009685 另外,Page_Load 中要写 if(!IsPostback)判断语句。
xyz08085213 2016-03-13
  • 打赏
  • 举报
回复
引用 4 楼 abcd2038321 的回复:
需要绑定每一列的数据吧,直接填充整个表,貌似不能用
大哥能给个具体代码吗,没有作伸手党的意思,早上我又找了下,现在不报错了,可没读出数据,应该是没有绑定数据,就是想少走弯路而已,谢谢。
香蕉皮不滑 2016-03-13
  • 打赏
  • 举报
回复
需要绑定每一列的数据吧,直接填充整个表,貌似不能用
xyz08085213 2016-03-13
  • 打赏
  • 举报
回复
引用 7 楼 sp1234 的回复:
或者 http://www.jiancool.com/article/4251466597/;jsessionid=24312C06DAACA43D553168B28CF459D0 http://www.thinksaas.cn/group/topic/277376/
在您推荐的http://www.jiancool.com/article/4251466597/;jsessionid=24312C06DAACA43D553168B28CF459D0这篇文章里 类:NorthwindTableAdapters.ProductsTableAdapter: C# NorthwindTableAdapters.ProductsTableAdapter productsAdapter = new NorthwindTableAdapters.ProductsTableAdapter(); Northwind.ProductsDataTable products; products = productsAdapter.GetProducts(); foreach (Northwind.ProductsRow productRow in products) Response.Write("Product: " + productRow.ProductName + "<br />"); 是怎么生成的呢?直接写代码还是自动生成,在哪f个文件里写?麻烦了?
xyz08085213 2016-03-12
  • 打赏
  • 举报
回复
首先感谢楼上大哥,但问题还是一样
江南小鱼 2016-03-12
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" /> <asp:BoundField DataField="Pid" HeaderText="Pid" SortExpression="Pid" /> <asp:BoundField DataField="Pname" HeaderText="Pname" SortExpression="Pname" /> <asp:BoundField DataField="Pmode" HeaderText="Pmode" SortExpression="Pmode" /> <asp:BoundField DataField="Ptime" HeaderText="Ptime" SortExpression="Ptime" /> </Columns> </asp:GridView> 也有可能DataKeyNames的列要出现在Columns集合里面
江南小鱼 2016-03-12
  • 打赏
  • 举报
回复
1、确定下你的sql语句,在数据库里面执行是否正常
select * From [Pinfo] order by id desc
2、改成
GridView1.DataSource =ds.Tables[0];

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧