469,593 Members | 2,099 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

gridview inside datagrid doen't raise RowCommand event

Hi all!

I have a gridview inside a datagrid (nested) for which (gridview) the
rowcommand is not raised in order to delete a row from the grid!

I also tried OnRowCommand="method", didn't work either!

Does anyone know how can I make this work?

Thanks in advance!

Apr 3 '07 #1
7 16858
Hi,

As long as you bind the datasource for the nested - Inner gridview in
the ItemDataBound event and not in the ItemCreated Event of the outer Data
grid, it shd work...the RowCommand event of the inner Gridview will fire.

// dgMyGrid is my outer DataGrid.
// gvMyGridView is the inner GridView.
protected void dgMyGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem )
{
((GridView)e.Item.FindControl("gvMyGridView")).Dat aSource = dt;
((GridView)e.Item.FindControl("gvMyGridView")).Dat aBind();
}
}

public void gvMyGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
// Your delete code for the RowCommand...

}

In the HTML Source:
<asp:DataGrid ID="dgMyGrid" runat="server" AutoGenerateColumns="false"
OnItemDataBound="dgMyGrid_ItemDataBound" >
<Columns>
<asp:BoundColumn DataField="col1" HeaderText="Colum 1" />
<asp:TemplateColumn HeaderText="Nested GridView Column">
<ItemTemplate >
<asp:GridView ID="gvMyGridView" runat="server"
OnRowCommand="gvMyGridView_RowCommand"
AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="col2"
HeaderText="InnerCol1" />
<asp:BoundField DataField="col3"
HeaderText="InnerCol2"/>
<asp:ButtonField ButtonType="button"
CommandName="Delete"
Text="delete"
HeaderText="Delete" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
I guess this shd work...

-Parvathy Padmanabhan

Apr 3 '07 #2
oops... sorry,
it's actually a gridview inside a datalist, sorry for that...
I DO bind the gridview inside the datalist's ItemDataBound event and doesn't
work!!!
Please help me out, I can't figure out what is going wrong here!

"Parvathy Padmanabhan" wrote:
Hi,

As long as you bind the datasource for the nested - Inner gridview in
the ItemDataBound event and not in the ItemCreated Event of the outer Data
grid, it shd work...the RowCommand event of the inner Gridview will fire.

// dgMyGrid is my outer DataGrid.
// gvMyGridView is the inner GridView.
protected void dgMyGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem )
{
((GridView)e.Item.FindControl("gvMyGridView")).Dat aSource = dt;
((GridView)e.Item.FindControl("gvMyGridView")).Dat aBind();
}
}

public void gvMyGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
// Your delete code for the RowCommand...

}

In the HTML Source:
<asp:DataGrid ID="dgMyGrid" runat="server" AutoGenerateColumns="false"
OnItemDataBound="dgMyGrid_ItemDataBound" >
<Columns>
<asp:BoundColumn DataField="col1" HeaderText="Colum 1" />
<asp:TemplateColumn HeaderText="Nested GridView Column">
<ItemTemplate >
<asp:GridView ID="gvMyGridView" runat="server"
OnRowCommand="gvMyGridView_RowCommand"
AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="col2"
HeaderText="InnerCol1" />
<asp:BoundField DataField="col3"
HeaderText="InnerCol2"/>
<asp:ButtonField ButtonType="button"
CommandName="Delete"
Text="delete"
HeaderText="Delete" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
I guess this shd work...

-Parvathy Padmanabhan
Apr 3 '07 #3

Hi,

The RowCommand event of the inner gridview wld fire, provided you bind
your Datalist in the first request and not again in the subsequent post backs.

//dlMyDataList is the datalist am using and dt is my datatable, which will
be the source.

if (! IsPostBack)
{
dlMyDataList.DataSource = dt;
dlMyDataList.DataBind();
}

if you have bound your datalist on the page load and if you are rebinding it
during every post back, the onRowCommand event will not fire because, the
grid view gets created every single time.

I guess that shd resolve your problem....

- Parvathy Padmanabhan

Apr 3 '07 #4
No, I don't!
Well here is my code:

