Hi phonghue,
Check this sample. now take its reference and correct your code.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowCommand="OnRowCommand">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" Text="View Agency" runat="server" CommandName="EditRow"
CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</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[] { new DataColumn("Id"), new DataColumn("Name") });
dt.Rows.Add("1", "Amit");
dt.Rows.Add("2", "Smith");
dt.Rows.Add("3", "John");
dt.Rows.Add("4", "Rachel");
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRow")
{
int index = Convert.ToInt32(e.CommandArgument);
}
}
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() {New DataColumn("Id"), New DataColumn("Name")})
dt.Rows.Add("1", "Amit")
dt.Rows.Add("2", "Smith")
dt.Rows.Add("3", "John")
dt.Rows.Add("4", "Rachel")
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End If
End Sub
Protected Sub OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "EditRow" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
End If
End Sub