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

Problem when I try to edit an specific row

2 Answers 57 Views
Grid
This is a migrated thread and some comments may be shown as answers.
davortsan
Top achievements
Rank 1
davortsan asked on 09 Feb 2015, 10:48 AM
Hi mates,

I have a radgrid and a external textbox and button to search specifics rows in the radgrid. Basically I change the SelectCommand of the RadGrid DataSource when I use the search button.
 
The first column of the radgrid is the following:

<telerik:GridTemplateColumn UniqueName="EditarEstado" ItemStyle-HorizontalAlign="Center" AutoPostBackOnFilter="false">
    <HeaderStyle Width="50px" />
    <ItemTemplate>
          <asp:ImageButton ID="btnEditarEstado" CommandName="Edit" runat="server" ImageUrl="~/img/buttons/edit.png"
          CommandArgument='<%# Eval("id") %>' ToolTip="Modificar" />
    </ItemTemplate>
</telerik:GridTemplateColumn>

If I search an specific row using the textbox and the search button, the row is showed successfully in the radgrid. The issue happens if I try to edit that row using the btnEditarEstado ImageButton. When I clic on it, autopostback is executed and the default SelectCommand of the radgrid datasource is set, so, it's like if I try to edit the first row and not the row that I searched.

How could I edit the selected row and not the default first one, please?

Please, let me know if you have any problem to understand my problem.

Thanks a lot in advance.

Cheers.

2 Answers, 1 is accepted

Sort by
0
davortsan
Top achievements
Rank 1
answered on 09 Feb 2015, 11:18 AM
I've just checked if I use the following edit column in the radgrid, the behaviour is the same...

<telerik:GridEditCommandColumn UniqueName="EditarEstado" ButtonType="ImageButton" ItemStyle-HorizontalAlign="Center"  EditImageUrl="../img/buttons/edit.png" ItemStyle-Width="50px" />

... Without more ideas... ;(

Thanks in advance.

0
davortsan
Top achievements
Rank 1
answered on 09 Feb 2015, 03:39 PM
Hi mates,

I found the solution for this issue.

If I update the SelectCommand of the RadGrid in the ItemCommand function and then I set e.Edit to true, the issue is resolved. Additionaly, I had to add the CancelCommand to refresh the SelectCommand again becuase I had problems if I cancel the update.

This is the code that resolves this issue:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                string _query = "SELECT id, estado, descripcion FROM dbo.tblEstadosPresupuesto";

                if (this.txtBuscarEstado.Text.Trim() != "")
                {
                    _query = _query + " WHERE (estado LIKE '%" + this.txtBuscarEstado.Text.Trim() + "%')";
                }
                _query = _query + " ORDER BY estado";

                this.SqlDataSource1.SelectCommand = _query;
                this.RadGrid1.DataBind();

                e.Item.Edit = true;
            }
        }

protected void RadGrid1_CancelCommand(object sender, GridCommandEventArgs e)
        {
            string _query = "SELECT id, estado, descripcion FROM dbo.tblEstadosPresupuesto";

            if (this.txtBuscarEstado.Text.Trim() != "")
            {
                _query = _query + " WHERE (estado LIKE '%" + this.txtBuscarEstado.Text.Trim() + "%')";
            }
            _query = _query + " ORDER BY estado";

            this.SqlDataSource1.SelectCommand = _query;
            this.RadGrid1.DataBind();
        }

Now, all work fine.

Thanks a lot.

Cheers.

David Ortega.
Tags
Grid
Asked by
davortsan
Top achievements
Rank 1
Answers by
davortsan
Top achievements
Rank 1
Share this question
or