Expand|Select|Wrap|Line Numbers
  1. <asp:datalist id="lstTicketsInfo" cellspacing="0" cellpadding="0"
  2. repeatdirection="Vertical" enableviewstate="false" runat="Server" >
  3. <ItemTemplate>
  4. <asp:gridview id="gvBasket" runat="server"
  5. CssClass="myGrid" GridLines="None" ShowHeader="True"
  6. ShowFooter="True" autogeneratecolumns="False"
  7. OnRowCommand="gvBasket_RowCommand">
  8. <headerstyle horizontalalign="Right"></headerstyle>
  9. <rowstyle HorizontalAlign="Right"/>
  10. <FooterStyle HorizontalAlign="Right"/>
  11. <columns>
  12. <asp:TemplateField HeaderText="Quantity">
  13. <headerstyle horizontalalign="Right"></headerstyle>
  14. <ItemTemplate><input id="txtQuantity" style="text-align: right;"
  15. runat="server" Value='<%# Eval("Quantity") %>' /></ItemTemplate>
  16. <FooterTemplate><input id="txtQuantityTotal" style="text-align:
  17. right;" readonly="readonly" runat="server" /></FooterTemplate>
  18. </asp:TemplateField>
  19.  
  20. <asp:TemplateField HeaderText="Price">
  21. <ItemTemplate><asp:Label ID="lblPrice" runat="server" Text='<%#
  22. Eval("Price") %>'></asp:Label></ItemTemplate>
  23. </asp:TemplateField>
  24.  
  25. <asp:TemplateField HeaderText="Total">
  26. <ItemTemplate><asp:Label ID="lblTotalSingle" runat="server"
  27. Text='<%# Eval("Total") %>'></asp:Label></ItemTemplate>
  28. <FooterTemplate><input id="txtPriceTotal" style="text-align:
  29. right;" readonly="readonly" runat="server" /></FooterTemplate>
  30. </asp:TemplateField>
  31.  
  32. <asp:TemplateField>
  33. <ItemTemplate><asp:button id="btnDelete" text="" CommandName =
  34. "Delete" CommandArgument='<%# Eval("EventID").ToString() & "~" &
  35. Eval("D13_D05_IDs").ToString() & "|" & Eval("E01_i03_id").ToString() & "~" &
  36. Eval("E01_index").ToString() %>' runat="server" OnCommand="DeleteTicket"
  37. /></ItemTemplate>
  38. </asp:TemplateField>
  39. </columns>
  40. </asp:gridview>
  41. </ItemTemplate>
  42. </asp:datalist>
  43.  
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2.  
  3. Partial Class Basket
  4. Inherits System.Web.UI.Page
  5.  
  6. Private m_QuantitySubTotal As Byte
  7. Private m_PriceSubTotal As Double
  8. Private WithEvents gvBasket As GridView
  9. Private m_ActiveGridCounter As Byte
  10.  
  11. Private m_QuantityTotal As Byte
  12. Private m_PriceTotal As Double
  13.  
  14. Private ReadOnly Property colBasketItems() As helperclasses.BasketItems
  15. Get
  16. If Session("colBasketItems") Is Nothing Then
  17. Return Nothing
  18. Else
  19. Return CType(Session("colBasketItems"), helperclasses.BasketItems)
  20. End If
  21. End Get
  22. End Property
  23.  
  24. Protected Sub Page_Load(ByVal sender As Object, ByVal e As
  25. System.EventArgs) Handles Me.Load
  26.  
  27. Master.BasketControl.Visible = False
  28.  
  29. If Me.IsPostBack Then Exit Sub
  30.  
  31. LoadMasterData()
  32.  
  33. End Sub
  34.  
  35. Private Sub LoadMasterData()
  36.  
  37. If colBasketItems Is Nothing OrElse (Not CBool(colBasketItems.Count)) Then
  38. If Session("EventId") Is Nothing OrElse (Not
  39. IsNumeric(Session("EventId"))) Then Exit Sub
  40. Response.Redirect("~/BookingSteps/EventBookStep1.aspx?id=" &
  41. Session("EventId").ToString)
  42. Exit Sub
  43. End If
  44.  
  45. Dim dsBasketItems As New DataSet
  46. With dsBasketItems
  47. .Tables.Add("BasketItems")
  48. With .Tables(0).Columns
  49. .Add("EventName", GetType(String))
  50. .Add("VenueName", GetType(String))
  51. .Add("EventDate", GetType(String))
  52. .Add("EventID", GetType(Integer))
  53. .Add("D13_D05_IDs", GetType(String))
  54. End With
  55. End With
  56.  
  57. For Each colTickets As helperclasses.Tickets In colBasketItems
  58. Dim dr As DataRow = dsBasketItems.Tables(0).NewRow
  59. With colTickets
  60. dr("EventName") = .EventName
  61. dr("VenueName") = .VenueName
  62. dr("EventDate") = String.Format("{0:dd\/MM\/yyyy - HH:mm}",
  63. ..EventDate)
  64. dr("EventID") = .EventID
  65. dr("D13_D05_IDs") = .D13_D05_IDs
  66. End With
  67. dsBasketItems.Tables(0).Rows.Add(dr)
  68. Next
  69.  
  70. If Not CBool(dsBasketItems.Tables(0).Rows.Count) Then
  71. InfoLabel1.InnerHtml = "Δεν βρ*θηκαν δεδομ*να."
  72. Exit Sub
  73. End If
  74.  
  75. lstTicketsInfo.DataSource = dsBasketItems
  76. lstTicketsInfo.DataBind()
  77.  
  78. txtQuantityTotal.Value = m_QuantityTotal.ToString
  79. txtPriceTotal.Value = String.Format("{0:##,##0.00;(##,##0.00);0}",
  80. m_PriceTotal)
  81.  
  82. End Sub
  83.  
  84. Protected Sub lstTicketsInfo_ItemDataBound(ByVal sender As Object, ByVal e
  85. As System.Web.UI.WebControls.DataListItemEventArgs) Handles
  86. lstTicketsInfo.ItemDataBound
  87.  
  88. gvBasket = CType(e.Item.FindControl("gvBasket"),
  89. System.Web.UI.WebControls.GridView)
  90. LoadDetailData(gvBasket, _
  91. CType(CType(e.Item.FindControl("lblEventID"),
  92. Label).Text, Integer), _
  93. CType(e.Item.FindControl("lblD13_D05_IDs"), Label).Text)
  94.  
  95. End Sub
  96.  
  97. Protected Sub lstTicketsInfo_ItemCreated(ByVal sender As Object, ByVal e
  98. As System.Web.UI.WebControls.DataListItemEventArgs) Handles
  99. lstTicketsInfo.ItemCreated
  100.  
  101. Dim gvInnerBasket As GridView = CType(e.Item.FindControl("gvBasket"),
  102. GridView)
  103. AddHandler gvInnerBasket.RowCommand, AddressOf gvBasket_RowCommand
  104.  
  105. End Sub
  106.  
  107. Private Sub LoadDetailData(ByRef gvBasket As GridView, ByVal nEventId As
  108. Integer, ByVal sD13_D05_IDs As String)
  109.  
  110. Dim dsTickets As New DataSet
  111. With dsTickets
  112. .Tables.Add("Tickets")
  113. With .Tables(0).Columns
  114. .Add("Quantity", GetType(String))
  115. .Add("Price", GetType(String))
  116. .Add("Total", GetType(String))
  117. .Add("EventID", GetType(Integer))
  118. .Add("D13_D05_IDs", GetType(String))
  119. .Add("E01_i03_id", GetType(Integer))
  120. .Add("E01_index", GetType(Integer))
  121. End With
  122. End With
  123.  
  124. For Each ticket As helperclasses.Ticket In
  125. colBasketItems(nEventId.ToString & "~" & sD13_D05_IDs)
  126. Dim dr As DataRow = dsTickets.Tables(0).NewRow()
  127. With ticket
  128. dr("Price") = String.Format("{0:##,##0.00;(##,##0.00);0}", .Price +
  129. ..ServiceCharge)
  130. If .Section = "" Then
  131. dr("Quantity") = .E01_index.ToString
  132. Else
  133. dr("Quantity") = colBasketItems(nEventId.ToString & "~" &
  134. sD13_D05_IDs).Count
  135. End If
  136. dr("Total") = String.Format("{0:##,##0.00;(##,##0.00);0}", (.Price +
  137. ..ServiceCharge) * .E01_index)
  138. dr("EventID") = nEventId.ToString
  139. dr("D13_D05_IDs") = sD13_D05_IDs
  140. dr("E01_i03_id") = .E01_i03_id
  141. dr("E01_index") = .E01_index
  142. End With
  143. dsTickets.Tables(0).Rows.Add(dr)
  144. Next
  145.  
  146. If Not CBool(dsTickets.Tables(0).Rows.Count) Then
  147. Exit Sub
  148. End If
  149.  
  150. m_QuantitySubTotal = 0
  151. m_PriceSubTotal = 0
  152.  
  153. gvBasket.DataSource = dsTickets
  154. gvBasket.DataBind()
  155.  
  156. m_QuantityTotal += m_QuantitySubTotal
  157. m_PriceTotal += m_PriceSubTotal
  158.  
  159. End Sub
  160.  
  161. Protected Sub gvBasket_RowDataBound(ByVal sender As Object, ByVal e As
  162. System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBasket.RowDataBound
  163.  
  164. If e.Row.RowType = DataControlRowType.DataRow Then
  165. m_QuantitySubTotal +=
  166. CType(Val(CType(e.Row().FindControl("txtQuantity"), HtmlInputText).Value),
  167. Integer)
  168. m_PriceSubTotal +=
  169. CType(Val(CType(e.Row().FindControl("lblTotalSingle"), Label).Text), Double)
  170. ElseIf e.Row.RowType = DataControlRowType.Footer Then
  171. CType(e.Row.FindControl("txtQuantityTotal"), HtmlInputText).Value =
  172. m_QuantitySubTotal.ToString
  173. CType(e.Row.FindControl("txtPriceTotal"), HtmlInputText).Value =
  174. String.Format("{0:##,##0.00;(##,##0.00);0}", m_PriceSubTotal)
  175. End If
  176.  
  177. End Sub
  178.  
  179. Protected Sub gvBasket_RowCommand(ByVal sender As Object, ByVal e As
  180. System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
  181. gvBasket.RowCommand
  182.  
  183. If e.CommandName = "Delete" Then
  184. colBasketItems(Split(e.CommandArgument.ToString,
  185. "|")(0)).Remove(Split(e.CommandArgument.ToString, "|")(1))
  186. LoadMasterData()
  187. End If
  188.  
  189. End Sub
  190.  
  191. Protected Sub DeleteTicket(ByVal sender As Object, ByVal e As
  192. System.Web.UI.WebControls.CommandEventArgs)
  193.  
  194. colBasketItems(Split(e.CommandArgument.ToString,
  195. "|")(0)).Remove(Split(e.CommandArgument.ToString, "|")(1))
  196. LoadMasterData()
  197.  
  198. End Sub
  199. End Class
  200.  
Apr 3 '07 #5

Hi,

You need not have to handle your ItemCreated event for the datalist and
also it is not necessary to create an event handler for the rowCommand of the
grid view.

You cld take a look at my sample code for the same.

Html:

<asp:DataList ID="dlDataList" runat="Server"
OnItemDataBound="dlDataList_ItemDataBound">
<ItemTemplate>
<asp:GridView ID="gvMyGridView_1" runat="server"
OnRowCommand="gvMyGridView_1_RowCommand"
AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="col2" HeaderText="InnerCol1" />
<asp:BoundField DataField="col3" HeaderText="InnerCol2"/>
<asp:ButtonField ButtonType="button"
CommandName="test" Text="delete"
HeaderText="Delete" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:DataList>
Source Code C#:

// I am using the same datatable for both the girdView and the
datalist,
// but that shd not have any effect here..

DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{

dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");

DataRow dr = dt.NewRow();
dr[0] = "A11";
dr[1] = "A12";
dr[2] = "A13";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = "A21";
dr[1] = "A22";
dr[2] = "A23";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = "A31";
dr[1] = "A32";
dr[2] = "A33";
dt.Rows.Add(dr);

if (! IsPostBack)
{
dlDataList.DataSource = dt;
dlDataList.DataBind();
}

}

protected void dlDataList_ItemDataBound(object sender, DataListItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
((GridView)e.Item.FindControl("gvMyGridView_1")).D ataSource
= dt;
((GridView)e.Item.FindControl("gvMyGridView_1")).D ataBind();
}
}

public void gvMyGridView_1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "test")
Response.Write("My rowCommand jus fired");

}
The code above works fine...
Probably jus try removing the itemCreated event of the datalist and re-run
your code.
I guess the rest of it is completely fine.

Let me know how it goes.....

- Parvathy Padmanabhan
Apr 3 '07 #6
thanks for your herp Parvathy Padmanabhan!

The ItemCreated event is just for testing in order to make that event work!

I'll try tomorrow and I'll let you know!
Apr 3 '07 #7
found that!

it was not working because I had enableviewstate="false" on the datalist

Apr 4 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by luisxx | last post: by
4 posts views Thread by Chris Davoli | last post: by
6 posts views Thread by Kevin Attard | last post: by
3 posts views Thread by Gabriel | last post: by
3 posts views Thread by =?Utf-8?B?SlA=?= | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.