.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Dynamic Search in Windows Application

Posted By:Santosh       Posted Date: October 15, 2009    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  
 

Please follow the below code,how it is working to search Dynamically.
it will filter all the data corresponding to the textbox entering characters.
VB Code
 Private Sub frmDynamicSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ds = clsDBManager.FillDataSet("Select * from tbUserMst")
        If Not ds.Tables(0) Is Nothing AndAlso ds.Tables(0).Rows.Count > 0 Then
            dgvDisplay.DataSource = ds.Tables(0)
            _strDynamicSearchColumn = "UserName"
            FindGridTable = ds.Tables(0).Copy
            dtSearchResult = ds.Tables(0).Copy
        Else
            MessageBox.Show("No Records Available", "Santosh Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
 Private Sub txtCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUserName.TextChanged
        Dim dt As DataTable = DynamicSearch()
        If Not dt Is Nothing And dgvDisplay.RowCount > 0 Then
            dgvDisplay.DataSource = dt
        Else
            MessageBox.Show("No Records Available", "Santosh Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
 Private Function DynamicSearch() As DataTable
        Try
            If txtUserName.Text.Trim.Equals(String.Empty) Then
                Return FindGridTable
            End If
            dtSearchResult.Rows.Clear()
            Dim dr() As DataRow = Nothing
            Dim rowSearchResult As DataRow
            For Each row As DataRow In FindGridTable.Rows
                If Not IsDBNull(row(DynamicSearchColumn)) Then
                    If row(DynamicSearchColumn).ToString().Trim.ToUpper.StartsWith(txtUserName.Text.Trim.ToUpper) Then
                        rowSearchResult = dtSearchResult.NewRow()
                        rowSearchResult.ItemArray = row.ItemArray
                        dtSearchResult.Rows.Add(rowSearchResult)
                    End If
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return dtSearchResult
    End Function
C# Code
//TODO: INSTANT C# TODO TASK: Insert the following converted event handler wireups at the end of the 'InitializeComponent' method for forms, 'Page_Init' for web pages, or into a constructor for other classes:
base.Load += frmDynamicSearch_Load;
txtUserName.TextChanged += txtCode_TextChanged;

 private void frmDynamicSearch_Load(object sender, System.EventArgs e)
 {
        ds = clsDBManager.FillDataSet("Select * from tbUserMst");
        if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
        {
            dgvDisplay.DataSource = ds.Tables[0];
            _strDynamicSearchColumn = "UserName";
            FindGridTable = ds.Tables[0].Copy();
            dtSearchResult = ds.Tables[0].Copy();
        }
        else
        {
            MessageBox.Show("No Records Available", "Santosh Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
 private void txtCode_TextChanged(object sender, System.EventArgs e)
 {
        DataTable dt = DynamicSearch();
        if (dt != null & dgvDisplay.RowCount > 0)
        {
            dgvDisplay.DataSource = dt;
        }
        else
        {
            MessageBox.Show("No Records Available", "Santosh Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
 private DataTable DynamicSearch()
 {
        try
        {
            if (txtUserName.Text.Trim().Equals(string.Empty))
            {
                return FindGridTable;
            }
            dtSearchResult.Rows.Clear();
            DataRow[] dr = null;
            DataRow rowSearchResult = null;
            foreach (DataRow row in FindGridTable.Rows)
            {
                if (! (System.Convert.IsDBNull(row[DynamicSearchColumn])))
                {
                    if (row[DynamicSearchColumn].ToString().Trim().ToUpper().StartsWith(txtUserName.Text.Trim().ToUpper()))
                    {
                        rowSearchResult = dtSearchResult.NewRow();
                        rowSearchResult.ItemArray = row.ItemArray;
                        dtSearchResult.Rows.Add(rowSearchResult);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return dtSearchResult;
    }


How was the Coding ??
Any Problem Plz feel free to Contact Santosh.mca08@gmail.com
i will reply u soon

Thanx and Regards
Santosh
More Details Plz visit my blog
http://santoshdotnetarena.blogspot.com/2009/10/dynamic-search-in-windows-application.html



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend