为什么我照着教程上面做了个datagrid显示数据库中的数据,却什么也不显示,是哪里的原因啊? 没绑定吧:datagrid1.DataBind(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上说了datagrid1.DataBind() 呵。是因为没有绑定,但是这个绑定放在哪里呢?我把绑定放在private void Page_Load(object sender, System.EventArgs e) { DataGrid1.DataBind();// 在此处放置用户代码以初始化页面 }字段是显示出来了,可是数据没有! 数据库里有数据的啊! 1:要得到一个数据集2:指定datagrid1.datasource=dataset; 3:绑定 datagrid1.datasource=dataset;DataGrid1.DataBind(); 绑定数据:DataGrid1.DataSource=DataSet11;DataGrid1.DataBind(); void Page_Load(object sender, EventArgs E) {OleDbConnection DangManaConn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("DangMana.mdb")); OleDbCommand selectCMD = new OleDbCommand("SELECT * FROM tHResource", DangManaConn); OleDbDataAdapter opermanaDA = new OleDbDataAdapter(); opermanaDA.SelectCommand = selectCMD; DataSet opermanaDS = new DataSet(); opermanaDA.Fill(opermanaDS, "tHResource"); DataGrid1.DataSource = opermanaDS.Tables["tHResource"].DefaultView; DataGrid1.DataBind();} http://xml.sz.luohuedu.net/xml/Content.asp 我也是刚学,非常理解你的困难,呵呵,下面的是我弄了好几天才弄出来的datagrid显示数据的代码,基本都是用vs.net2003生成的,希望对你有帮助显示页面:<%@ Page Language="vb" AutoEventWireup="false" Codebehind="01.aspx.vb" Inherits="srz.srz_class"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <title>SRZ的.net页</title> <meta name="vs_showGrid" content="True"> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体" language="vbscript"> <asp:DataGrid id="my_DataGrid" runat="server" DataSource="<%# My_DataSet_1 %>" DataKeyField="id" AllowSorting="True" DataMember="f_board" ShowFooter="True" BackColor="White" BorderColor="#E7E7FF" Font-Names="宋体" GridLines="Horizontal" BorderStyle="None" BorderWidth="1px" CellPadding="3" PageSize="1" HorizontalAlign="Center" Height="162px" Width="864px" OnItemDataBound="Item_Bound"> <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle> <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle> <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle> <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle> <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle> <Columns> <asp:TemplateColumn SortExpression="id" HeaderText="哈哈" FooterText="呵呵"></asp:TemplateColumn> </Columns> <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle> </asp:DataGrid> <asp:DropDownList id=my_DropDownList style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 345px" runat="server" Height="48px" Width="312px" DataSource = "<%# My_DataSet_1%>" DataMember = "f_board" DataTextField = "_date" DataValueField = "id"> </asp:DropDownList></FONT> </form> </body></HTML>.vb页面:Public Class srz_class Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.my_SqlConnection = New System.Data.SqlClient.SqlConnection Me.my_SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter Me.my_SqlSelectCommand = New System.Data.SqlClient.SqlCommand Me.My_DataSet_1 = New srz.my_DataSet CType(Me.My_DataSet_1, System.ComponentModel.ISupportInitialize).BeginInit() ' 'my_SqlConnection ' Me.my_SqlConnection.ConnectionString = "user id=sa;data source=localhost;initial catalog=temp_web;password=换你自己的" ' 'my_SqlDataAdapter ' Me.my_SqlDataAdapter.SelectCommand = Me.my_SqlSelectCommand Me.my_SqlDataAdapter.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "f_board", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("_related_id", "related_id"), New System.Data.Common.DataColumnMapping("_re_id", "re_id"), New System.Data.Common.DataColumnMapping("__top", "_top"), New System.Data.Common.DataColumnMapping("_quintessence", "quintessence"), New System.Data.Common.DataColumnMapping("_lock", "lock"), New System.Data.Common.DataColumnMapping("_user_id", "user_id"), New System.Data.Common.DataColumnMapping("_face", "face"), New System.Data.Common.DataColumnMapping("_enclosure", "enclosure"), New System.Data.Common.DataColumnMapping("__date", "_date"), New System.Data.Common.DataColumnMapping("_total_", "total_"), New System.Data.Common.DataColumnMapping("_title", "title"), New System.Data.Common.DataColumnMapping("_text", "text"), New System.Data.Common.DataColumnMapping("_id", "id")})}) ' 'my_SqlSelectCommand ' Me.my_SqlSelectCommand.CommandText = "SELECT related_id, re_id, _top, quintessence, lock, user_id, face, enclosure, _da" & _ "te, total_, title, text, id FROM f_board" Me.my_SqlSelectCommand.Connection = Me.my_SqlConnection ' 'My_DataSet_1 ' Me.My_DataSet_1.DataSetName = "my_DataSet" Me.My_DataSet_1.Locale = New System.Globalization.CultureInfo("zh-CN") CType(Me.My_DataSet_1, System.ComponentModel.ISupportInitialize).EndInit() End Sub Protected WithEvents my_SqlConnection As System.Data.SqlClient.SqlConnection Protected WithEvents my_SqlDataAdapter As System.Data.SqlClient.SqlDataAdapter Protected WithEvents my_SqlSelectCommand As System.Data.SqlClient.SqlCommand Protected WithEvents My_DataSet_1 As srz.my_DataSet Protected WithEvents my_DataGrid As System.Web.UI.WebControls.DataGrid Protected WithEvents my_DropDownList As System.Web.UI.WebControls.DropDownList '注意: 以下占位符声明是 Web 窗体设计器所必需的。 '不要删除或移动它。 Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 my_SqlDataAdapter.Fill(My_DataSet_1, "f_board") Page.DataBind() End Sub Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then '鼠标挪到该行的时候整行变颜色 e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#eeeeee'") e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#E7E7FF'") '鼠标挪到列上后,该列会变颜色 e.Item.Cells(3).Attributes.Add("onmouseover", "this.style.backgroundColor='#eeeeee'") e.Item.Cells(3).Attributes.Add("onmouseout", "this.style.backgroundColor='#E7E7FF'") '鼠标挪到该表格上时会变成手形 e.Item.Cells(7).Style("cursor") = "hand" '鼠标挪到表格上并点击后会弹出对话框 e.Item.Cells(6).Attributes.Add("onclick", "alert('You click at ID: " & e.Item.Cells(1).Text & "!');") End If End Sub 'Item_BoundEnd Class 关于Repeate数据循环的问题 该如何分解这个字符串,谢谢. 求救!页面转换以后cookies都丢光了! 删除确认问题 如何在页面中动态嵌入html页面? 求一句XPATH。。谢谢(XML和asp.net的问题) 一个变态又有意思的DataGrid问题(早8点到晚8点等大家来讨论) 关于用户登录状态问题一直不理解 求救!怎样获取动态生成控件的值? 一个让我非常郁闷的问题,向大家咨询一下看看有没有解决的办法 在自定义控件上添加的toolbar的toolbarlabel上显示系统时间啊?asp.net+vb???????? 未能在设计视图中打开
datagrid1.DataBind()
我把绑定放在private void Page_Load(object sender, System.EventArgs e)
{
DataGrid1.DataBind();// 在此处放置用户代码以初始化页面
}
字段是显示出来了,可是数据没有! 数据库里有数据的啊!
2:指定datagrid1.datasource=dataset;
3:绑定
DataGrid1.DataBind();
DataGrid1.DataSource=DataSet11;
DataGrid1.DataBind();
{
OleDbConnection DangManaConn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("DangMana.mdb"));
OleDbCommand selectCMD = new OleDbCommand("SELECT * FROM tHResource", DangManaConn);
OleDbDataAdapter opermanaDA = new OleDbDataAdapter();
opermanaDA.SelectCommand = selectCMD;
DataSet opermanaDS = new DataSet();
opermanaDA.Fill(opermanaDS, "tHResource");
DataGrid1.DataSource = opermanaDS.Tables["tHResource"].DefaultView;
DataGrid1.DataBind();
}
显示页面:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="01.aspx.vb" Inherits="srz.srz_class"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>SRZ的.net页</title>
<meta name="vs_showGrid" content="True">
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体" language="vbscript">
<asp:DataGrid id="my_DataGrid" runat="server" DataSource="<%# My_DataSet_1 %>" DataKeyField="id" AllowSorting="True" DataMember="f_board" ShowFooter="True" BackColor="White" BorderColor="#E7E7FF" Font-Names="宋体" GridLines="Horizontal" BorderStyle="None" BorderWidth="1px" CellPadding="3" PageSize="1" HorizontalAlign="Center" Height="162px" Width="864px" OnItemDataBound="Item_Bound">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:TemplateColumn SortExpression="id" HeaderText="哈哈" FooterText="呵呵"></asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:DropDownList id=my_DropDownList style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 345px" runat="server" Height="48px" Width="312px" DataSource = "<%# My_DataSet_1%>" DataMember = "f_board" DataTextField = "_date" DataValueField = "id">
</asp:DropDownList></FONT>
</form>
</body>
</HTML>.vb页面:
Public Class srz_class
Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.my_SqlConnection = New System.Data.SqlClient.SqlConnection
Me.my_SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
Me.my_SqlSelectCommand = New System.Data.SqlClient.SqlCommand
Me.My_DataSet_1 = New srz.my_DataSet
CType(Me.My_DataSet_1, System.ComponentModel.ISupportInitialize).BeginInit()
'
'my_SqlConnection
'
Me.my_SqlConnection.ConnectionString = "user id=sa;data source=localhost;initial catalog=temp_web;password=换你自己的"
'
'my_SqlDataAdapter
'
Me.my_SqlDataAdapter.SelectCommand = Me.my_SqlSelectCommand
Me.my_SqlDataAdapter.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "f_board", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("_related_id", "related_id"), New System.Data.Common.DataColumnMapping("_re_id", "re_id"), New System.Data.Common.DataColumnMapping("__top", "_top"), New System.Data.Common.DataColumnMapping("_quintessence", "quintessence"), New System.Data.Common.DataColumnMapping("_lock", "lock"), New System.Data.Common.DataColumnMapping("_user_id", "user_id"), New System.Data.Common.DataColumnMapping("_face", "face"), New System.Data.Common.DataColumnMapping("_enclosure", "enclosure"), New System.Data.Common.DataColumnMapping("__date", "_date"), New System.Data.Common.DataColumnMapping("_total_", "total_"), New System.Data.Common.DataColumnMapping("_title", "title"), New System.Data.Common.DataColumnMapping("_text", "text"), New System.Data.Common.DataColumnMapping("_id", "id")})})
'
'my_SqlSelectCommand
'
Me.my_SqlSelectCommand.CommandText = "SELECT related_id, re_id, _top, quintessence, lock, user_id, face, enclosure, _da" & _
"te, total_, title, text, id FROM f_board"
Me.my_SqlSelectCommand.Connection = Me.my_SqlConnection
'
'My_DataSet_1
'
Me.My_DataSet_1.DataSetName = "my_DataSet"
Me.My_DataSet_1.Locale = New System.Globalization.CultureInfo("zh-CN")
CType(Me.My_DataSet_1, System.ComponentModel.ISupportInitialize).EndInit() End Sub
Protected WithEvents my_SqlConnection As System.Data.SqlClient.SqlConnection
Protected WithEvents my_SqlDataAdapter As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents my_SqlSelectCommand As System.Data.SqlClient.SqlCommand
Protected WithEvents My_DataSet_1 As srz.my_DataSet
Protected WithEvents my_DataGrid As System.Web.UI.WebControls.DataGrid
Protected WithEvents my_DropDownList As System.Web.UI.WebControls.DropDownList '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
my_SqlDataAdapter.Fill(My_DataSet_1, "f_board")
Page.DataBind()
End Sub Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'鼠标挪到该行的时候整行变颜色
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#eeeeee'")
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#E7E7FF'")
'鼠标挪到列上后,该列会变颜色
e.Item.Cells(3).Attributes.Add("onmouseover", "this.style.backgroundColor='#eeeeee'")
e.Item.Cells(3).Attributes.Add("onmouseout", "this.style.backgroundColor='#E7E7FF'")
'鼠标挪到该表格上时会变成手形
e.Item.Cells(7).Style("cursor") = "hand"
'鼠标挪到表格上并点击后会弹出对话框
e.Item.Cells(6).Attributes.Add("onclick", "alert('You click at ID: " & e.Item.Cells(1).Text & "!');")
End If
End Sub 'Item_BoundEnd Class