HTML SOURCE OF THE PAGE
Now generate SelectedIndexChanged event for parent gridview (GridView1) and write code mentioned below.
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" onselectedindexchanged="GridView1_SelectedIndexChanged"> <RowStyle VerticalAlign="Top" /> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> <asp:CommandField ShowSelectButton="True" SelectText="Show Details"/> <asp:TemplateField> <ItemTemplate> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderID" DataSourceID="SqlDataSource2" Visible="false"> <Columns> <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID" /> <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" /> </Columns> </asp:GridView> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]"> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [OrderID], [OrderDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)"> <SelectParameters> <asp:Parameter Name="CustomerID" Type="String"/> </SelectParameters> </asp:SqlDataSource> </div> </form>
Now generate SelectedIndexChanged event for parent gridview (GridView1) and write code mentioned below.
C# CODE BEHIND
1
protected
void
GridView1_SelectedIndexChanged(
object
sender, EventArgs e)
2
{
3
foreach
(GridViewRow gvRow
in
GridView1.Rows)
4
{
5
gvRow.FindControl(
"GridView2"
).Visible =
false
;
6
}
7
SqlDataSource2.SelectParameters[0].DefaultValue = GridView1.SelectedDataKey[0].ToString();
8
GridView1.SelectedRow.FindControl(
"GridView2"
).Visible =
true
;
9
}
VB.NET CODE BEHIND
1
Protected
Sub
GridView1_SelectedIndexChanged(sender
As
Object
, e
As
EventArgs)
2
For
Each
gvRow
As
GridViewRow
In
GridView1.Rows
3
gvRow.FindControl(
"GridView2"
).Visible =
False
4
Next
5
SqlDataSource2.SelectParameters(0).DefaultValue = GridView1.SelectedDataKey(0).ToString()
6
GridView1.SelectedRow.FindControl(
"GridView2"
).Visible =
True
7
End
Sub
0 comments:
Post a Comment