search/filter the records for the entered value

Below Code will help u to select a GridView Row in Run-Time and also search/filter the record according to the enter value in a textbox..kindly refer the image below..
VB.NET 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
DataGridView1.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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub HilightCell()
For i As Integer = 0 To DataGridView1.Rows.Count - 2
If DataGridView1.Rows(i).Cells(1).Value.ToString.StartsWith(txtUserName.Text.Trim) Then
'dgvDisplay.Rows(i).DefaultCellStyle.BackColor = Color.Tan
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Cyan
Else
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.White
End If
Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If txtUserName.Text = "" Then
MessageBox.Show("Please Enter a Value to Search", "Santosh Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
HilightCell()
End If
End Sub
C#.NET CODE
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)
{
DataGridView1.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 HilightCell()
{
for (int i = 0; i <= DataGridView1.Rows.Count - 2; i++) { if (DataGridView1.Rows[i].Cells[1].Value.ToString().StartsWith(txtUserName.Text.Trim())) { //dgvDisplay.Rows(i).DefaultCellStyle.BackColor = Color.Tan DataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Cyan; } else { DataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White; } } } private void Button2_Click(object sender, System.EventArgs e) { if (txtUserName.Text == "") { MessageBox.Show("Please Enter a Value to Search", "Santosh Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { HilightCell(); } } OUTPUT:
How Was The Coding
Regards
Santosh

No comments:

Post a Comment