Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

GridView Example in Asp.net Application

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

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

Step 2: Add a GridView control in your aspx file.
<asp:GridView ID="gvStudent" runat="server" ></asp: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();
}
}

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.

Step 5: Add Columns in GridView
<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

Here are some important GridView Events we learn
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

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



 
GridView Example in Asp.net Application


Asp.net GridView Events Example

Group Training