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

Editing Radgrid

5 Answers 99 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arnaud
Top achievements
Rank 1
Arnaud asked on 31 Jan 2011, 10:18 PM
I have a page where I have placed 2 RadGrids for testing purposes.
the 2 grids are filled by the same data; but Grid 1 is binded automatically via an SQLDataSource, and Grid 2 is binded manually in my vb code.
I have a created a procedure to handle the update command of each grid.

The update event for grid 1 is executed successfully.
The update event for grid does not execute any statement of the procedure which makes me think that the i have to do something different for cases when a grid is binded manually:

For the record:
- all grids are using templates that are identical.
- the templates have buttons that are set to Update Command.

Can someone help me with this?

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Feb 2011, 12:54 PM
Hello Arnaud,

I assume that you are binding the grid in PageLoad event, which is Simple data binding technique. If you are using any advanced feature in grid(like insert/update/delete), then a better approach is using "AdvancedData binding" using NeedDataSource event.
For more information about this can be available here.
Advanced Data-binding (using NeedDataSource event)

Here is the sample code that I tried in my application for manual update operation.
ASPX:
<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="False" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
    OnUpdateCommand="RadGrid1_UpdateCommand">
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="EmployeeID">
        <Columns>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("FirstName") %>'></asp:TextBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                </ItemTemplate>
             </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
           //populate RadGrid
    }
   protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
   {
       GridEditFormItem editItem = (GridEditFormItem)e.Item;
       TextBox txtname = (TextBox)editItem.FindControl("TextBox1");
       int id =Convert.ToInt32(editItem.GetDataKeyValue("EmployeeID"));
       SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
       SqlCommand cmd = new SqlCommand("Update Employees set FirstName=@FirstName where EmployeeID=@EmployeeID ", con);
       cmd.Parameters.Add(new SqlParameter("@EmployeeID", id ));
       cmd.Parameters.Add(new SqlParameter("@FirstName", txtname.Text));
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
       RadGrid1.Rebind();
   }

Could you please paste the complete code if it doesn't help?

Thanks,
Princy.
0
Arnaud
Top achievements
Rank 1
answered on 01 Feb 2011, 05:46 PM
Hey Princy.
the problematic grid is bound via "NeedDatasource" event.
I'm loosing hope about being able to manually update a grid that is manually bound.

I added a 3rd grid which is bound automatically via an SQL data source. for that one, the update event fires but the template controls that are for the update are not accessed. here is my code below for the update command

Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand

           Try

                        Dim the_edited_row As GridEditFormItem = CType(e.Item, GridEditFormItem)

            Dim theMailMessage As MailMessage = New MailMessage("someone@something.com", "someone@something.com", "Working", "Working")

            Dim theClient As SmtpClient = New SmtpClient(ConfigurationManager.AppSettings("SMTPServer").ToString)

            theClient.Send(theMailMessage)

        Catch

            Dim theMailMessage As MailMessage = New MailMessage("someone@something.com", "someone@something.com", "Not Working", "Not Working")

            Dim theClient As SmtpClient = New SmtpClient(ConfigurationManager.AppSettings("SMTPServer").ToString)

            theClient.Send(theMailMessage)

        End Try

End sub

I always receive the "Not Working" mail which means that the edit form is not casted. why would that be?

0
Princy
Top achievements
Rank 2
answered on 02 Feb 2011, 06:27 AM
Hello Arnaud,

I guess you are using 'InPlace' edit mode. If so change the edit item in UpdateCommand like below and check whether it works now.

VB.NET:
Protected Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    Try
        Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                ' .  .   .   .  .  .  .  .  .
     Catch
    End Try
 
End Sub


Thanks,
Princy.
0
Arnaud
Top achievements
Rank 1
answered on 04 Feb 2011, 07:53 PM
Hello Princy.
I figured what the problem is.
When my grid goes into edit mode.
e.item is not a grideditformitem or grideditableitem.
It's just a griddataitem and when trying to convert it to either a grideditform or grideditableitem (via DirectCast or Ctype) the conversion fails.

Do you have any idea why the e.item does not become and editable item (grideditform or grideditableitem) in edit mode?
0
Arnaud
Top achievements
Rank 1
answered on 04 Feb 2011, 08:08 PM
Never mind. i found a way of casting it:

Dim edititem As GridEditFormItem = DirectCast(TryCast(e.Item, GridDataItem).EditFormItem, GridEditFormItem)
Tags
Grid
Asked by
Arnaud
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Arnaud
Top achievements
Rank 1
Share this question
or