Binding DataReader to DataGrid : DataGrid « ASP.net Controls « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

    SqlDataReader myReader;
    
    void Page_Load(object sender, EventArgs e)
    {
       string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);
    
       SqlConnection myConnection = new SqlConnection(ConnectionString);
       SqlCommand myCommand = new SqlCommand();
       myCommand.Connection = myConnection;
    
       try {
          myConnection.Open();
          if (!(Page.IsPostBack)) {
             myCommand.CommandText = "SELECT PublisherID, PublisherName FROM Publisher";
             myReader = myCommand.ExecuteReader();
             ListBox1.DataBind();
             myReader.Close();
          } else {
             myCommand.CommandText = "SELECT * FROM Book WHERE BookPublisherID=" + ListBox1.SelectedItem.Value;
             myReader = myCommand.ExecuteReader();
             DataGrid1.DataSource = myReader;
             DataGrid1.DataBind();
             myReader.Close();
          }
       } catch (Exception ex) {
          throw (ex);
       } finally {
          myConnection.Close();
       }
    }
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <h2>A RadioButtonList 
        </h2>
            <asp:RadioButtonList id="RadioButtonList1" runat="server" AutoPostBack="True" DataValueField="PublisherID" DataTextField="PublisherName" DataSource="<%# myReader %>"></asp:RadioButtonList>
        <h2>A DropDownList 
        </h2>
            <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" DataValueField="PublisherID" DataTextField="PublisherName" DataSource="<%# myReader %>"></asp:DropDownList>
        <h2>A ListBox 
        </h2>
            <asp:ListBox id="ListBox1" runat="server" AutoPostBack="True" DataValueField="PublisherID" DataTextField="PublisherName" DataSource="<%# myReader %>" Rows="5"></asp:ListBox>
            <asp:Label id="Label1" runat="server"></asp:Label>
            <asp:DataGrid id="DataGrid1" runat="server" HorizontalAlign="Justify" BorderColor="#CC9966" BackColor="White" CellPadding="4" BorderWidth="1px" BorderStyle="None">
                <FooterStyle forecolor="#330099" backcolor="#FFFFCC"></FooterStyle>
                <HeaderStyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></HeaderStyle>
                <PagerStyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC"></PagerStyle>
                <SelectedItemStyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></SelectedItemStyle>
                <ItemStyle forecolor="#330099" backcolor="White"></ItemStyle>
            </asp:DataGrid>
    </form>
</body>
</html>




File: Web.config

<configuration>
    <appSettings>
        <add key="MSDEConnectString" value="server=(local)\YourDatabase;database=Books;uid=YourID;pwd=letmein;" />
    </appSettings>
</configuration>








3.37.DataGrid
3.37.1.DataGrid Data Binding
3.37.2.Use asp:DataGrid to edit data in an Access table (VB.net)
3.37.3.Use asp:HyperlinkColumn and asp:ButtonColumn in asp:DataGrid (VB.net)
3.37.4.asp:EditCommandColumn in asp:DataGrid (VB.net)
3.37.5.Bind data of directories to asp:DataGrid (VB.net)
3.37.6.Bind file information to asp:DataGrid (VB.net)
3.37.7.Binding DataReader to DataGrid