
<asp:GridView ID="GridView1" DataSourceId="MyDataSource"
DataKeyNames="AccountCode,AccountName" ShowFooter="true" AutoGenerateColumns="false"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
OnRowCommand="GridView1_RowCommand"
runat="server">
<Columns>
<asp:BoundField DataField="AccountCode" HeaderText="Account Code" />
<asp:BoundField DataField="AccountName" HeaderText="Account Name" />
<asp:BoundField DataField="AccountDescription" HeaderText="Description" />
<asp:buttonfield buttontype="button" Text="Details" commandname="Details" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="MyDataSource"
ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT AccountsTable.AccountCode,AccountsTable.AccountName,
AccountsTable.AccountDescription FROM AccountsTable"
UpdateCommand="Update AccountsTable SET AccountName=@AccountName,
AccountDescription=@AccountDescription Where AccountCode=@AccountCode"
DeleteCommand="Delete AccountsTable Where AccountCode=@AccountCode"
runat="server"/>
In the RowCommand event, we can access the values of a cell using DataKeys property of GridView control.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Int32.Parse((string)e.CommandArgument);
string Code = (string)GridView1.DataKeys[index].Values["AccountCode"];
string Name = (string)GridView1.DataKeys[index].Values["AccountName"];
}
Copyright © 2012 - All Rights Reserved - VKInfotek.com