GridView Example in C#

In this GridView tutorial, you will learn how to implement gridview in page step by step. GridView Tutorial 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 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

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


    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 GridView control

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

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

<asp:TemplateField HeaderText="Action Button">
<asp:Button runat="server" Text="Show More" CommandName="showMore" CommandArgument='<%#Eval("FullName")%>' />
</Columns> 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

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 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 Remote Developer GridView C# Example Interview Questions Answers MVC interview Core question GridView Events Example
Asp.Net C# Examples | Join Asp.Net Course | Learn MVC | Core Tutorial