Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Working with detailsview without using datasource controls - Part 39

Suggested Videos 
Part 36 - Drilldown and display hierarchical data in gridview without using datasource controls
Part 37 - Detailsview in
Part 38 - Using objectdatasource control with detailsview

In Part 37 of gridview tutorial, we discussed about using sqldatasource control with detailsview, and in Part 38, we discussed about using objectdatasource control. In this video, we will discuss about working with detailsview without using any datasource control.

In a gridview control, I just want to show 3 columns (Id, FirstName and City) from tblEmployee table. As soon as I select a row, in the gridview control, then all the columns of the selected row, should be displayed in details view control as shown below.
details view in

We will be using tblEmployee table for this demo. If you need sql script to create this table, please refer to Part 37 by clicking here. We will be using EmployeeDataAccessLayer, that we coded in Part 38. If you need EmployeeDataAccessLayer code, please refer to Part 38, by clicking here.

Drag and drop a gridview control and a detailsview control on webform1.aspx.
1. Configure GridView1, to include 1 TemplateField and 3 BoundFields.
2. The TemplateField should contain an ItemTemplate, which includes a linkbutton, that the user can click.
3. Set the CommandName property of the LinkButton to SelectFullDetails.
4. Configure the rest of the BoundFields to get data from Id, FirstName and City properties.
5. Set AutoGenerateColumns property of GridView1 to false.
6. Finally generate RowCommand event handler method for GridView1.
7. Set DataKeyNames property of GridView1

At this point the HTML on WebForm1.aspx should be as shown below
<asp:GridView ID="GridView1" runat="server" 
                <asp:LinkButton ID="LinkButton1" 
        <asp:BoundField DataField="Id" 
            HeaderText="Id" />
        <asp:BoundField DataField="FirstName" 
            HeaderText="FirstName" />
        <asp:BoundField DataField="City" 
            HeaderText="City" />
<br />
<asp:DetailsView ID="DetailsView1" runat="server">

Copy and paste the following code in webform1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        GridView1.DataSource = 

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    if (e.CommandName == "SelectFullDetails")
        int rowIndex = 

        int employeeId = Convert.ToInt32(GridView1.SelectedValue);

        List<Employee> employeeList = new List<Employee>()

        DetailsView1.DataSource = employeeList;

No comments:

Post a Comment

It would be great if you can help share these free resources