How to generate edit, delete, update and select buttons automatically?

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 28976 red flag
Rating: 5 out of 5  
 1 vote(s)

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.


Get hundreds of ASP.NET Tips and Tricks and ASP.NET Online training here.

 

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.


ASPX PAGE

<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="AutoId">

<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

</asp:GridView>

<p><asp:Button runat="server" ID="btnSelect" OnClick="GetSelected" Text="Get Selected" /></p>

To generate Delete, Edit and Select button automatically set AutoGenerateDeleteButton, AutoGenerateEditButton and AutoGenerateSelectButton to “true” respectively.

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. 

CODE BEHIND

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;


protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetData();

}

}


/// <summary>

/// Gets the selected.

/// </summary>

/// <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());

}


/// <summary>

/// Gets the data.

/// </summary>

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

ad.Fill(table);

}

}

}

// specify the data source for the GridView

GridView1.DataSource = table;

// bind the data now

GridView1.DataBind();

}

OUTPUT

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 SelectedRowStyle.


Thanks for reading, hope you liked it.

Keep reading my forth coming articles. To read my series of articles on ASP.NET, click here.
Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)