By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,812 Members | 1,288 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,812 IT Pros & Developers. It's quick & easy.

Parent Child GridView Issue

P: n/a

We are working on a gridview inside the gridview (parent-child) scenario.
The data of child grid will depend on the data of parent.
1.Add new row in parent grid after each row and add child grid into that row
because the columns in child grid are same as parent grid and we want to
align them with the cols of parent grid.
2. If there is data for child grid we show a plus image in the parent row
(in the row above the child grid row).
3.Clicking on Plus and Minus images controls the visibility of the child

Achieved so far:
1.Adding new row to parent grid and adding child grid to that row at the
time of row binding.
2.Plus image visible or not
3.Showing child grid when clicked on plus image.

1.Plus image is not changing to minus image.
2.How do we hide the child grid when minus image is clicked?
3.How do we access the data/controls of parent grid which bound to the Bound

Any help will be greatly appreciated.

Below is the code which give you some idea how we achiving our objectives.

<asp:GridView ID="gvStock" CellPadding="0" CellSpacing="0" runat="server"
GridLines="Both" CssClass="midgrey" ForeColor="#333333"
AutoGenerateColumns="false" OnRowDataBound="gvStock_RowDataBound" >
<FooterStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />

<Columns >
<asp:TemplateField ItemStyle-Width="1%" >
<ItemTemplate >
<asp:ImageButton id="btPlus"
ImageUrl="~/img/plus.jpg" CausesValidation="false" CommandName="Edit"
CommandArgument='<%#DataBinder.Eval(Container, "DataItem.stockID")%>'
OnCommand="ShowChildGrid" Visible='<%#
"DataItem.StockID").ToString())%>' runat="server" />


<asp:BoundField ItemStyle-Width="30%"
DataField="Stock" HeaderText="Stock" />
<asp:BoundField ItemStyle-Width="22%"
DataField="CurrentValue" HeaderText="Current Value" />
<asp:BoundField ItemStyle-Width="22%"
DataField="PreviousValue" HeaderText="Previous Value" />
<asp:BoundField ItemStyle-Width="20%"
DataField="change" HeaderText="Change" />
<asp:TemplateField HeaderText ="%">
<ItemTemplate >
<asp:Label runat="server" CssClass='<%#
"DataItem.ChangePercentage").ToString(),DataBinder .Eval(Container,
"DataItem.Type").ToString() ) %>' Text='<%# DataBinder.Eval(Container,
================================================== ======
public void gvStock_RowDataBound(object sender, GridViewRowEventArgs e)
DataControlRowState rowstate ;
if (e.Row.RowType == DataControlRowType.DataRow)

Table table = e.Row.Parent as Table;

if (table != null)
{ BoundField bs;
rowstate = e.Row.RowState;
if (rowstate == DataControlRowState.Normal )
clsStock st = e.Row.DataItem as clsStock;
GridViewRow row = new GridViewRow(-1, -1,
DataControlRowType.DataRow, DataControlRowState.Normal);
row.ID = "gv_" + st.StockID.ToString();
TableCell cell = new TableCell();
cell.ColumnSpan = gvStock.Columns.Count;
cell.Width = Unit.Percentage(100);

GridView gv = new GridView();
gv.DataSource = clsStock.GetMyStock_Filter(userSessoin,
gv.GridLines = GridLines.Both;
gv.AutoGenerateColumns = false;
gv.Width = Unit.Percentage(100);
gv.CellPadding = 0;
gv.CellSpacing = 0;
gv.ShowHeader = false;
gv.CssClass = "midgrey";

bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(4);
bs.DataField = "Type";

bs=new BoundField ();
bs.ItemStyle.Width = Unit.Percentage(27);
bs.DataField = "stock";

bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(22);
bs.DataField = "CurrentValue";

bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(22);
bs.DataField = "PreviousValue";

bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(20);
bs.DataField = "change";

bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(15);
bs.DataField = "ChangePercentage";


for (byte i = 0; i<=gv.Rows.Count - 1;i++ )
gv.Rows[i].Cells[5].CssClass =
getColour(gv.Rows[i].Cells[5].Text, gv.Rows[i].Cells[0].Text);

row.Visible = false;



public void ShowChildGrid(object sender, CommandEventArgs e)
string str = "gv_" + e.CommandArgument;
TableRow gv = gvStock.FindControl(str) as TableRow ;
gv.Visible = gv.Visible? false :true ;
ImageButton bt =(ImageButton ) sender ;

bt.ImageUrl = bt.ImageUrl == "~/img/plus.jpg" ? "~/img/minus.jpg" :
"~/img/plus.jpg" ;


Jun 9 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.