Asp.net GridView Example in C#

GridView is another data collection control, just like repeater, but this control has different set of in-built events.

In this asp.net GridView tutorial, you will learn how to implement gridview in asp.net page step by step.

Asp.net GridView Tutorial

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 Control in Aspx file

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

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

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

  • 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 event in Asp.net example

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;
}
 
Hire Asp.Net Developer
Asp.net GridView C# Example
Asp.net Interview Questions Answers
Asp.net MVC interview
Asp.net Core question
Asp.net application development tutorials with real-time examples create asp.net web application with SQL database step-by-step.

Asp.net GridView Events Example
Asp.Net C# Examples | Join Asp.Net Course | Learn Asp.net MVC | Asp.net Core Tutorial