Hi,
We are working on a gridview inside the gridview (parent-child) scenario.
The data of child grid will depend on the data of parent.
Objectives:
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
grid.
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.
Questions
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
Field?
Any help will be greatly appreciated.
Thanks,
Innova..
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='<%#
GetImageVaisibility(DataBinder.Eval(Container,
"DataItem.StockID").ToString())%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<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='<%#
getColour(DataBinder.Eval(Container,
"DataItem.ChangePercentage").ToString(),DataBinder .Eval(Container,
"DataItem.Type").ToString() ) %>' Text='<%# DataBinder.Eval(Container,
"DataItem.ChangePercentage")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
================================================== ======
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,
st.StockID);
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";
gv.Columns.Add(bs);
bs=new BoundField ();
bs.ItemStyle.Width = Unit.Percentage(27);
bs.DataField = "stock";
gv.Columns.Add(bs);
bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(22);
bs.DataField = "CurrentValue";
gv.Columns.Add(bs);
bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(22);
bs.DataField = "PreviousValue";
gv.Columns.Add(bs);
bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(20);
bs.DataField = "change";
gv.Columns.Add(bs);
bs = new BoundField();
bs.ItemStyle.Width = Unit.Percentage(15);
bs.DataField = "ChangePercentage";
gv.Columns.Add(bs);
gv.DataBind();
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);
cell.Controls.Add(gv);
row.Cells.Add(cell);
row.Visible = false;
table.Rows.Add(row);
}
}
}
}
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" ;
}