I am trying to make something like this:
But give me error in this line from cs file when i click Show details in the GridView:
gvRow.FindControl("GridView2").Visible = true;
I am trying when i click Show details to see the Names from other table where ID_Proba is = to ID from first Table.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField
DataField="id"
HeaderText="id"
ReadOnly="True"
SortExpression="id" InsertVisible="False" />
<asp:BoundField
DataField="name"
HeaderText="name"
SortExpression="name" />
<asp:CommandField ShowSelectButton="True"
SelectText="Show Details"/>
</Columns>
<EmptyDataTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
<Columns>
<asp:BoundField DataField="id"
HeaderText="id"
SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="id_proba"
HeaderText="id_proba"
SortExpression="id_proba" />
<asp:BoundField DataField="name"
HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = @id_proba)">
<SelectParameters>
<asp:Parameter Name="id_proba" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
This is the cs file:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
gvRow.FindControl("GridView2").Visible = true;
}
SqlDataSource2.SelectParameters[0].DefaultValue = GridView1.SelectedDataKey[0].ToString();
GridView1.SelectedRow.FindControl("GridView2").Visible = true;
}
You need to take “GridView2” out of the
EmptyDataTemplateand put it in aTemplateField. When you have data, theEmptyDataTemplateis not created at all, and also there is no grid in each row, because this template is for the whole grid.