473,545 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="m ethod", didn't work either!

Does anyone know how can I make this work?

Thanks in advance!

Apr 3 '07 #1
7 17307
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_ItemDa taBound(object sender, DataGridItemEve ntArgs e)
{
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem )
{
((GridView)e.It em.FindControl( "gvMyGridView") ).DataSource = dt;
((GridView)e.It em.FindControl( "gvMyGridView") ).DataBind();
}
}

public void gvMyGridView_Ro wCommand(object sender, GridViewCommand EventArgs e)
{
// Your delete code for the RowCommand...

}

In the HTML Source:
<asp:DataGrid ID="dgMyGrid" runat="server" AutoGenerateCol umns="false"
OnItemDataBound ="dgMyGrid_Item DataBound" >
<Columns>
<asp:BoundColum n DataField="col1 " HeaderText="Col um 1" />
<asp:TemplateCo lumn HeaderText="Nes ted GridView Column">
<ItemTemplate >
<asp:GridView ID="gvMyGridVie w" runat="server"
OnRowCommand="g vMyGridView_Row Command"
AutoGenerateCol umns="false" >
<Columns>
<asp:BoundFie ld DataField="col2 "
HeaderText="Inn erCol1" />
<asp:BoundFie ld DataField="col3 "
HeaderText="Inn erCol2"/>
<asp:ButtonFiel d ButtonType="but ton"
CommandName="De lete"
Text="delete"
HeaderText="Del ete" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateCol umn>
</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_ItemDa taBound(object sender, DataGridItemEve ntArgs e)
{
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem )
{
((GridView)e.It em.FindControl( "gvMyGridView") ).DataSource = dt;
((GridView)e.It em.FindControl( "gvMyGridView") ).DataBind();
}
}

public void gvMyGridView_Ro wCommand(object sender, GridViewCommand EventArgs e)
{
// Your delete code for the RowCommand...

}

In the HTML Source:
<asp:DataGrid ID="dgMyGrid" runat="server" AutoGenerateCol umns="false"
OnItemDataBound ="dgMyGrid_Item DataBound" >
<Columns>
<asp:BoundColum n DataField="col1 " HeaderText="Col um 1" />
<asp:TemplateCo lumn HeaderText="Nes ted GridView Column">
<ItemTemplate >
<asp:GridView ID="gvMyGridVie w" runat="server"
OnRowCommand="g vMyGridView_Row Command"
AutoGenerateCol umns="false" >
<Columns>
<asp:BoundFie ld DataField="col2 "
HeaderText="Inn erCol1" />
<asp:BoundFie ld DataField="col3 "
HeaderText="Inn erCol2"/>
<asp:ButtonFiel d ButtonType="but ton"
CommandName="De lete"
Text="delete"
HeaderText="Del ete" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateCol umn>
</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.Da taSource = dt;
dlMyDataList.Da taBind();
}

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_It emDataBound">
<ItemTemplate >
<asp:GridView ID="gvMyGridVie w_1" runat="server"
OnRowCommand="g vMyGridView_1_R owCommand"
AutoGenerateCol umns="false" >
<Columns>
<asp:BoundFie ld DataField="col2 " HeaderText="Inn erCol1" />
<asp:BoundFie ld DataField="col3 " HeaderText="Inn erCol2"/>
<asp:ButtonFiel d ButtonType="but ton"
CommandName="te st" Text="delete"
HeaderText="Del ete" />
</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(objec t 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.Data Source = dt;
dlDataList.Data Bind();
}

}

protected void dlDataList_Item DataBound(objec t sender, DataListItemEve ntArgs
e)
{
if (e.Item.ItemTyp e == ListItemType.Al ternatingItem ||
e.Item.ItemType == ListItemType.It em)
{
((GridView)e.It em.FindControl( "gvMyGridView_1 ")).DataSou rce
= dt;
((GridView)e.It em.FindControl( "gvMyGridView_1 ")).DataBin d();
}
}

public void gvMyGridView_1_ RowCommand(obje ct sender,
GridViewCommand EventArgs 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
10229
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when appropriate based on your method names . the GridView has OnRowCommand="GridView1_RowCommand" in the aspx. my problem is that the RowCommand event...
2
2176
by: luisxx | last post by:
WinSrv2003 SP1 IIS 6.0 ASP.NET C#, 2.0 SQL 2005 All on same machine Hello everyone! I have a method in ASP.NET 2.0 that I can't seem to get right, Gridview1_rowcommand.
0
1952
by: Mike P | last post by:
When you specify updateparameters or deleteparameters for your gridview, I was under the impression that whether or not you write some code in the rowcommand event to deal with this, the update or delete happens anyway. For example, I have a gridview like this : <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$...
4
6713
by: Chris Davoli | last post by:
I've got two questions on how to do things in the new GridView. I'm used to the DataGrid in ASP 1.1, so I need the equavalent in ASP 2.0 Gridview. 1.) What is the equavalent for Item Command event in GridView? I thought it would be RowCommand, but I can't get that event to fire with a check box field that I drag/drop in a template column in...
6
28110
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the RowDataBound event to set the commandargument of the delete button. The event is being fired and works fine. When I press the delete button, the...
3
11948
by: Gabriel | last post by:
Hello, Is there a way to add a Textbox to a GridView ? I'd like one TextBox by record. Thanks,
1
4674
by: HockeyFan | last post by:
protected void gvAuthorizedSigners_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "EmptyInsert") { odsAuthorizedSigners.Insert(); return; } if (e.CommandName == "Insert")
0
3158
by: jrnail23 | last post by:
I have a user control which contains an UpdatePanel, which contains a MultiView inside, with a GridView in one of the views. In my GridView, I have a ButtonField which is supposed to trigger a server-side operation, using the RowCommand functionality. In IE, this all works great, but in Firefox (v2.0.0.6), clicking the above mentioned...
3
7532
by: =?Utf-8?B?SlA=?= | last post by:
<asp:GridView ID="gridResults" runat="server" AutoGenerateColumns="False" Width="98%" PageSize="25" AllowPaging="True" OnSorting="gridResults_Sorting" OnPageIndexChanging ="gridResults_PageIndexChange" AllowSorting="True" OnRowDataBound="gridResults_RowDataBound" EnableViewState="False" OnRowCommand="gridResults_Command" ></asp:GridView> ...
0
7473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7660
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7761
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5337
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3457
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.