DataGrid RRS feed

  • Question

  • User237232814 posted

    Hi everyone having some diffculty with the datagrid control

    I have the following datagrid

    <asp:DataGrid ID="DataGrid1" runat="server" PageSize="8" AllowPaging="True" 
            DataKeyField="CI_Paciente" AutoGenerateColumns="False" CellPadding="4" AllowSorting="true" OnSortCommand="SortResults" >
            <HeaderStyle Font-Bold="true" BackColor="#CCCCCC"/>
            <AlternatingItemStyle BackColor="#dedede"/>
                <asp:TemplateColumn HeaderText="">
                        <asp:Image ID="Image2" runat="server" ImageUrl="~/images/comunes/del.gif" />
                <asp:BoundColumn SortExpression="CI_Paciente" HeaderText="Cedula" DataField="CI_Paciente" ItemStyle-Width="100px"></asp:BoundColumn>
                <asp:BoundColumn SortExpression="Nombre" HeaderText="Nombre" DataField="Nombre" ItemStyle-Width="120px"></asp:BoundColumn>
                <asp:BoundColumn SortExpression="Apellido" HeaderText="Apellido" DataField="Apellido" ItemStyle-Width="120px"></asp:BoundColumn>
                <asp:BoundColumn SortExpression="Historia" HeaderText="Historia Nro" DataField="Historia" ItemStyle-Width="120px"></asp:BoundColumn>
                <asp:BoundColumn SortExpression="Primera_Visita" HeaderText="Primera Visita" DataField="Primera_Visita" DataFormatString="{0:yyyy-MM-dd}" ItemStyle-Width="100px"></asp:BoundColumn>
                <asp:TemplateColumn HeaderText="">
                        <asp:Image ID="Image1" runat="server" ImageUrl="~/images/comunes/upload.gif" />
                <asp:TemplateColumn HeaderText="">
                        <asp:Image ID="Image2" runat="server" ImageUrl="~/images/comunes/edit.gif" />

    Notice that I have multiple ItemTemplates. On the first itemTemplate I have an ASP image control showing some image. I would like that when a user clicks on that image the row gets deleted. How can I do that without using the asp:buttoncolumn?

    Also the same applies for selecting a row, that instead of deleting the row the row gets selected

    Any help would greatly be appreciated

    thanks in advance


    Thursday, November 4, 2010 10:41 PM


All replies

  • User-2135385890 posted

    You could use an ImageButton instead of the image and set the CommandArgument attribute of the image button to be the ID of the row. For example:

    <asp:ImageButton ID="imgbtnImage1" runat="server"  ImageUrl="" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")>' OnClick="imgbtnImage1_Click"/>

    Then retrieve the CommandArgument in the imgbtnImage1_Click event and delete that row.

    Friday, November 5, 2010 12:26 AM
  • User-1171043462 posted

    Refer here


    i have made use of Linkbutton lnkRemove. You simply replace that with ImageButton

    and in DeleteCustomer method replace

    LinkButton lnkRemove = (LinkButton)sender;


    ImageButton lnkRemove = (ImageButton)sender; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 5, 2010 1:26 AM