Here's code for a page that fills a datagrid and lets you search for
either first or last name and lets you page. Html follows...

Imports System.Data.SqlClient
Public Class SearchDataGrid
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtFirst As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtLast As System.Web.UI.WebControls.TextBox
    Protected WithEvents dg As System.Web.UI.WebControls.DataGrid
    Protected WithEvents btn As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the
Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    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
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            BindGrid()
        End If
    End Sub

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn.Click
        dg.CurrentPageIndex = 0
        BindGrid()
    End Sub

    Private Sub BindGrid()
        dg.DataSource = GetCustomer()
        dg.DataBind()
    End Sub

    Private Function GetCustomer() As DataTable
        Dim SQL As String
        Dim WHERE As String = ""
        SQL = "SELECT * FROM Customers"
        If txtFirst.Text.Length > 0 Then
            WHERE = " WHERE FirstName LIKE '%" & txtFirst.Text & "%'"
        End If
        If txtLast.Text.Length > 0 Then
            If WHERE.Length = 0 Then
                WHERE = " WHERE "
            Else
                WHERE &= " AND "
            End If
            WHERE &= "LastName LIKE '%" & txtLast.Text & "%'"
        End If

        Dim conn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("connString"))
        Dim cmd As SqlCommand = New SqlCommand(SQL & WHERE, conn)
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable
        da.Fill(dt)
        Return dt
    End Function

    Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dg.PageIndexChanged
        dg.CurrentPageIndex = e.NewPageIndex
        BindGrid()
    End Sub
End Class

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="SearchDataGrid.aspx.vb"
Inherits="Playground.SearchDataGrid"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
        <HEAD>
                <title>SearchDataGrid</title>

        </HEAD>
        <body>
                <form id="Form1" method="post" runat="server">
                        <table>
                                <tr>
                                        <td><table>
                                                        <tr>
                                                                <td>First:</td>
                                                                
<td><asp:TextBox ID="txtFirst" Runat="server"></asp:TextBox></td>
                                                                <td>Last:</td>
                                                                
<td><asp:TextBox ID="txtLast" Runat="server"></asp:TextBox></td>
                                                                <td><asp:Button 
ID="btn" Text="Search" Runat="server"></asp:Button></td>
                                                        </tr>
                                                </table>
                                        </td>
                                </tr>
                                <tr>
                                        <td><asp:DataGrid id="dg" 
runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
                                                        PageSize="5">
                                                        <Columns>
                                                                
<asp:BoundColumn DataField="FirstName"
SortExpression="FirstName" ReadOnly="True"
HeaderText="First"></asp:BoundColumn>
                                                                
<asp:BoundColumn DataField="LastName"
SortExpression="LastName" ReadOnly="True"
HeaderText="Last"></asp:BoundColumn>
                                                                
<asp:BoundColumn DataField="EmailAddress"
SortExpression="EmailAddress" ReadOnly="True"
HeaderText="Email"></asp:BoundColumn>
                                                        </Columns>
                                                        <PagerStyle 
Mode="NumericPages"></PagerStyle>
                                                </asp:DataGrid></td>
                                </tr>
                        </table>
                </form>
        </body>
</HTML>


-- 
Dean Fiala
Very Practical Software, Inc
http://www.vpsw.com


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/A77XvD/vlQLAA/TtwFAA/saFolB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to