multiple selection in nested gridview, help please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharonrao123
    New Member
    • Mar 2007
    • 4

    #1

    multiple selection in nested gridview, help please

    hello all,

    I have a parent gridview company and in this one a nested gridview people, Is it possible to allow the user to select one row or multiple rows from the people gridview using a check box and also get the datakey (in my case personid) of the the rows selected. Please point me in the right direction. This is what i have so far but i have problem accessing the child gridview in the button click event

    Cheers, Shilpa.

    <asp:GridView ID="grdCompanyP eople" runat="server" AutoGenerateCol umns="False" DataKeyNames ="CoDetailID,Co ID" OnRowDataBound ="grdCompanyPeo ple_RowDataBoun d" SelectedIndex ="0" OnSelectedIndex Changing ="grdCompanyPeo ple_SelectedInd exChanging" OnRowCreated="g rdCompanyPeople _RowCreated" OnRowCommand ="grdCompanyPeo ple_RowCommand" OnPageIndexChan ging ="grdCompanyPeo ple_PageIndexCh anging" PageSize ="2" AllowPaging="Tr ue" OnSelectedIndex Changed ="grdCompanyPeo ple_SelectedInd exChanged" GridLines="Hori zontal" Width="100%">

    <Columns>

    <asp:TemplateFi eld>



    <ItemTemplate >

    <asp:ImageButto n ID="btnDSelect " runat="server" ImageUrl="../images/ok.png"

    ToolTip="Displa y Deal Details" CommandArgument ='<%# Bind("CoDetailI D") %>' OnClick="btnCSe lect_Click"/>

    </ItemTemplate>

    <ItemStyle VerticalAlign=" Top" />

    </asp:TemplateFie ld>
    <asp:TemplateFi eld HeaderText="Com pany Type">

    <ItemTemplate >

    <table>

    <tr>

    <td></td>

    <td><asp:DropDo wnList ID="ddlCoType" runat="server" CssClass="form" >

    </asp:DropDownLis t>

    <asp:Label ID="DetailID" runat="server" Text='<%# Bind("CoDetailI D") %>' Visible="False" ></asp:Label></td>

    <td valign="top"><a sp:Label ID="Label2" runat="server" Text="Hired By:" Height="1px" Width="50px"></asp:Label></td>

    <td valign="top"><a sp:DropDownList ID="ddlClientLi st" runat="server" AppendDataBound Items="true" CssClass="form" >

    <asp:ListItem Text ="Please Select" Value="0" />

    </asp:DropDownLis t></td>

    </tr>

    </table>



    </ItemTemplate>

    <ItemStyle Width="10px" VerticalAlign=" Top" />

    <HeaderStyle HorizontalAlign ="Center" />

    </asp:TemplateFie ld>

    <asp:TemplateFi eld HeaderText="Com pany Role">

    <ItemTemplate >

    <asp:TextBox ID="tbCoRole" runat="server" CssClass ="form"></asp:TextBox>

    </ItemTemplate>

    <ItemStyle Width="10px" VerticalAlign=" Top" />

    <HeaderStyle HorizontalAlign ="Center" />

    </asp:TemplateFie ld>

    <asp:BoundFie ld DataField="Comp any" HeaderText="Com pany" >

    <ItemStyle Width="80px" VerticalAlign=" Top" />

    <HeaderStyle HorizontalAlign ="Center" />

    </asp:BoundField>

    <asp:BoundFie ld DataField="City " HeaderText="Cit y" >

    <ItemStyle Width="40px" VerticalAlign=" Top" />

    <HeaderStyle HorizontalAlign ="Center" />

    </asp:BoundField>

    <asp:BoundFie ld DataField="Stat e" HeaderText="Sta te" >

    <ItemStyle Width="20px" VerticalAlign=" Top" />

    <HeaderStyle HorizontalAlign ="Center" />

    </asp:BoundField>

    <asp:BoundFie ld DataField="Coun try" HeaderText="Cou ntry" >

    <ItemStyle Width="50px" VerticalAlign=" Top" />

    <HeaderStyle HorizontalAlign ="Center" />

    </asp:BoundField>

    <asp:ButtonFiel d ButtonType ="Image" CommandName="Sh ow" ImageUrl="../images/add.png" >

    <ItemStyle VerticalAlign=" Top" />

    </asp:ButtonField >

    <asp:ButtonFiel d ButtonType ="Image" CommandName="Hi de" ImageUrl="../images/remove.png" >

    <ItemStyle VerticalAlign=" Top" />

    </asp:ButtonField >

    <asp:TemplateFi eld HeaderText="Peo ple List">

    <ItemTemplate > /// Nested people gridview

    <asp:GridView ID="grdPersons " runat="server" AutoGenerateCol umns ="false" DataKeyNames ="PersonID" GridLines="Hori zontal" HeaderStyle-CssClass="head_ 1" Width="160px">

    <Columns>

    <asp:TemplateFi eld>

    <HeaderStyle HorizontalAlign ="left" />

    <HeaderTemplate >

    <asp:CheckBox ID="chkSelectAl l" ToolTip="Click here to select/deselect all rows"

    runat="server" />

    </HeaderTemplate>

    <ItemTemplate >

    <asp:CheckBox ID="chkSelect" runat="server" />

    <asp:ImageButto n ID="btnPersonSe lect" runat="server" ImageUrl="../images/ok.png"

    ToolTip="Displa y People Details" CommandArgument ='<%# Bind("PersonID" ) %>' OnClick="btnPer sonSelect_Click "/>

    </ItemTemplate>

    <ItemStyle Width="5px" />

    </asp:TemplateFie ld>

    <asp:BoundFie ld DataField="Full Name" HeaderText="Nam e" >

    <ItemStyle Width="150px" />

    </asp:BoundField>

    <asp:BoundFie ld DataField="Titl e" HeaderText="Job Title" />

    </Columns>

    </asp:GridView>

    </ItemTemplate>

    <HeaderStyle HorizontalAlign ="Center" />

    <ItemStyle VerticalAlign=" Top" Width="250px" />

    </asp:TemplateFie ld>

    </Columns>

    <FooterStyle CssClass="head_ 1" />

    <HeaderStyle CssClass="head_ 1" />

    </asp:GridView>
    //Binding the nested people grid in parent company gridview rowdatabound event
    protected void grdCompanyPeopl e_RowDataBound( Object sender, GridViewRowEven tArgs e)
    {

    if (e.Row.RowType == DataControlRowT ype.DataRow)
    {

    GridView People = e.Row.FindContr ol("grdPersons" ) as GridView;
    String DetailID = (e.Row.FindCont rol("DetailID") as Label).Text.ToS tring();

    int RecordCount;
    DataSet DS = new DataSet();

    String Cmd = "stored procedure name" + DetailID;
    DS = info.SelectDSQu ery(Cmd);

    RecordCount = DS.Tables.Count ;

    People.Visible = false;if (RecordCount > 0)
    {

    People.DataSour ce = DS.Tables[0];

    People.DataBind ();

    }

    CheckBox chk = (CheckBox)Peopl e.FindControl(" chkSelect");if (chk != null)
    {

    chk.Attributes. Add("onclick", "SelectRow( )");
    }

    foreach (GridViewRow rw in People.Rows)
    {

    rw.Attributes.A dd("onclick", "SelectRow( )");

    rw.Attributes.A dd("title", "Click to toggle the selection of this row");
    }

    }

    }

    //Trying to get the personid of the checked rows but not able to find the nested gridview in this method

    protected void Showselection()
    {

    GridView People = grdCompanyPeopl e.FindControl(" grdPersons") as GridView; // this is returning null valueforeach (GridViewRow rw in People.Rows)
    {

    CheckBox chk = (CheckBox)rw.Fi ndControl("chkS elect");

    if (chk != null)
    {

    if (chk.Checked)
    {

    string PersonID = People.DataKeys[rw.RowIndex]["PersonID"].ToString()+"," ;
    Response.Write( PersonID.ToStri ng());

    }

    }

    }

    }
Working...