ASP.Net/ADO.net Database/DataGrid

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

asp:datagrid: sort expression (VB.net)

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim DBConn as OleDbConnection
        Dim DBCommand As OleDbDataAdapter
        Dim DSPageData as New DataSet
        DBConn = New OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))
        DBCommand = New OleDbDataAdapter _
            ("Select * " _
            & "From Employee " _
            & "Order By LastName, FirstName", DBConn)
        DBCommand.Fill(DSPageData, _
            "Employee")
        dgEmps.DataSource = _
            DSPageData.Tables("Employee").DefaultView
        dgEmps.DataBind()
    End If
End Sub
Sub Sort_Grid(ByVal Sender as Object, _
    ByVal E as DataGridSortCommandEventArgs)
    Dim DBConn as OleDbConnection
    Dim DBCommand As OleDbDataAdapter
    Dim DSPageData as New DataSet
    DBConn = New OleDbConnection( _
        "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        & "DATA SOURCE=" _
        & Server.MapPath("EmployeeDatabase.mdb;"))
    DBCommand = New OleDbDataAdapter _
        ("Select * " _
        & "From Employee Order By " _
        & E.SortExpression.ToString() , DBConn)
    DBCommand.Fill(DSPageData, _
        "Employee")
    dgEmps.DataSource = _
        DSPageData.Tables("Employee").DefaultView
    dgEmps.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sorting a DataGrid Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:datagrid
    id="dgEmps" 
    allowsorting="True"
    onsortcommand="Sort_Grid"
    autogeneratecolumns="false"
    runat="server" 
>
    <columns>
        <asp:boundcolumn 
            headertext="Last Name" 
            datafield="LastName"
            sortexpression="LastName"
        />
        <asp:boundcolumn 
            headertext="First Name" 
            datafield="FirstName"
            sortexpression="FirstName"
        />
        <asp:boundcolumn 
            headertext="ID" 
            datafield="ID"
            DataFormatString="{0:c}"
            sortexpression="ID"
        />
    </columns>
</asp:datagrid>
</form>
</BODY>
</HTML>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>


Bind asp:DataGrid to access database (VB.net)

<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Oledb" %>
<script language="vb" runat="server">
  Sub Page_Load()
    Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
      strConnection += "Data Source=yourDatabase.mdb"
      data_src.text = strConnection
    Dim strSQL as string = "SELECT * FROM products;"
    Dim objDataSet as new DataSet()
    Dim objConnection as new OledbConnection(strConnection)
    Dim objAdapter as new OledbDataAdapter(strSQL, objConnection)
    objAdapter.Fill(objDataSet, "products")
    Dim objDataView as New DataView(objDataSet.Tables("products"))
    dgNameList.DataSource=objDataView
    dgNameList.DataBind()
  end Sub
</script>
<html>
  <body>
  <h4>Reading data from the connection
  <asp:label id="data_src" runat="server"/> to the DataGrid control.</h4>
  <asp:datagrid id="dgNameList" runat="server" /><br />
  </body>
</html>



Bind data through SqlConnection to asp:DataGrid (VB.net)

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    Dim DBConn as SQLConnection
    Dim DBCommand As SQLDataAdapter
    Dim DSPageData as New DataSet
"        DBConn = New SQLConnection("server=localhost;" _
 "           & "Initial Catalog=TT;" _
  "          & "User Id=sa;" _
   "         & "Password=yourpassword;")
    DBConn = New SQLConnection("Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password="password";")
    DBCommand = New SQLDataAdapter _
        ("Select LastName as [Last Name], " _
        & "FirstName as [First Name], " _
        & "Salary as [Current Salary] " _
        & "from Employee", DBConn)
    DBCommand.Fill(DSPageData, _
        "EmployeesAll")
    dgEmps.DataSource = _
        DSPageData.Tables("EmployeesAll").DefaultView
    dgEmps.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Displaying SQL Server Data in a DataGrid Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:datagrid
    id="dgEmps" 
    runat="server" 
    autogeneratecolumns="True"
>
</asp:datagrid>
</form>
</BODY>
</HTML>



Bind Data to asp DataGrid in C#

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="C#" runat="server">
void Page_Load(object sender, EventArgs e)
{ 
  OleDbConnection objConnection;
  OleDbDataAdapter objCommand;  
  string strConnect; 
  string strCommand; 
  DataSet DataSet1 = new DataSet();
 
  strConnect =  @"Provider=Microsoft.Jet.OLEDB.4.0;";
  //If you don"t have the EmployeeDatabase.mdb database, then you will have to 
  //change the following three lines to a different database
 
  strConnect += @"Data Source=" + MapPath("EmployeeDatabase.mdb;");
  
  strConnect += "Persist Security Info=False";
  strCommand = "SELECT FirstName, LastName FROM Employee";
  objConnection = new OleDbConnection(strConnect);
  objCommand = new OleDbDataAdapter(strCommand, objConnection);
  objCommand.Fill(DataSet1, "Employee");
  DataGrid1.DataSource=DataSet1.Tables["Employee"].DefaultView;
  DataGrid1.DataBind();
}
</script>
<html>
<head>
<title>Data Grid Control example</title>
</head>
<body>
<asp:DataGrid id="DataGrid1" runat="server"  />
</body>
</html>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>


