Tuesday, 15 January 2013 16:24

how to show Gridview items details using DetailsView

Written by 
Rate this item
(0 votes)

GridView is the most common data representation
contol in asp.net.It's needed to show users the
items details when the item is selected.DetailsView
control can be used precisely to show the Gridview items
details.

following codes show how to use DetailsView control with GridView
control to
show details -

in the aspx page -

<asp:GridView ID="studentGridView" runat="server" AutoGenerateColumns="False"
AutoGenerateSelectButton="True" DataKeyNames="Id"
onselectedindexchanged="studentGridView_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Student Id" />
<asp:BoundField DataField="Name" HeaderText="Student Name" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:DetailsView ID="studentDetailsView" runat="server"
AutoGenerateRows="False" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" />
</Fields>
</asp:DetailsView>

use code behind file to populate GridView from database,
then include the following codes to show details for each
item using detailsview -

protected void studentGridView_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt16(studentGridView.SelectedValue);
//get details on id from database
students = studentGateway.GetStudent(id);
studentDetailsView.DataSource = students;
studentDetailsView.DataBind();

Read 1656 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments