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

Grid not refreshing correctly after adding or deleting data

2 Answers 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 02 Jul 2012, 06:41 PM
I am using the latest Telerik dlls and all my events are firing correctly in the code behind. When you add or delete a row in the grid it updates the datasource correctly and I can debug and see on the OnNeedDataSource that the correct datasource is being set with either the row added or deleted. But the client grid is still showing the row you deleted or not showing the row you added. I have tried it with a RadAjaxManager and a RadAjaxPanel and both don't update the grid correctly. When I do an edit it will update correctly. Here is the code aspx code.
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
                    <telerik:RadGrid ID="dgStations" runat="server" PageSize="10" AllowPaging="true"
                        AutoGenerateColumns="false" OnInsertCommand="dgStationsOnInsertCommand" OnUpdateCommand="dgStationsOnUpdateCommand"
                        OnDeleteCommand="dgStationsOnDeleteCommand" OnNeedDataSource="dgStationsOnNeedDataSource">
                        <PagerStyle Mode="NextPrevAndNumeric" />
                        <MasterTableView DataKeyNames="StationId" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
                            <Columns>
                                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" SortExpression="Description">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ServerPort" HeaderText="Server Port" SortExpression="ServerPort">
                                </telerik:GridBoundColumn>
                                <telerik:GridEditCommandColumn ButtonType="LinkButton" UniqueName="EditCommandColumn"
                                    EditText="Edit">
                                </telerik:GridEditCommandColumn>
                                <telerik:GridButtonColumn ConfirmText="Delete this station?" ConfirmDialogType="Classic"
                                    ButtonType="LinkButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
                                </telerik:GridButtonColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
                </telerik:RadAjaxPanel>

Any idea why it isn't updating correctly?

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Jul 2012, 05:09 AM
Hi,

Unfortunately I couldn't replicate any issue with your code. When binding the RadGrid with NeedDataSource event the Grid will automatically refresh after the update/delete operation.Please take a look into the sample code snippet I tried with different data field.

ASPX:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="dgStations">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="dgStations" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
    <telerik:RadGrid ID="dgStations" runat="server" PageSize="10" AllowPaging="true"
    AutoGenerateColumns="false" onneeddatasource="dgStations_NeedDataSource" onupdatecommand="dgStations_UpdateCommand" ondeletecommand="dgStations_DeleteCommand" >
        <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
            <Columns>
                <telerik:GridBoundColumn DataField="ShipCity" HeaderText="Description" SortExpression="Description">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipName"  HeaderText="Server Port" SortExpression="ServerPort">
                </telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn ButtonType="LinkButton" UniqueName="EditCommandColumn"
                    EditText="Edit">
                </telerik:GridEditCommandColumn>
                <telerik:GridButtonColumn ConfirmText="Delete this station?" ConfirmDialogType="Classic"
                    ButtonType="LinkButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
                </telerik:GridButtonColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
</telerik:RadAjaxPanel>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
public SqlCommand SqlCommand = new SqlCommand();
 
protected void dgStations_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    string selectQuery = "select top 10 * from Orders";
    SqlDataAdapter adapter1 = new SqlDataAdapter(selectQuery, conn);
    DataTable dt1 = new DataTable();
    conn.Open();
    adapter1.Fill(dt1);
    conn.Close();
    dgStations.DataSource = dt1;
}
protected void dgStations_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    GridEditableItem editItem = (GridEditableItem)e.Item;
    string orderId = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["OrderID"].ToString();
    string ShipCity = (editItem["ShipCity"].Controls[0] as TextBox).Text;
    string ShipName = (editItem["ShipName"].Controls[0] as TextBox).Text;
    conn.Open();
    string updateQuery = "Update Orders set ShipName='" + ShipName + "',ShipCity='" + ShipCity + "' where OrderID='" + orderId + "'";
    SqlCommand.CommandText = updateQuery;
    SqlCommand.Connection = conn;
    SqlCommand.ExecuteNonQuery();
    conn.Close();
}
protected void dgStations_DeleteCommand(object sender, GridCommandEventArgs e)
{
    conn.Open();
    GridDataItem editItem = (GridDataItem)e.Item;
    string orderID = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["OrderID"].ToString();
    string delQuery = "delete from orders where OrderID='" + orderID + "'";
    SqlCommand.CommandText = delQuery;
    SqlCommand.Connection = conn;
    SqlCommand.ExecuteNonQuery();
    conn.Close();
}

Please provide the code behind if it doesn't help.

Thanks,
Shinu.
0
Eric
Top achievements
Rank 1
answered on 03 Jul 2012, 04:02 PM
Here is my code behind:
public void dgStationsOnInsertCommand(object sender, GridCommandEventArgs e)
{
    var item = (GridEditableItem)e.Item;
    if (item != null)
    {
        Station station = new Station();
        Hashtable values = new Hashtable();
        item.ExtractValues(values);
        if(values["Description"] != null)
        {
            station.Description = values["Description"].ToString();
        }
        if (values["ServerPort"] != null)
        {
            station.ServerPort = values["ServerPort"].ToString();
        }
        optionCache.AddStation(station);
    }
}
 
public void dgStationsOnDeleteCommand(object sender, GridCommandEventArgs e)
{
    int stationId = (int)((GridEditableItem)e.Item).GetDataKeyValue("StationId");
    optionCache.RemoveStation(stationId);
}
 
public void dgStationsOnUpdateCommand(object sender, GridCommandEventArgs e)
{
    var item = (GridEditableItem)e.Item;
    if (item != null)
    {
        Station station  =new Station();
        station.StationId = (int)item.GetDataKeyValue("StationId");
        item.UpdateValues(station);
        optionCache.UpdateStation(station);
    }
}
 
public void dgStationsOnNeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dgStations.DataSource = optionCache.GetAllStations();
}
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Eric
Top achievements
Rank 1
Share this question
or