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

How to do editing in Advanced data binding?

4 Answers 105 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 20 Jul 2014, 07:50 PM
Hi All,

I've built a radgrid based on the advanced data binding demo: here. Where can I also find an example of doing editing? Especially when the grid will be postback and disappeared after edit command, how should I handle it?

Thanks in anticipation!

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Jul 2014, 04:44 AM
Hi Tim,

You can take a look at the following demo to have Edit functionality for your Grid. You can customize it according to your needs.
Grid - Manual CRUD Operations
Grid - Edit Form Types

Thanks,
Princy
0
Tim
Top achievements
Rank 1
answered on 22 Jul 2014, 12:06 PM
TYVM Princy, you're a life saver, btu can I also ask how do I rebind the radgrid after update, as it will disappear from needdatasource?

I've tried to use both DataBind and ReBind in by BatchEditCommand but no luck.

Thanks
0
Tim
Top achievements
Rank 1
answered on 22 Jul 2014, 12:12 PM
Thanks this can be done by calling the advanced data binding again at the end of the update command to refill the table.
0
Princy
Top achievements
Rank 2
answered on 23 Jul 2014, 04:21 AM
Hi Tim,

I'm not clear about your issue. Please take a look at the sample code snippet for serverside Batch Edit.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnBatchEditCommand="RadGrid1_BatchEditCommand" AutoGenerateColumns="false">
    <MasterTableView EditMode="Batch" CommandItemDisplay="Top" DataKeyNames="OrderID">
        <BatchEditingSettings OpenEditingEvent="Click" EditType="Row" />
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" ReadOnly="true" />
            <telerik:GridTemplateColumn DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName">
                <ItemTemplate>
                    <%# Eval("ShipName")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadDropDownList ID="RadDropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="ShipName" DataValueField="ShipName">
                    </telerik:RadDropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<br />
<asp:Label ID="lblDisplay" runat="server" Text=""></asp:Label>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
DataTable dt1 = new DataTable();   
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    string selectQuery1 = "SELECT [OrderID], [ShipName] FROM [Orders]";
    SqlDataAdapter adapter1 = new SqlDataAdapter(selectQuery1, conn);
    conn.Open();
    adapter1.Fill(dt1);
    conn.Close();
    RadGrid1.DataSource = dt1;
}
 
protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
{       
    foreach (GridBatchEditingCommand command in e.Commands)
    {
        if (command.Type == GridBatchEditingCommandType.Update)
        {              
            Hashtable newValues = command.NewValues;               
            string OrderID = newValues["OrderID"].ToString();
            string ShipName = newValues["ShipName"].ToString();             
            try
            {
                conn.Open();
                string query = "UPDATE Orders SET ShipName='" + ShipName + "' WHERE OrderID = '" + OrderID + "'";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                lblDisplay.Text = "Product with ID " + OrderID + " is updated!";
            }
            catch (Exception ex)
            {
                lblDisplay.Text = "Product with ID " + OrderID + " cannot be updated. ";
            }
        }
        if (command.Type == GridBatchEditingCommandType.Insert)
        {
            Hashtable newValues = command.NewValues;                 
            string ShipName = newValues["ShipName"].ToString();
            try
            {
                conn.Open();
                string query = "INSERT into Orders (ShipName ) VALUES ('" + ShipName + "')";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                lblDisplay.Text = "Product Inserted!";
            }
            catch (Exception ex)
            {
                lblDisplay.Text = "Product cannot be Inserted ";
            }
        }
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Tim
Top achievements
Rank 1
Share this question
or