This is a migrated thread and some comments may be shown as answers.

Delete grid rows

2 Answers 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 04 Apr 2011, 06:57 PM
Hello Friends,

I have trouble on my project, and at moment I didn't get to solve it !! I have a grid and when I delete row for first time it is ok, the problem is if I delete the other line in sequence. For some reason, I can not delete any row where the ID is larger than the ID of the deleted first.

I am using telerik ajax Q3 2010 and my project is in C# Framework 4.0 !!

Could you help me with this case ??

See attached my code:

<div id="divListagem">
        <telerik:RadAjaxPanel ID="apListagem" runat="server" LoadingPanelID="lpListagem"
            Width="100%" HorizontalAlign="NotSet"
            meta:resourcekey="apListagemResource1">
            <telerik:RadGrid ID="dgListagem" runat="server" AllowFilteringByColumn="True" AllowPaging="True"
                AllowSorting="True" GridLines="None" Skin="Windows7" EnableLinqExpressions="False"
                OnItemCommand="dgListagem_ItemCommand" OnItemDataBound="dgListagem_ItemDataBound"
                OnNeedDataSource="dgListagem_NeedDataSource" CellPadding="0" Height="100%" HorizontalAlign="Center"
                PageSize="20" meta:resourcekey="dgListagemResource" >
                <SortingSettings SortedAscToolTip="Ordem Crescente" SortedDescToolTip="Ordem Decrescente"
                    SortToolTip="Clique aqui para ordenar" />
                <GroupingSettings CaseSensitive="false" />
                <ClientSettings>
                    <Scrolling UseStaticHeaders="True" />
                </ClientSettings>
                <MasterTableView AutoGenerateColumns="False" CommandItemDisplay="Top" DataKeyNames="ID" ClientDataKeyNames="ID"
                    CellPadding="0" CellSpacing="0">
                    <Columns>
                        <telerik:GridTemplateColumn AutoPostBackOnFilter="True" DataField="ID" FilterControlWidth="60px"
                            GroupByExpression="ID Group By ID" HeaderText="Código" meta:resourcekey="gtCodigoResource"
                            ShowFilterIcon="false" SortExpression="ID" UniqueName="ID">
                            <ItemTemplate>
                                <asp:Literal ID="ltrId" runat="server"></asp:Literal>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" Width="80px" />
                            <ItemStyle HorizontalAlign="Center" Width="80px" />
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn FilterControlWidth="180px" AutoPostBackOnFilter="True" DataField="Titulo" GroupByExpression="Titulo Group By Titulo"
                            HeaderText="Titulo" meta:resourcekey="gtTituloResource" ShowFilterIcon="False" SortExpression="Titulo"
                            UniqueName="Titulo">
                            <ItemTemplate>
                                <asp:Literal ID="ltrTitulo" runat="server"></asp:Literal>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn AutoPostBackOnFilter="True" DataField="Idioma" FilterControlWidth="180px"
                            GroupByExpression="Idioma Group By Idioma" HeaderText="Idioma" meta:resourcekey="gtIdiomaResource"
                            ShowFilterIcon="False" SortExpression="Idioma" UniqueName="Idioma">
                            <ItemTemplate>
                                <asp:Literal ID="ltrIdioma" runat="server"></asp:Literal>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" Width="200px" />
                            <ItemStyle HorizontalAlign="Center" Width="200px" />
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn AllowFiltering="False" UniqueName="TemplateColumn">
                            <ItemTemplate>
                                <asp:HyperLink ID="hplEdit" runat="server" meta:resourcekey="hplEditResource" ImageUrl="/CMS/Estrutura/Image/lista_edit.png"></asp:HyperLink>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" Width="19px" />
                            <ItemStyle HorizontalAlign="Center" Width="19px" />
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn AllowFiltering="False" UniqueName="TemplateColumn1">
                            <ItemTemplate>
                                <asp:ImageButton ID="ibtExcluir" runat="server" CommandName="Excluir" ImageUrl="/CMS/Estrutura/image/lista_delete.png"
                                    meta:resourcekey="ibtExcluirResource" />
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" Width="20px" />
                            <ItemStyle HorizontalAlign="Center" Width="20px" />
                        </telerik:GridTemplateColumn>
                    </Columns>
                    <CommandItemTemplate>
                        <div class="CabecalhoBotaoGrid">
                            <table width="100%">
                                <tr>
                                    <td align="center">
                                          
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </CommandItemTemplate>
                </MasterTableView>
                <HeaderContextMenu EnableImageSprites="True" CssClass="GridContextMenu GridContextMenu_Default">
                </HeaderContextMenu>
            </telerik:RadGrid>
        </telerik:RadAjaxPanel>
        <telerik:RadAjaxLoadingPanel ID="lpListagem" runat="server" Skin="Windows7" meta:resourcekey="lpListagemResource1">
            <div id="loading">
                <asp:Literal ID="ltrLoading" runat="server"
                    meta:resourcekey="ltrLoadingResource1"></asp:Literal>
            </div>
        </telerik:RadAjaxLoadingPanel>
    </div>


