Dynamic Search In Windows Application

Santosh4u
Posted by Santosh4u under Windows Forms category on | Views : 2339
Description:
Please follow the below code,how it is working to search Dynamically.
it will filter all the data corresponding to the textbox entering characters.
means if three names are there like "santosh","susanta","paul"
if you type "s" in the textbox,data grid will show two name "santosh" and "susanta".
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

Thanx and Regards
Santosh

Comments or Responses

Login to post response