Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch event on Child-DataGrid in Parent-Datagrid 1

Status
Not open for further replies.

saturnius

Technical User
May 7, 2002
22
0
0
GB
Hello,
I have two Datagrids on my Page: Company(Parent) and User(Child). I load all users belonging to a company on a child grid. How can I select a particular user?
HTML-Code for Datagrid:
<asp:datagrid id=&quot;dgCompany&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; OnItemCommand=&quot;dgCompanyItem&quot;>
<Columns>
<asp:TemplateColumn HeaderText=&quot;DETAILS&quot;>
<ItemTemplate>
<asp:ImageButton id=&quot;btnUser&quot; CommandName=&quot;show&quot; ImageUrl=&quot;images/add.gif&quot; runat=&quot;server&quot;></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
+++
...and more columns....
+++
<asp:TemplateColumn>
<ItemTemplate>
<tr>
<td colspan=&quot;5&quot;>
<asp:placeHolder id=&quot;plcDG&quot; Runat=&quot;server&quot;>
</asp:placeHolder>
</td>
</tr>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

Codebehind for button click:
Sub dgCompanyItem(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs) Handles dgCompany.ItemCommand
If E.CommandName = &quot;show&quot; Then
Dim tmpdg As New DataGrid()
tmpdg.AutoGenerateColumns = False

Dim colButton As New ButtonColumn()
colButton.ButtonType = ButtonColumnType.PushButton
colButton.HeaderText = &quot;SELECT&quot;
colButton.Text = &quot;Select&quot;
colButton.CommandName = &quot;Select&quot;

Dim colID As New BoundColumn()
colID.DataField = &quot;id&quot;
colID.HeaderText = &quot;ID&quot;

Dim colLogonname As New BoundColumn()
colLogonname.DataField = &quot;logonname&quot;
colLogonname.HeaderText = &quot;LOGONNAME&quot;

Dim colDepartment As New BoundColumn()
colDepartment.DataField = &quot;department&quot;
colDepartment.HeaderText = &quot;DEPARTMENT&quot;

tmpdg.Columns.Add(colButton)
tmpdg.Columns.Add(colID)
tmpdg.Columns.Add(colLogonname)
tmpdg.Columns.Add(colDepartment)

Me.daUser.SelectCommand.CommandType = CommandType.StoredProcedure
Me.daUser.Fill(dsUser, &quot;user&quot;)
tmpdg.DataSource = Me.dsUser.Tables(&quot;user&quot;).DefaultView
tmpdg.DataBind()

E.Item.FindControl(&quot;plcDG&quot;).Controls.Add(tmpdg)
End If
End Sub

***
How can I ascertain wich user was selected
 
Cast sender to a datagrid to implicitly refer to the datagrid that was clicked, and then you can extract the ItemIndex from your command arguments to find which row was clicked:

CType(sender,DataGrid).Item(e.Item.ItemIndex)

So let's say that you have a label in that child grid which holds userID called lblUserID. Then you can pull that userID like so:

dim UserID as int
UserID = int.Parse(CType(CType(sender,DataGrid).Item(e.Item.ItemIndex).FindControl(&quot;lblUserID&quot;),Label).Text)

It's a bit of a long statement, but it breaks down very easily:

CType(sender,DataGrid) : gets your grid -- let's call it dg from here on out.

dim dg as DataGrid = CType(sender,DataGrid)

CType(dg.Item(e.Item.ItemIndex).FindControl(&quot;lblUserID&quot;),Label) : gets your label. Let's now call this dgLabel.

dim dgLabel as Label = CType(dg.Item(e.Item.ItemIndex).FindControl(&quot;lblUserID&quot;),Label)

And then you simply parse its .Text into an integer:

UserID = int.Parse(dgLabel.Text)

Now you can use those separate statements to break out the various pieces one by one so that the code reads a little better, but I prefer the first statement that lumps it all together. Just have to build it in pieces while thinking of the various steps that it takes to get at the value you want.

hth -
paul
penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Hi,
Thank you very much for your help.Unfortunately I could not find out how to catch events on the child grid. I tried with ctype(sender, datagrid) but there is always the parent datagrid. Here is the code i try:
*******
(1)Datagrid &quot;dgCompany&quot; - itemtemplate with imagebutton & commandname &quot;show&quot;
(2)Datagrid &quot;dgCompany&quot; - placeholder column
(3)Datagrid onItemcommand - Create new DG (with user details) and add to Placeholder
(4)If I click the child datagrid with the user details I want to see e.g. the &quot;logonname&quot; - How?
******
Public Sub dgCompanyItem(ByVal Sender As System.Object, ByVal E As DataGridCommandEventArgs) Handles dgCompany.ItemCommand
Dim dg As DataGrid = CType(Sender, DataGrid)
If E.CommandName = &quot;show&quot; Then
Label2.Text = CType(dg.Items(E.Item.ItemIndex).FindControl(&quot;lblUserCompany_hubname_short&quot;), Label).Text
Dim child As New DataGrid()
child.AutoGenerateColumns = False
Dim colButton As New ButtonColumn()
colButton.ButtonType = ButtonColumnType.PushButton
colButton.HeaderText = &quot;SELECT&quot;
colButton.Text = &quot;Select&quot;
colButton.CommandName = &quot;Select&quot;
****
here goes the rest of the code - see above
****
Else
Label2.Text = &quot;I am here!!!&quot;
Label2.Text = CType(dg.Items(E.Item.ItemIndex).FindControl(&quot;logonname&quot;), Label).Text
End If
End Sub
 
Looks like you're going to need to specify an event handler for your child datagrid.

W/ VB, the method is 'AddHandler'. Plug it into your help files or Google to get details. I use C#, and I know the method is different enough that my code would not help you.

Once you specify the handler, then it should fire whenever your user clicks the child grid.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top