how to highlight the search result in datagridview using c#.
-
foreach (GridViewRow row in GridView_pm.Rows) { if (row.Cells["MachineTypeNo"].Value.ToString().Equals(txt_search.Text)) { GridView_pm.Rows[row.Index].DefaultCellStyle.BackColor =("yellow"); } else { GridView_pm.Rows[row.Index].Visible = false; } I don't really sure about it..can you pls help me.
-
foreach (GridViewRow row in GridView_pm.Rows) { if (row.Cells["MachineTypeNo"].Value.ToString().Equals(txt_search.Text)) { GridView_pm.Rows[row.Index].DefaultCellStyle.BackColor =("yellow"); } else { GridView_pm.Rows[row.Index].Visible = false; } I don't really sure about it..can you pls help me.
You should write the code in row data bound event of the gridview. The following code will change the value of the corresponding row to yellow color.
protected void GridView_pm_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (!String.IsNullOrWhiteSpace(txt_search.Text))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var rowView = e.Row.DataItem as DataRowView;
if (String.Compare(rowView["MachineTypeNo"].ToString(), txt_search.Text,true) == 0)
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}
else
{
e.Row.BackColor = System.Drawing.Color.White;
}
}
}
}Thanks & Regards Taleeb (trystwithdotnet.blogspot.com)
-
You should write the code in row data bound event of the gridview. The following code will change the value of the corresponding row to yellow color.
protected void GridView_pm_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (!String.IsNullOrWhiteSpace(txt_search.Text))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var rowView = e.Row.DataItem as DataRowView;
if (String.Compare(rowView["MachineTypeNo"].ToString(), txt_search.Text,true) == 0)
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}
else
{
e.Row.BackColor = System.Drawing.Color.White;
}
}
}
}Thanks & Regards Taleeb (trystwithdotnet.blogspot.com)
Thank you for your sharing..