Dynamic Search in Windows Application

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

Reference: http://santoshdotnetarena.blogspot.com/2009/10/dynamic-search-in-windows-application.html


Attachments

  • Dynamic Search in Windows Application (34010-15721-Dynamic Search.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: