Free Web Development Online Tutorials, Learn to Code
Learn Asp.net Aspx, Free Asp.net Tutorial in C#, Asp.net Web Application Development Guide

Asp.net GridView Example in C#

Asp.net GridView control is used for displaying collection data, we can bind DataSource property and GridView automatically figure out column name, like Repeater we don’t need to specify every column manually, but GridView can be always customized manually as per need.

Here are some features of Asp.net GridView control
  • Built-in paging capabilities.
  • Built-in sorting capabilities.
  • Built-in update and delete capabilities
  • Built-in row selection capabilities.
  • Customizable appearance using themes and styles
  • Multiple key fields
  • Programmatic access to Gridview object model
  • Binding any data source controls, like SqlDataSource.

How to use GridView in Asp.net

Getting DataSet using DataAdapter

Step 1: write a data access method like dataset / dataeader or custom object collection that will get you the data from database.

public DataSet getStudentDataset()
{
    DataSet ds = new DataSet();
    using (SqlConnection con = new SqlConnection(Util.ConnectionString))
    {
    SqlDataAdapter _sda = new SqlDataAdapter("Select * from tbStudent", con);

    _sda.Fill(ds);
    }

    return ds;
}
Add GridView in Aspx file

Step 2: Add a GridView control in your aspx file.

<asp:GridView ID="gvStudent" runat="server"> </asp:GridView>
DataBind in GridView

Step 3: write a method in your aspx.cs codebehind to set the DataSource property of asp.net GridView control

void displayStudents()
{
    DataTable dt = null;
          
    using (StudentDTO dto = new StudentDTO())
    {
        dt = dto.getStudentDataset().Tables[0];
        gvStudent.DataSource = dt;
        gvStudent.DataBind();
    }
}
GridView Load Data
Step 4: Call the method in page load event.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        loadStudentDetails();
}

Now you have loaded your GridView with data, now you learn Events in GridView, to do that you need to add one column for action button.

Add Columns in GridView

Step 5: We can add additional column in gridview, also can control how many columns to be displayed, alternatively we can just bind the data, columns will be automatically added same as dataset column

<Columns>
<asp:TemplateField HeaderText="Action Button">
<ItemTemplate>
<asp:Button runat="server" Text="Show More" CommandName="showMore" CommandArgument='<%#Eval("FullName")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>

Asp.net GridView Events

Now we learn how to handle different events in GridView

Events Description
RowCommand Occurs when any button on row is clicked, provides GridViewCommandEventArgs, which has CommandArgument property to indentify the unique data value of row
Sorting Occurs when you change the order of data displayed
SelectedIndexChanged Occurs when any row selected, to see the selected row set AutoGenerateSelectButton="True", GridView.SelectedRow property will have the value of selected row
RowUpdating Occurs when you update any particular row
RowDeleting Occurs when you delete any particular row
GridView RowCommand event example in Asp.net

RowCommand is very similar to ItemCommand , this event gets executed when user click on any particular row , there can be multiple command button in one row, we can distinguish them based on value of "commandName". Let's look how to implement events in gridview

protected void gvStudent_RowCommand(object sender, GridViewCommandEventArgs e)
{ if (e.CommandName == "showMore") { string _commandArgument= e.CommandArgument as string; // once yo get the CommandArgument value, you can write your further action. // here just to show you the , we are prnting on a label. labMessage.Text = _commandArgument; } }

Add the OnRowCommand="gvStudent_RowCommand" in your GridView
Note: CommandArgument is always a object type, so convert to right data type to retrieve the value

GridView SelectedIndexChanged example in Asp.net

Set AutoGenerateSelectButton="True" in your GridView to get the Select Link

protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
{
    string _selectedRow = gvStudent.SelectedRow.Cells[4].Text;

    labMessage.Text = _selectedRow;
}
Comment
Name
Email
Website
Subscribe
 
Asp.net GridView Example, GridView C# RowCommand, Sorting, Paging


Asp.net GridView Events Example