Hi Mehram,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvDetail" runat="server" AllowPaging="false" AllowSorting="false"
CssClass="table table-bordered" AutoGenerateColumns="false" DataKeyNames="VoucherDetailId"
ShowHeader="true" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<ItemStyle Width="20" />
</asp:TemplateField>
<asp:BoundField DataField="AccountTitle" SortExpression="AccountTitle" HeaderText="Account Title">
<ItemStyle Width="350px" />
</asp:BoundField>
<asp:BoundField DataField="Narration" SortExpression="Narration" HeaderText="Narration">
<ItemStyle Width="550px" />
</asp:BoundField>
<asp:BoundField DataField="ChequeNumber" SortExpression="ChequeNumber" HeaderText="Cheque #">
<ItemStyle Width="110" />
</asp:BoundField>
<asp:BoundField DataField="ChequeDate" SortExpression="ChequeDate" HeaderText="Date"
DataFormatString="{0:dd-MMM-yy}">
<ItemStyle Width="110" />
</asp:BoundField>
<asp:BoundField DataField="JobNo" SortExpression="JobNo" HeaderText="Job #">
<ItemStyle Width="110" />
</asp:BoundField>
<asp:BoundField DataField="CurrencyCode" SortExpression="CurrencyCode" HeaderText="Currency">
<ItemStyle Width="110" />
</asp:BoundField>
<asp:BoundField DataField="Debit" SortExpression="Debit" HeaderText="Debit">
<HeaderStyle CssClass="alignright" />
<ItemStyle Width="80px" CssClass="alignright" />
<FooterStyle HorizontalAlign="Right" CssClass="alignrightfooter" />
</asp:BoundField>
<asp:BoundField DataField="Credit" SortExpression="Credit" HeaderText="Credit">
<HeaderStyle CssClass="alignright" />
<ItemStyle Width="80px" CssClass="alignright" />
<FooterStyle HorizontalAlign="Right" CssClass="alignrightfooter" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CssClass="gridbuttonblue" Text="Edit"
CommandArgument='<%#Eval("VoucherDetailId")%>' OnClick="Edit"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="30px" CssClass="aligncenter" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" runat="server" CssClass="gridbuttonred" Text="Delete"
CommandArgument='<%#Eval("VoucherDetailId")%>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="30px" CssClass="aligncenter" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[9] { new DataColumn("ChequeNumber", typeof(int)),
new DataColumn("AccountTitle", typeof(string)),
new DataColumn("Narration",typeof(string)),
new DataColumn("ChequeDate",typeof(DateTime)),
new DataColumn("JobNo",typeof(int)) ,
new DataColumn("Debit",typeof(int)),
new DataColumn("Credit",typeof(int)),
new DataColumn("VoucherDetailId",typeof(int)),
new DataColumn("CurrencyCode",typeof(int))});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvDetail.DataSource = dt;
gvDetail.DataBind();
}
}
protected void Edit(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
GridViewRow row = btn.NamingContainer as GridViewRow;
row.BackColor = System.Drawing.Color.Red;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(8) {New DataColumn("ChequeNumber", GetType(Integer)), New DataColumn("AccountTitle", GetType(String)), New DataColumn("Narration", GetType(String)), New DataColumn("ChequeDate", GetType(DateTime)), New DataColumn("JobNo", GetType(Integer)), New DataColumn("Debit", GetType(Integer)), New DataColumn("Credit", GetType(Integer)), New DataColumn("VoucherDetailId", GetType(Integer)), New DataColumn("CurrencyCode", GetType(Integer))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvDetail.DataSource = dt
gvDetail.DataBind()
End If
End Sub
Protected Sub Edit(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As LinkButton = TryCast(sender, LinkButton)
Dim row As GridViewRow = TryCast(btn.NamingContainer, GridViewRow)
row.BackColor = System.Drawing.Color.Red
End Sub
Screenshot