2- Ado.net ile datagrid 2
Ado.net ile datagrid 2



Bildi?imiz gibi datagrid nesnesi Ado.net içerisindeki en geli?mi? özelliklere sahip veritabany nesnesi.Bundan önceki makalemde datagrid nesnesine giri? yapmy?tyk. ?imdi ayny datagrid nesnesine ek özellikler ekleyerek fonksiyonunu geni?letelim.



Di?er makalemdeki örne?e ek <asp:datagrid> etiketine OnSortCommand olayyny ekliyoruz.Bu bizim datagrid’i olu?turdu?umuzda  hangi sütuna göre syrala yapaca?ymyzy seçmemize olanak veriyor.Ayryca  <asp:BoundColumn> etiketine ise SortExpression="ad"

ekliyoruz ; bu syralama esnasynda veritabanyndaki hangi sütunu seçmesi gerekti?ini belirtiyor.





<%@ Page Language="vb" AutoEventWireup="false" src="data.aspx.vb" Inherits="data" %>

<HTML>

<HEAD>

<title>DataGrid 2</title>



</HEAD>

<body >

<form id="Form1" method="post" runat="server">

<ASP:DataGrid

    id="listtalepler"

    runat="server"

    Width="90%"

    BorderColor="black"

    CellPadding=3

    CellSpacing="0"

    Font-Name="Trebuchet MS"

    Font-Size="10pt"

    ForeColor="Black"

    BackColor="Beige"

    HeaderStyle-BackColor="Burlywood"

    HeaderStyle-Font-Bold="True"

    AutoGenerateColumns="False"

     OnSortCommand="Sort_Grid">

            <Columns>

       

           <asp:BoundColumn

            HeaderText="AD"

            DataField="ad"

SortExpression="ad"

           

        />

        <asp:BoundColumn

            HeaderText="Soyad"

            DataField="soyad"

SortExpression="soyad"



        />

       

              

    </Columns> 

</asp:DataGrid>

</form>

</body>

</HTML>



-----data.aspx.vb-------



Imports System.Data

Imports System.Data.OleDb







Public Class data

    Inherits System.Web.UI.Page

    Protected WithEvents listtalepler As System.Web.UI.WebControls.DataGrid





#Region " Web Form Designer Generated Code "



    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()



    End Sub



    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        'CODEGEN: This method call is required by the Web Form Designer

        'Do not modify it using the code editor.

        InitializeComponent()

    End Sub



#End Region



    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



        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("db/veritabanim.mdb;"))

            DBCommand = New OleDbDataAdapter _

                ("Select * From tbltalep " _

                & " order by ad" , DBConn)

            DBCommand.Fill(DSPageData, _

                "talepler")

            listtalepler.DataSource = _

                DSPageData.Tables("talepler").DefaultView

            listtalepler.DataBind()

        End If

    End Sub



‘ Sort_Grid olayyny tanymlayarak syralamama yapmamyza olanak veriyoruz.



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("db/veritabanim;"))

        DBCommand = New OleDbDataAdapter _

            ("Select * From tbltalep " _

            & "Order By " _

           ‘ e.sortExpression.ToString() olayy tyklady?ymyz sütunu syralyyor 

& E.SortExpression.ToString(), DBConn)

        DBCommand.Fill(DSPageData, _

            "talepler")

                    listtalepler.DataSource = _

                DSPageData.Tables("talepler").DefaultView

            listtalepler.DataBind()



End Sub



End Class
 
 
 
Bu web sitesi ücretsiz olarak Bedava-Sitem.com ile oluşturulmuştur. Siz de kendi web sitenizi kurmak ister misiniz?
Ücretsiz kaydol