DataGrid Data Binding : DataGrid « ASP.net Controls « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
   'declare connection
   dim Conn as new OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))
   
   
   sub GetData(Sender as Object, e as EventArgs) 
      dim objCmd as OleDbCommand = new OleDbCommand _
         ("select * from employee where ID = @ID", Conn)
      dim objReader as OleDbDataReader
      
      dim objParam as OleDbParameter
      objParam = objCmd.Parameters.Add("@ID", OleDbType.Integer)
      objParam.Direction = ParameterDirection.Input
      objParam.Value = tbID.Text
           
      try
         objCmd.Connection.Open()
         objReader = objCmd.ExecuteReader
         
         dgData.DataSource = objReader
         dgData.DataBind()
       
         objReader.Close
         objCmd.Connection.Close()
      catch objEx as FormatException
         lblMessage.Text = objEx.Message
      catch objEx as OleDbException
         lblMessage.Text = "Database error!"
      catch objEx as Exception
         lblMessage.Text = "Unknown error!"
      end try      
   end sub
</script>

<html><body>
   <form runat="server">
      <asp:Label id="lblMessage" runat="server"
         maintainstate=false /><br>
      Enter an ID: <asp:TextBox id="tbID" runat="server"
         AutoPostBack=True
         OnTextChanged=GetData />
      <asp:DataGrid id="dgData" runat="server" 
         BorderColor="black" 
         GridLines="Vertical" 
         width="100%"
         Font-Name="Arial" 
         Font-Size="8pt" 
         HeaderStyle-BackColor="#cccc99"
         ItemStyle-BackColor="#ffffff"
         AlternatingItemStyle-Backcolor="#cccccc"
         AutoGenerateColumns="true" />

   </form>
</body></html>








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