Link to home
Start Free TrialLog in
Avatar of gpinfotech
gpinfotechFlag for India

asked on

Grid view data binding

Hi experts,

I'm using asp:BoundField and asp:TemplateField combination to bind the data and edit image to the grid view, grid is loading but row command event not firing
Avatar of abhinayp
abhinayp

Here is a sample of how to use rowcommand.. chk if u r missing anything
****** aspx ********
   <asp:GridView runat="server" ID="gv_financials" AutoGenerateColumns="false">
                <Columns>
                    
                    <asp:BoundField DataField="CreateDate" HeaderText="Date"/>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
 
                    <asp:TemplateField HeaderText="T" ItemStyle-Width="100px">
                        <ItemStyle Width="10px" HorizontalAlign="Center" />
                        <HeaderStyle Width="10px" HorizontalAlign="Center" />
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblType" Font-Bold="true"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemStyle Width="10px" />
                        <ItemTemplate>
                            <table width="100%">
                                <tr>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnView" ImageUrl="~/images/view.png" CommandName="view"                                        CommandArgument='<%# Container.DataItemIndex %>'></asp:ImageButton>
                                    </td>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnCopy" ImageUrl="~/images/copy.png" CommandName="copy"
           CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm_copy();">
                                        </asp:ImageButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
               
            </asp:GridView>
**** vb code behind************
 Protected Sub gv_financials_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_financials.RowCommand
        If e.CommandName = "view" Then
           'ur code abt view
      ElseIf e.CommandName = "copy" Then
           'ur code to copy
        End If
     
    End Sub

Open in new window

Hello:
You need to add an event handler in you gridview for the RowCommand event

 OnRowCommand="MyMethod"

Best regards
****** aspx ********
   <asp:GridView runat="server" ID="gv_financials" AutoGenerateColumns="false"  OnRowCommand="MyMethod">
                <Columns>
                    
                    <asp:BoundField DataField="CreateDate" HeaderText="Date"/>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
 
                    <asp:TemplateField HeaderText="T" ItemStyle-Width="100px">
                        <ItemStyle Width="10px" HorizontalAlign="Center" />
                        <HeaderStyle Width="10px" HorizontalAlign="Center" />
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblType" Font-Bold="true"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemStyle Width="10px" />
                        <ItemTemplate>
                            <table width="100%">
                                <tr>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnView" ImageUrl="~/images/view.png" CommandName="view"                                        CommandArgument='<%# Container.DataItemIndex %>'></asp:ImageButton>
                                    </td>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnCopy" ImageUrl="~/images/copy.png" CommandName="copy"
           CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm_copy();">
                                        </asp:ImageButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
               
            </asp:GridView>
**** vb code behind************
 Protected Sub gv_financials_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_financials.RowCommand
        If e.CommandName = "view" Then
           'ur code abt view
      ElseIf e.CommandName = "copy" Then
           'ur code to copy
        End If
     
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abhinayp
abhinayp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
abhinayp:
Thanks for the tip. I work mostly with c# and sometimes forget the differences. ;-)
Avatar of gpinfotech

ASKER

Hi thank u for ur reply
Avatar of Amandeep Singh Bhullar
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.