In order to get CommandName you need to use OnRowCommand event.
Since you are using ShowSelectButton for both the CommandField you will get Select as CommandName for both.
So use ButtonField instead of CommandField.
Check the example.
HTML
<asp:GridView ID="Studentgrid" runat="server" AutoGenerateColumns="False"
OnSelectedIndexChanged="Studentgrid_SelectedIndexChanged" OnRowCommand="Studentgrid_RowCommand">
<Columns>
<asp:ButtonField CommandName="EditRecord" HeaderText="Edit Record" Text="Edit Record" />
<%--<asp:CommandField HeaderText="Edit Record" ShowHeader="True" ShowSelectButton="True" />--%>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<%--<asp:CommandField AccessibleHeaderText="Doc Attached View" HeaderText="Doc Attached"
SelectText="View Document" ShowSelectButton="True" />--%>
<asp:ButtonField CommandName="View" HeaderText="Doc Attached" Text="View Document" />
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country")
});
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");
Studentgrid.DataSource = dt;
Studentgrid.DataBind();
}
}
protected void Studentgrid_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Studentgrid.SelectedIndex;
GridViewRow gvRow = Studentgrid.Rows[index];
string name = Studentgrid.SelectedRow.Cells[1].Text;
string country = Studentgrid.SelectedRow.Cells[2].Text;
string commandName = sender.ToString();
}
protected void Studentgrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRecord")
{
//write your code.
}
if (e.CommandName == "View")
{
//write your code.
}
}
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(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
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")
Studentgrid.DataSource = dt
Studentgrid.DataBind()
End If
End Sub
Protected Sub Studentgrid_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = Studentgrid.SelectedIndex
Dim gvRow As GridViewRow = Studentgrid.Rows(index)
Dim name As String = Studentgrid.SelectedRow.Cells(1).Text
Dim country As String = Studentgrid.SelectedRow.Cells(2).Text
Dim commandName As String = sender.ToString()
End Sub
Protected Sub Studentgrid_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "EditRecord" Then
End If
If e.CommandName = "View" Then
End If
End Sub