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

Refresh radGridView

3 Answers 269 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 13 Aug 2009, 08:06 PM
Hi All,

I have a radGridView that is populated via a TableAdapter:

buUserTableAdapter buUserTA = new buUserTableAdapter();  
protected BindingSource buUserBindingSource = new BindingSource();  
 
buUserBindingSource.DataSource = buUserTA.GetData();  
this.radGvAdminUsers.DataSource = buUserBindingSource; 

The radGridView is read only, however, I have a "detail" section below the radGridView where users can edit the contents of the records.  The database is updated upon clicking a "save" button via a stored procedure of the TableAdapter:

private void radBtnAdminSaveUser_Click(object sender, EventArgs e)  
{  
    int userId = Convert.ToInt32(lblAdminUserDBID.Text);  
    string userLogin = this.radTxtAdminLoginId.Text.ToUpper();  
    string lastName = this.radTxtAdminLastName.Text;  
    string firstName = this.radTxtAdminFirstName.Text;  
    string userPhone = this.radMTxtAdminPhone.Value.ToString().Trim();  
    int userRole = Convert.ToInt32(this.radComboAdminRole.SelectedValue);  
      
    string userRoleDesc = radComboAdminRole.Text;  
    bool userActive = Convert.ToBoolean(this.radCbAdminUserIsActive.Checked);  
    if (userId != 0)  
    {  
        buUserTA.UpdateSP(userId, userLogin, lastName, firstName, userPhone, userRole, userActive, System.Configuration.ConfigurationManager.AppSettings["UserNetworkId"].ToString());  
    }  
    else 
    {  
        //Need to do an insert for a new user and update for the new id  
        userId = Convert.ToInt32(buUserTA.InsertSP(userLogin, lastName, firstName, userPhone, userRole, System.Configuration.ConfigurationManager.AppSettings["UserNetworkId"].ToString()));  
    }  

When this is clicked, the database is properly updated, however, the radGridView does not refresh to reflect the changes.  

From what I've read, it seems like the radGridView is supposed to automatically updated.  What am I missing or doing wrong?  Any help with this is greatly appreciated.

Robert

3 Answers, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 17 Aug 2009, 01:13 PM
Hi Robert,

Thank you for writing. The code you provided seems to be correct. However I am curious how the controls you are using to edit the data are bound. Please send the code which you are using to bind the editing controls, I have a feeling that there is something wrong there. I am looking forward to your reply.

Greetings,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Robert
Top achievements
Rank 1
answered on 18 Aug 2009, 05:55 PM
Hi Victor,

Thanks for getting back to me.

The controls I am using to edit the data are not bound.  However, the text property of them is populated using the following code:

private void UpdateUsersPanel(DataRowView currentRow)  
{  
    if (currentRow != null)  
    {  
        this.lblAdminUserDBID.Text = this.GetSafeString(currentRow["user_db_id"]);  
        this.radTxtAdminLoginId.Text = this.GetSafeString(currentRow["user_login"]);  
        this.radTxtAdminLastName.Text = this.GetSafeString(currentRow["last_name"]);  
        this.radTxtAdminFirstName.Text = this.GetSafeString(currentRow["first_name"]);  
        this.radMTxtAdminPhone.Value = this.GetSafeString(currentRow["phone_no"]);  
        string sRole = this.GetSafeString(currentRow["role_description"]);  
        if (!string.IsNullOrEmpty(sRole.Trim()))  
        {  
            this.radComboAdminRole.SelectedItem = sRole;  
        }  
        else 
        {  
            this.radComboAdminRole.SelectedItem = -1;  
            this.radComboAdminRole.Text = string.Empty;  
        }  
        if (!string.IsNullOrEmpty(currentRow["is_active"].ToString()))  
        {  
            if (Convert.ToBoolean(currentRow["is_active"]) == true)  
            {  
                this.radCbAdminUserIsActive.Checked = true;  
            }  
            else 
            {  
                this.radCbAdminUserIsActive.Checked = false;  
            }  
        }  
    }  
    else 
    {  
        this.radTxtAdminLoginId.Text = string.Empty;  
        this.radTxtAdminFirstName.Text = string.Empty;  
        this.radTxtAdminLastName.Text = string.Empty;  
        this.radMTxtAdminPhone.Text = string.Empty;  
        this.radComboAdminRole.SelectedItem = -1;  
        this.radComboAdminRole.Text = string.Empty;  
        this.radCbAdminUserIsActive.Checked = true;  
        this.lblAdminUserDBID.Text = "0";  
        this.lblAdminUserLastUpdated.Text = string.Empty;  
    }  

Which is called like so:

private void radGvAdminUsers_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)  
{  
    UpdateUsersPanel((DataRowView)this.radGvAdminUsers.CurrentRow.DataBoundItem);  

Again, when clicking the "Save" button, the stored proc fires properly and the data is stored to the database; however, the radGridView does not reflect this change.  Am I not binding the radGridView correctly?  Do I need to call the GetData() method of the TableAdapter again?

Robert



0
Victor
Telerik team
answered on 20 Aug 2009, 09:05 AM
Hi Robert,

I suggest using data binding for such a scenario because it simplifies the code significantly. Here is an example of RadGridView bound to a binding source which points to the Employees table of the ubiquitous Northwind database. The textboxes are also bound to the binding source which leads to automatic updating of both the textboxes and RadGridView. The code is as follows:

NorthwindDataSetTableAdapters.EmployeesTableAdapter adapter = new WindowsFormsApplication1.NorthwindDataSetTableAdapters.EmployeesTableAdapter();  
adapter.Fill(this.northwindDataSet.Employees);  
 
this.radGridView1.DataSource = this.employeesBindingSource;  
 
this.radTextBox1.DataBindings.Add(new Binding("Text"this.employeesBindingSource, "FirstName"true, DataSourceUpdateMode.OnPropertyChanged));  
this.radTextBox2.DataBindings.Add(new Binding("Text"this.employeesBindingSource, "LastName"true, DataSourceUpdateMode.OnPropertyChanged));  
this.radTextBox3.DataBindings.Add(new Binding("Text"this.employeesBindingSource, "Title"true, DataSourceUpdateMode.OnPropertyChanged));  
this.radTextBox4.DataBindings.Add(new Binding("Text"this.employeesBindingSource, "City"true, DataSourceUpdateMode.OnPropertyChanged)); 

This is all that has to be done. The BindingSource class is one of the most important classes for manipulating data in .NET 2.0. I recommend using it whenever possible because it dramatically reduces the code that needs to be written which as a side effect leads to less bugs.

Note that if you do not update your underlying database with the table adapter Update() method, you will have to refill your tables whenever you make such external changes. For example if you have a method which updates the database without the table adapter knowing about it, your UI and data classes will not be updated with the new data in the database, and a refill will be required. Write again if you need further assistance.

Kind regards,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Robert
Top achievements
Rank 1
Answers by
Victor
Telerik team
Robert
Top achievements
Rank 1
Share this question
or