To generate Edit, Delete, Update and Select buttons automatically in the GridView, we can follow this approach.
GridView control is a powerful data grid control that allows us to display the data in tabular format with sorting and pagination. It also allows us to manipulate the data.
This article is one of the series of articles I am writing for last couple of days on GridView in ASP.NET. To generate Edit, Delete, Update and Select buttons automatically in the GridView, we can follow this approach.
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="AutoId">
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<p><asp:Button runat="server" ID="btnSelect" OnClick="GetSelected" Text="Get Selected" /></p>
To generate Delete, Edit and Select button automatically set
AutoGenerateSelectButton to “
In the above code snippet, notice that we have specified “
SelectRowStyle” attributes that takes effect when
Select link is clicked for a particular row. On click of “
Get Selected” button, “
GetSelected” server side method fires that retrieves the
SelectedDataKey value and writes on the page. Here the
DataKeyNames is set to the AutoId of the data source so we will get AutoId value.
In this article, I have just shown how to select records of the GridView and get the selected records primary key( (in my case AutoId) value. In case you are looking for article to learn how to edit, delete records into the Gridview, click here. Whether you are generating the Edit, Delete or Select button automatically by following this approach or explicitly defining it, both functions in the same way. To explicitly define the Edit, Delete, Select button you need to set the
CommandName property of the LinkButton to Edit, Delete or Select respectively.
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
/// Gets the selected.
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void GetSelected(object sender, EventArgs e)
Response.Write("Selected record:" + GridView1.SelectedDataKey.Value.ToString());
/// Gets the data.
private void GetData()
DataTable table = new DataTable();
// get the connection
using (SqlConnection conn = new SqlConnection(_connStr))
// write the sql statement to execute
string sql = "SELECT AutoId, FirstName, LastName, Age FROM PersonalDetail ORDER By AutoId";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
// fire Fill method to fetch the data and fill into DataTable
// specify the data source for the GridView
GridView1.DataSource = table;
// bind the data now
As you can see in the above picture that when Select link is clicked for a particular row of the Gridview, that gets selected and appears in different background style that was specified in the GridView as
Thanks for reading, hope you liked it.
Keep reading my forth coming articles. To read my series of articles on ASP.NET, click here