Bind to two different asp:datagrid (VB.net)

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    Dim DBConn as OleDbConnection
    Dim DBCommand As OleDbDataAdapter
    Dim DSPageData as New DataSet
    DBConn = New OleDbConnection( _
        "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        & "DATA SOURCE=" _
        & Server.MapPath("EmployeeDatabase.mdb;"))
    DBCommand = New OleDbDataAdapter _
        ("Select * from Employee", DBConn)
    DBCommand.Fill(DSPageData, _
        "EmployeeAll")
    DBCommand = New OleDbDataAdapter _
        ("Select LastName " _
        & " from Employee", DBConn)
    DBCommand.Fill(DSPageData, _
        "Employees")
    dgEmps1.DataSource = _
        DSPageData.Tables("EmployeeAll").DefaultView
    dgEmps1.DataBind()
    dgEmps2.DataSource = _
        DSPageData.Tables("Employees").DefaultView
    dgEmps2.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Displaying Access Data in a DataGrid Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:datagrid
    id="dgEmps1" 
    runat="server" 
    autogeneratecolumns="True"
>
</asp:datagrid>
<BR><BR>
<asp:datagrid
    id="dgEmps2" 
    runat="server" 
    autogeneratecolumns="True"
>
</asp:datagrid>
</form>
</BODY>
</HTML>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>


Pagable datagrid (VB.net)

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
  <head><title>DataGrid Paging</title></head>
  <body>
    <h3>DataGrid Paging</h3>
    <form runat="Server">
      <asp:DataGrid id="dgProducts" runat="server"
                    AllowPaging="True"
                    OnPageIndexChanged="GridPageChange"
                    PageSize="8" />
    </form>
  </body>
</html>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
  If Not Page.IsPostBack Then
    DataFiller()
  End IF
End Sub
Sub DataFiller()
  Dim strConnection As String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password="password";"
  Dim objConnection As New SqlConnection(strConnection)
  Dim strSqlProducts As String = "SELECT ID, FirstName FROM Employee"
  Dim objAdapter As New SqlDataAdapter(strSqlProducts, objConnection)
  Dim objDataSet As New DataSet()
  objAdapter.Fill(objDataSet, "Employee")
  dgProducts.PagerStyle.NextPageText = "Next"
  dgProducts.PagerStyle.PrevPageText = "Previous"
  dgProducts.DataSource = objDataSet.Tables.Item("Employee")
  dgProducts.DataBind()
End Sub
Sub GridPageChange(S As Object, E As DataGridPageChangedEventArgs)
  dgProducts.CurrentPageIndex = E.NewPageIndex
  DataFiller()
End Sub
</script>



Set asp:datagrid title bar (C#)

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script Language="c#" runat="server">
  void Page_Load()
  {
    string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
//    strConnection += @"Data Source=C:\Northwind.mdb";
    strConnection += @"Data Source="+MapPath("EmployeeDatabase.mdb");
    string strSQL = "Select LastName, FirstName, ID from Employee;";
    OleDbConnection objConnection = new OleDbConnection(strConnection);
    OleDbCommand objCommand = new OleDbCommand(strSQL,objConnection);
    objConnection.Open();
    dgEmps.DataSource = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
    dgEmps.DataBind();
  }
</script>
<html>
  <body>
    <asp:datagrid id="dgEmps"
        runat="server"
        CellPadding="3"
        Font-Name="arial"
        Font-Size="8pt"
        HeaderStyle-BackColor="#dcdcdc"
        HeaderStyle-ForeColor="blue"
    />
  </body>
</html>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>


Sort asp:datagrid (VB.net)

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim DBConn as OleDbConnection
        Dim DBCommand As OleDbDataAdapter
        Dim DSPageData as New DataSet
        DBConn = New OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))
        DBCommand = New OleDbDataAdapter _
            ("Select * " _
            & "From Employee " _
            & "Order By LastName, FirstName", DBConn)
        DBCommand.Fill(DSPageData, _
            "Employee")
        dgEmps.DataSource = _
            DSPageData.Tables("Employee").DefaultView
        dgEmps.DataBind()
    End If
End Sub
Sub Sort_Grid(ByVal Sender as Object, _
    ByVal E as DataGridSortCommandEventArgs)
    Dim DBConn as OleDbConnection
    Dim DBCommand As OleDbDataAdapter
    Dim DSPageData as New DataSet
    DBConn = New OleDbConnection( _
        "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        & "DATA SOURCE=" _
        & Server.MapPath("EmployeeDatabase.mdb;"))
    DBCommand = New OleDbDataAdapter _
        ("Select * " _
        & "From Employee Order By " _
        & E.SortExpression.ToString() , DBConn)
    DBCommand.Fill(DSPageData, _
        "Employee")
    dgEmps.DataSource = _
        DSPageData.Tables("Employee").DefaultView
    dgEmps.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sorting a DataGrid Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:datagrid
    id="dgEmps" 
    allowsorting="True"
    onsortcommand="Sort_Grid"
    autogeneratecolumns="false"
    runat="server" 
>
    <columns>
        <asp:boundcolumn 
            headertext="Last Name" 
            datafield="LastName"
            sortexpression="LastName"
        />
        <asp:boundcolumn 
            headertext="First Name" 
            datafield="FirstName"
            sortexpression="FirstName"
        />
        <asp:boundcolumn 
            headertext="ID" 
            datafield="ID"
            DataFormatString="{0:c}"
            sortexpression="ID"
        />
    </columns>
</asp:datagrid>
</form>
</BODY>
</HTML>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>