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

Am I using data binding correctly? (Adding columns to data table)

5 Answers 317 Views
GridView
This is a migrated thread and some comments may be shown as answers.
BECK
Top achievements
Rank 1
BECK asked on 24 Oct 2011, 02:55 PM
I have a data table object that I bind a gridview to. During runtime, I'd like to give the user an option to add a column to the gridview. Now ideally the way I thought it would work was if I add a column to the Data Table object that the grid view is bound to, it would automatically add that column to the gridview because of the data binding. But when I try that, only the Data Table is updated with the new column and not the GridView. Am I not using databinding correctly? 

Thanks

5 Answers, 1 is accepted

Sort by
0
BECK
Top achievements
Rank 1
answered on 26 Oct 2011, 12:55 AM
Really? No help at all?
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Oct 2011, 07:00 AM
Hello Beck,

I will prepare a datatable sample shorty.

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Oct 2011, 07:06 AM
Hello again,

At a first glance i have to ask you if you have defined the columns for the grid or have you left it on autogeneratecolumns. If you have defined all the columns so far you will also have to define the new ones, but if not the columns should be visible in the column chooser if you have hidden them initially.

Please take a look at the following example:
using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1 = new RadGridView();
 
    public Form1()
    {
        InitializeComponent();
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
 
        this.Controls.Add(radGridView1);
    }
 
    private void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        //hide 2 columns here, these will be later accessible to the using by using column chooser
        for (int i = 0; i < 2; i++)
        {
            radGridView1.Columns[i].IsVisible = false;
        }
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
 
        radGridView1.DataSource = GetTable();
    }
 
    /// <summary>
    /// This example method generates a DataTable.
    /// </summary>
    private static DataTable GetTable()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
 
        //
        // Here we add five DataRows.
        //
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        return table;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
BECK
Top achievements
Rank 1
answered on 26 Oct 2011, 01:11 PM
Here's what I did to reproduce the issue I've been having. 

1) Create a Windows Application form
2) Add a RadGridView control to the form
3) Add a RadButton control to the form and set the text to "Add Column"
4) In the code, on load, I construct a DataTable object with 4 columns and add some rows to it and set the RadGridView's DataSource to this DataTable
5) In the button's event handler, I add a column to the DataTable object
6) During runtime, the DataTable's contents are displayed as expected in the RadGridView control
7) Now, when I click the RadButton, no changes are reflected in the RadGridView showing that a column was added to its DataSource. 

To ensure that I'm using this property correctly, I followed the steps above but with the DataGridView control (the one supplied by default from Microsoft) and it does indeed adds the column when  hit the button. 

I checked the AutoGenerateColumns property in the RadGridView and set it to True and it still has this issue. 

NOTE that if I added rows to the DataSource, it reflects those changes on the RadGridView but not when adding column. 

Am I missing something?
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 27 Oct 2011, 06:29 AM
Hello again,

Sorry, apparently you need to reset the datasource after changing the columns for a DataTable
I've updated my example to reflect this scenario:
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1 = new RadGridView();
    private RadButton radButton1 = new RadButton();
    public Form1()
    {
        InitializeComponent();
 
        radButton1 = new RadButton();
        radButton1.Text = "Add column!";
        radButton1.Click += new EventHandler(radButton1_Click);
        radButton1.Dock = DockStyle.Bottom;
        this.Controls.Add(radButton1);
 
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
 
        this.Controls.Add(radGridView1);
    }
 
    void radButton1_Click(object sender, EventArgs e)
    {
        using (radGridView1.DeferRefresh())
        {
             
            var table = radGridView1.DataSource as DataTable;
            if (table != null)
            {
                var currentRow = radGridView1.CurrentRow;
                radGridView1.DataSource = null;
                table.Columns.Add("Column" + table.Columns.Count, typeof(string));
                radGridView1.DataSource = table;
                if (currentRow != null)
                {
                    radGridView1.CurrentRow =
                        radGridView1.Rows.Where(r => r.DataBoundItem == currentRow.DataBoundItem).FirstOrDefault();
                }
            }   
        }
         
    }
 
    private void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        //hide 2 columns here, these will be later accessible to the using by using column chooser
        for (int i = 0; i < 2; i++)
        {
            //radGridView1.Columns[i].IsVisible = false;
        }
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        radGridView1.DataSource = GetTable();
    }
 
    /// <summary>
    /// This example method generates a DataTable.
    /// </summary>
    private static DataTable GetTable()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
 
        //
        // Here we add five DataRows.
        //
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        return table;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
Tags
GridView
Asked by
BECK
Top achievements
Rank 1
Answers by
BECK
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Share this question
or