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

Reloading RadGrid Initial Data from DataSource

6 Answers 951 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nancy
Top achievements
Rank 1
Nancy asked on 03 Dec 2012, 08:55 PM
I want to know a way I can reload the RadGrid with the DataSource that it is originally loading from. Case Scenario would involve the user click on a button to refresh the RadGrid to return the original data from the database, because they don't want the updated changes on the RadGrid anymore. I've tried DataBind, but it does not do anything for me. Is there an event that I need to use to do what I need it to do?
I am using .ASPX and C#. Thanks in advanced.

 

protected void RadGrid1_Load(object source, GridNeedDataSourceEventArgs e)
{
    String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(@"SELECT columns FROM tables)", conn);
    DataSet myDataTable = new DataSet();
    conn.Open();
    try
    {
        adapter.Fill(myDataTable, "tables");
    }
    finally
    {
        conn.Close();
    }
    DataView myDataView = myDataTable.Tables["tabels"].DefaultView;
    RadGrid1.MasterTableView.DataSource = myDataView;
    RadGrid1.MasterTableView.DataBind();
}

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Dec 2012, 04:24 AM
Hello,

Please use Advanced Data Binding to bind your grid.

//Refresh button
protected void LinkButton1_Click(object sender, EventArgs e)
{
    RadGrid1.Rebind();
}
 

Thanks,
Jayesh Goyani
0
Nancy
Top achievements
Rank 1
answered on 04 Dec 2012, 02:14 PM

I've tried what you have suggested, and it is not reseting the radgrid to the data from the the datasource. Here's what I have:

<asp:Button ID="Refresh" Text="Refresh" runat="server" 
      style="position: relative; float: right; top: 0px; left: 0px; height: 25px; width: 125px;" CommandName="RadGrid1_Load"/>
protected void RadGrid1_Load(object source, EventArgs e)
{
    RadGrid1.Rebind();
}

Do you have any other suggestions?
0
Manuel Buendia
Top achievements
Rank 1
answered on 04 Dec 2012, 07:37 PM
Intenta con 


RadGrid1.DataBind();
        RadGrid1.Rebind();
        RadGrid1.MasterTableView.Rebind();
        RadGrid1.MasterTableView.DataBind();
0
Eyup
Telerik team
answered on 06 Dec 2012, 02:11 PM
Hi Nancy,

Could you verify that the datasource remains unchanged at the time when the button fires click event? Please elaborate some more on the specific scenario and on the desired behavior.

Kind regards,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Nancy
Top achievements
Rank 1
answered on 06 Dec 2012, 08:13 PM
What I want is to reload the DataSet to the original data from the table before the users had updated on the RadGrid. For an example, a user had changed one item in the column, "Fruit", and the original string was 'Apple', and they changed it to 'Orange', but they decided not to have Orange uploaded to the table in the database. They would press the Reset button to get the original string, which was 'Apple'.

Here is my DataSource for my RadGrid. I used the OnNeedDataSource Event:

public DataSet TABLE    {
        get
        {
            object obj = this.Session["TABLE"];
            if (obj != null)
            {
                return(DataSet) obj;
            }
  
            DataSet iTABLE = new DataSet();
  
            String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(@"SELECT COLUMNS FROM TABLE", conn);
  
            adapter.Fill(iTABLE, "TABLE");
  
            this.Session["TABLE"] = iTABLE;
            return iTABLE;
        }
    }
  
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = this.TABLE;
        this.TABLE.Tables["TABLE"].PrimaryKey = new DataColumn[]
        {
            this.TABLE.Tables["TABLE"].Columns["TABLE_ID"]
        };
  
    }

This Code is showing my Update for the RadGrid....

protected void GridUpdate(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    GridEditableItem editedItem = (GridEditableItem)e.Item;
    DataTable ordersTable = this.TABLE.Tables["TABLE"];
    //Locate changed row
    if(ordersTable != null)
    {
        DataRow[] changedRows = ordersTable.Select("TABLE_ID = " + editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["TABLE_ID"]);
        if (changedRows.Length != 1)
        {
            SetMessage("Unable to located the TABLE ID for updating.");
            e.Canceled = true;
            return;
        }
        //Updating New Values
        Hashtable newValues = new Hashtable();
        //fill values from editable columns
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
        DataRow changedRow = changedRows[0];
        changedRow.BeginEdit();
        try
        {
            foreach (DictionaryEntry entry in newValues)
            {
                changedRow[(string)entry.Key] = entry.Value;
            }
            changedRow.EndEdit();
        }
        catch (Exception ex)
        {
            changedRow.CancelEdit();
            SetMessage("Unable to update TABLE ID. Reason: " + ex.Message);
            e.Canceled = true;
        }
      
        SetMessage("TABLE " + changedRow["TABLE_ID"] + " updated");        
    }
    else
    {
        SetMessage("unable to locate the TABLE_ID for update.");
        e.Canceled = true;
        return;
    }
}

0
Nancy
Top achievements
Rank 1
answered on 06 Dec 2012, 08:43 PM
never mind, i have gotten it to work.

What I did:

adapter.Fill(this.TABLE, "TABLE");

and it refilled the table with the original data.

Thanks for the replies!
Tags
Grid
Asked by
Nancy
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Nancy
Top achievements
Rank 1
Manuel Buendia
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or