protected void dgListagem_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Excluir")
                {
                    GridEditableItem editeditem = (GridEditableItem)e.Item;
                    int codigo = Convert.ToInt32(editeditem.OwnerTableView.DataKeyValues[editeditem.ItemIndex]["ID"].ToString());
 
                    try
                    {
                        BLFaq.Delete(codigo);
                        dgListagem.Rebind();
                    }
                    catch
                    {
                        RadScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showError", string.Format("javascript:alert('{0}')", txtErrorParent.Value), true);
                    }
                }
            }
            catch (Exception ex)
            {
                BAL.BLError.TrataErro(new Model.MLError() { Ex = ex, Page = this.Page });
            }
        }
 
        protected void dgListagem_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            try
            {
                dgListagem.DataSource = BLFaq.SelectListagem();
            }
            catch (Exception ex)
            {
                BAL.BLError.TrataErro(new Model.MLError() { Ex = ex, Page = this.Page });
            }
        }
 
        protected void dgListagem_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var obj = e.Item.DataItem as MLFaq;
            if (obj == null) return;
 
            if (!(e.Item is GridDataItem)) return;
 
            GridDataItem item = (GridDataItem)e.Item;
            Literal control;
 
            //Id
            Literal ltrId = item.FindControl("ltrId") as Literal;
            ltrId.Text = obj.Id.ToString();
 
            //Titulo
            Literal ltrTitulo = item.FindControl("ltrTitulo") as Literal;
            ltrTitulo.Text = obj.Titulo;
 
            ////Texto
            //control = item.FindControl("ltrTexto") as Literal;
            //control.Text = obj.Texto;
 
            //Idioma
            Literal ltrIdioma = item.FindControl("ltrIdioma") as Literal;
            ltrIdioma.Text = obj.Idioma;
 
            //Editar
            var edit = item.FindControl("hplEdit") as HyperLink;
            var qs = new SecureQueryString();
            qs["IdItem"] = obj.Id.ToString();
            qs["indice"] = param.Index;
            qs["Update"] = Update.ToString();
            qs["Publish"] = Publish.ToString();
            qs["ItemMenuId"] = param.ItemMenuId.ToString();
            qs["MenuId"] = param.MenuId.ToString();
            qs["MenuName"] = param.MenuName;
 
            if (!Update)
            {
                edit.ImageUrl = "~/CMS/Imagens/pesquisar.png";
                edit.ToolTip = "Visualizar Cadastro";
            }
            else
            {
                edit.ToolTip = "Editar Cadastro";
            }
            edit.NavigateUrl = string.Format("Cadastro.aspx?IdItem={0}", qs);
 
            //Excluir
            var delete = item.FindControl("ibtExcluir") as ImageButton;
            if (!Delete)
            {
                delete.Enabled = false;
                delete.ImageUrl = "~/CMS/Estrutura/Image/lista_delete_off.png";
                delete.ToolTip = "Sem permissão para Excluir";
            }
            else
            {
                delete.ToolTip = "Excluir Registro" ;
                delete.OnClientClick = string.Format("javascript:return confirm('{0}');", txtMessage.Value);
            }
        }
 
    }
}

2 Answers, 1 is accepted

Sort by
0
Carlos Rodriguez
Top achievements
Rank 1
answered on 05 Apr 2011, 09:15 PM
Si es en C# 4.0 me imagino que estás usando List<T>, lo mas común sería obtener el índice del grid, y en base al índice, eliminas el objeto en el listado y vuelves a recargar el Grid.
=============================================================================
If
it is in C # 4.0 I guess you're using <T> List, the most common would be the index of the grid, and based on the index, delete the object in the list and come back to recharge the grid.

0
Daniel
Top achievements
Rank 1
answered on 07 Apr 2011, 02:28 PM
Hello Carlos, how are you ??

My friend,  I already tried it , but it didn't work yet. I discovered that the problem is on "Ajax", if I remove the RadAjaxPanel from the page, the method works . I think the problem is something on webconfig, because my application is on 4.0. I already worked with telerik on 3.5 and I never had troubles like this. Do you know how I can solve this matter ? sorry for my poor English = )

thanks so much !!
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Carlos Rodriguez
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Share this question
or