Hi dorsa,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Width="49%"
DataKeyNames="Id" CssClass="Hedear-center" HorizontalAlign="Center" Font-Names="Tahoma"
Font-Size="Smaller" Style="margin-right: 0px" DataSourceID="EditCourseDB" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" HeaderStyle-CssClass="Hedear-center"
ReadOnly="True" Visible="false"></asp:BoundField>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" HeaderStyle-CssClass="Hedear-center">
</asp:BoundField>
<asp:BoundField DataField="Floor" HeaderText="Floor" SortExpression="Floor" HeaderStyle-CssClass="Hedear-center">
</asp:BoundField>
<asp:BoundField DataField="Capacity" HeaderText="Capacity" SortExpression="Capacity"
HeaderStyle-CssClass="Hedear-center"></asp:BoundField>
<asp:BoundField DataField="TypeId" HeaderText="TypeId" SortExpression="TypeId" HeaderStyle-CssClass="Hedear-center">
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" Text="Get Value" runat="server" OnClick="GridView_Button_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<span class="auto-style14"><strong>هیچ واحد درسی تعریف نشده است</strong></span>
</EmptyDataTemplate>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<asp:SqlDataSource ID="EditCourseDB" runat="server" ConnectionString="<%$ ConnectionStrings:KDUIS-v1ConnectionString %>"
SelectCommand="SELECT * FROM [EDU_Room]"></asp:SqlDataSource><br />
Id :<asp:Label ID="lblId" runat="server" /><br />
Title :<asp:Label ID="LblCode" runat="server" />
C#
protected void GridView_Button_Click(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)(sender as Button).NamingContainer;
lblId.Text = GridView2.DataKeys[row.RowIndex].Values[0].ToString();
LblCode.Text = row.Cells[1].Text;
foreach (GridViewRow row1 in GridView2.Rows)
{
if (row1.RowIndex == row.RowIndex)
{
row1.BackColor = System.Drawing.ColorTranslator.FromHtml("#A1DCF2");
}
else
{
row1.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
}
}
}
VB.Net
Protected Sub GridView_Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = CType((TryCast(sender, Button)).NamingContainer, GridViewRow)
lblId.Text = GridView2.DataKeys(row.RowIndex).Values(0).ToString()
LblCode.Text = row.Cells(1).Text
For Each row1 As GridViewRow In GridView2.Rows
If row1.RowIndex = row.RowIndex Then
row1.BackColor = System.Drawing.ColorTranslator.FromHtml("#A1DCF2")
Else
row1.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF")
End If
Next
End Sub
Screenshot
![](https://i.imgur.com/8SN3Tm4.gif)