求gridview c#纯代码绑定mssql数据库的例子gridview太多属性,希望各位能给一个完整的例子参详一下,只用c#代码在cs文件绑定
解决方案 »
- ASP.net中怎么控制输入法状态
- 请教一个关于字符串匹配的正则表达式
- WebPartZone问题,急!!!高分求救
- !!!!!!!!!!!!急,如何在treeview里面判断我选择的是根节点还是子节点???????????
- 准备写一个asp.net+sqlserver构建的自助建站程序
- 关于取汉字的长度的问题(第2版)
- 关于DataList的2个问题
- 字符串格式问题,回者有分
- 巨难问题:多个RSS合并输出为一个RSS
- System.data.SqlClient 与 System.data.OleDb
- 谁有.Net开源的内容管理系统(CMS)
- 关于web.config中验证身份登录控制问题????在线关注ing...................
void Page_Load(Object sender, EventArgs e)
{ // This example uses Microsoft SQL Server and connects
// to the Northwind sample database. The data source needs
// to be bound to the GridView control only when the
// page is first loaded. Thereafter, the values are
// stored in view state.
if(!IsPostBack)
{ // Declare the query string.
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"; // Run the query and bind the resulting DataSet
// to the GridView control.
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
AuthorsGridView.DataSource = ds;
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
} } } DataSet GetData(String queryString)
{ // Retrieve the connection string stored in the Web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString; DataSet ds = new DataSet(); try
{
// Connect to the database and run the query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection); // Fill the DataSet.
adapter.Fill(ds); }
catch(Exception ex)
{ // The connection failed. Display an error message.
Message.Text = "Unable to connect to the database."; } return ds; }<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView DataBind Example</title>
</head>
<body>
<form id="form1" runat="server"> <h3>GridView DataBind Example</h3> <asp:label id="Message"
forecolor="Red"
runat="server"/> <br/> <asp:gridview id="AuthorsGridView"
autogeneratecolumns="true"
runat="server">
</asp:gridview> </form>
</body>
</html>
http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx