关于asp.net的gridview控件的数据绑定问题,显示不出数据,求修改后的代码 30
前台代码<asp:ContentID="Content1"ContentPlaceHolderID="head"Runat="Server"></asp:Content>...
前台代码
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="UserName" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"
SortExpression="UserName" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [UserName] FROM [tb_User]"></asp:SqlDataSource>
</asp:Content>
后台代码
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //页面第一次进行加载时进行数据绑定
{
Bind();
}
}
public void Bind() //主页中控件GridView1的数据源方法
{
DBbase db = new DBbase();
string sql = "select * from tb_User order by UserName";
DataSet ds = db.ReturnDataSet(sql);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind(); //设置GridView1的数据源
DataBind(); //进行绑定
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//获取或设置当前页的索引
Bind();
}
} 展开
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="UserName" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"
SortExpression="UserName" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [UserName] FROM [tb_User]"></asp:SqlDataSource>
</asp:Content>
后台代码
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //页面第一次进行加载时进行数据绑定
{
Bind();
}
}
public void Bind() //主页中控件GridView1的数据源方法
{
DBbase db = new DBbase();
string sql = "select * from tb_User order by UserName";
DataSet ds = db.ReturnDataSet(sql);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind(); //设置GridView1的数据源
DataBind(); //进行绑定
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//获取或设置当前页的索引
Bind();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询