Hi asmaa
you can do it like this
HTML
<div>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" >
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="comp_name" HeaderText="comp_name" ItemStyle-Width="100" >
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" >
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
</Columns>
<HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>
</asp:GridView>
<br />
<br />
<asp:TextBox ID="txtsearch" runat="server" placeholder="Search"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnsearch" runat="server" Text="Search"
onclick="btnsearch_Click" />
</div>
Namespaces
Using System.Data;
Using System.Drawing;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("comp_name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "TCS", "India");
dt.Rows.Add(2, "McDonald", "USA");
dt.Rows.Add(3, "Alibaba", "China");
dt.Rows.Add(4, "Pizza Ht", "USA");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnsearch_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (txtsearch.Text == GridView1.Rows[i].Cells[2].Text)
{
SelectRow(GridView1.Rows[i].RowIndex);
}
}
}
private void SelectRow(int rowindex)
{
GridView1.Rows[rowindex].BackColor = Color.Gray;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowIndex != rowindex)
{
GridView1.Rows[i].BackColor = Color.Empty;
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.SelectedRow.BackColor = Color.Gray;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowIndex != GridView1.SelectedRow.RowIndex)
{
GridView1.Rows[i].BackColor = Color.Empty;
}
}
}