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

Update/Insert/Delete record in Group

1 Answer 16 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anne
Top achievements
Rank 1
Anne asked on 08 May 2014, 08:55 PM
How can I Update/Insert/Delete a record in a grid having a GroupByExpressions ?

My grid is a timesheet and is grouped by weekdays: sunday, monday, tuesday, wednesday, thursday, friday, saturday.
Thanks!
 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 13 May 2014, 07:14 AM
Hi Anne,

Please take a look at the sample code snippet. The inserted record will be shown in the grouped column.
Please share your code and elaborate on your requirement if this doesn't help.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="true" OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand"    OnDeleteCommand="RadGrid1_DeleteCommand">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID" CommandItemDisplay="Top">
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <SelectFields>
                    <telerik:GridGroupByField FieldAlias="ShipName" FieldName="ShipName"></telerik:GridGroupByField>
                </SelectFields>
                <GroupByFields>
                    <telerik:GridGroupByField FieldName="ShipName" SortOrder="Descending"></telerik:GridGroupByField>
                </GroupByFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
        <Columns>
            <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" ConfirmText="Delete" ConfirmDialogType="RadWindow" />
            <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" UniqueName="OrderID" ReadOnly="true" />
            <telerik:GridBoundColumn HeaderText="CustomerID" DataField="CustomerID" UniqueName="CustomerID" />
            <telerik:GridBoundColumn HeaderText="ShipName" DataField="ShipName" UniqueName="ShipName" />
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
public static DataTable dtTable;
SqlConnection SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
public SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();
public SqlCommand SqlCommand = new SqlCommand();
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dtTable = new DataTable();
    try
    {
        SqlConnection.Open();
        string selectQuery = "SELECT * FROM Orders";
        SqlDataAdapter.SelectCommand = new SqlCommand(selectQuery, SqlConnection);
        SqlDataAdapter.Fill(dtTable);
        RadGrid1.DataSource = dtTable;
    }
    catch (Exception ex)
    {
        RadGrid1.Controls.Add(new LiteralControl("Unable to insert Employee. Reason: " + ex.Message));
    }
    finally
    {
        //Close the SqlConnection   
        SqlConnection.Close();
    }
}
 
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
    string CustomerID = (insertedItem["CustomerID"].Controls[0] as TextBox).Text;
    string ShipName = (insertedItem["ShipName"].Controls[0] as TextBox).Text;
 
    try
    {
        //Open the SqlConnection   
        SqlConnection.Open();
        //Insert Query to insert into  the database    
        string insertQuery = "INSERT into  Orders(CustomerID,ShipName) values('" + CustomerID + "','" + ShipName + "')";
        SqlCommand.CommandText = insertQuery;
        SqlCommand.Connection = SqlConnection;
        SqlCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        RadGrid1.Controls.Add(new LiteralControl("Unable to insert Employee. Reason: " + ex.Message));
        e.Canceled = true;
    }
    finally
    {
        //Close the SqlConnection   
        SqlConnection.Close();
    }
}
protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
{
    //Code to delete
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    //Code to Update
}

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