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 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
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(); } }
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: 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>
Now we learn how to handle different events in GridView
Occurs when any button on row is clicked, provides GridViewCommandEventArgs, which has CommandArgument property to indentify the unique data value of row
Occurs when you change the order of data displayed
Occurs when any row selected, to see the selected row set AutoGenerateSelectButton="True", GridView.SelectedRow property will have the value of selected row
Occurs when you update any particular row
Occurs when you delete any particular row
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
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; }