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

multiple selection in nested gridview, help please

P: 4
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="grdCompanyPeople" runat="server" AutoGenerateColumns="False" DataKeyNames ="CoDetailID,CoID" OnRowDataBound ="grdCompanyPeople_RowDataBound" SelectedIndex ="0" OnSelectedIndexChanging ="grdCompanyPeople_SelectedIndexChanging" OnRowCreated="grdCompanyPeople_RowCreated" OnRowCommand ="grdCompanyPeople_RowCommand" OnPageIndexChanging ="grdCompanyPeople_PageIndexChanging" PageSize ="2" AllowPaging="True" OnSelectedIndexChanged ="grdCompanyPeople_SelectedIndexChanged" GridLines="Horizontal" Width="100%">

<Columns>

<asp:TemplateField>



<ItemTemplate>

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

ToolTip="Display Deal Details" CommandArgument='<%# Bind("CoDetailID") %>' OnClick="btnCSelect_Click"/>

</ItemTemplate>

<ItemStyle VerticalAlign="Top" />

</asp:TemplateField>
<asp:TemplateField HeaderText="Company Type">

<ItemTemplate>

<table>

<tr>

<td></td>

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

</asp:DropDownList>

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

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

<td valign="top"><asp:DropDownList ID="ddlClientList" runat="server" AppendDataBoundItems="true" CssClass="form">

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

</asp:DropDownList></td>

</tr>

</table>



</ItemTemplate>

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

<HeaderStyle HorizontalAlign="Center" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Company Role">

<ItemTemplate>

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

</ItemTemplate>

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

<HeaderStyle HorizontalAlign="Center" />

</asp:TemplateField>

<asp:BoundField DataField="Company" HeaderText="Company" >

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

<HeaderStyle HorizontalAlign="Center" />

</asp:BoundField>

<asp:BoundField DataField="City" HeaderText="City" >

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

<HeaderStyle HorizontalAlign="Center" />

</asp:BoundField>

<asp:BoundField DataField="State" HeaderText="State" >

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

<HeaderStyle HorizontalAlign="Center" />

</asp:BoundField>

<asp:BoundField DataField="Country" HeaderText="Country" >

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

<HeaderStyle HorizontalAlign="Center" />

</asp:BoundField>

<asp:ButtonField ButtonType ="Image" CommandName="Show" ImageUrl="../images/add.png" >

<ItemStyle VerticalAlign="Top" />

</asp:ButtonField>

<asp:ButtonField ButtonType ="Image" CommandName="Hide" ImageUrl="../images/remove.png" >

<ItemStyle VerticalAlign="Top" />

</asp:ButtonField>

<asp:TemplateField HeaderText="People List">

<ItemTemplate> /// Nested people gridview

<asp:GridView ID="grdPersons" runat="server" AutoGenerateColumns ="false" DataKeyNames ="PersonID" GridLines="Horizontal" HeaderStyle-CssClass="head_1" Width="160px">

<Columns>

<asp:TemplateField>

<HeaderStyle HorizontalAlign="left" />

<HeaderTemplate>

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

runat="server" />

</HeaderTemplate>

<ItemTemplate>

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

<asp:ImageButton ID="btnPersonSelect" runat="server" ImageUrl="../images/ok.png"

ToolTip="Display People Details" CommandArgument='<%# Bind("PersonID") %>' OnClick="btnPersonSelect_Click"/>

</ItemTemplate>

<ItemStyle Width="5px" />

</asp:TemplateField>

<asp:BoundField DataField="FullName" HeaderText="Name" >

<ItemStyle Width="150px" />

</asp:BoundField>

<asp:BoundField DataField="Title" HeaderText="Job Title" />

</Columns>

</asp:GridView>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Center" />

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

</asp:TemplateField>

</Columns>

<FooterStyle CssClass="head_1" />

<HeaderStyle CssClass="head_1" />

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

if (e.Row.RowType == DataControlRowType.DataRow)
{

GridView People = e.Row.FindControl("grdPersons") as GridView;
String DetailID = (e.Row.FindControl("DetailID") as Label).Text.ToString();

int RecordCount;
DataSet DS = new DataSet();

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

RecordCount = DS.Tables.Count;

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

People.DataSource = DS.Tables[0];

People.DataBind();

}

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

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

foreach (GridViewRow rw in People.Rows)
{

rw.Attributes.Add("onclick", "SelectRow()");

rw.Attributes.Add("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 = grdCompanyPeople.FindControl("grdPersons") as GridView; // this is returning null valueforeach (GridViewRow rw in People.Rows)
{

CheckBox chk = (CheckBox)rw.FindControl("chkSelect");

if (chk != null)
{

if (chk.Checked)
{

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

}

}

}

}
Aug 13 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.