求gridview c#纯代码绑定mssql数据库的例子gridview太多属性,希望各位能给一个完整的例子参详一下,只用c#代码在cs文件绑定

解决方案 »

  1.   


    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>
      

  2.   

    ojlovecd ,有更新,删除和指定页数跳转的例子也发出来,谢谢哦
      

  3.   

    引用清清月儿的blog
    http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx