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

6 posts, 1 answers
  1. BECK
    BECK avatar
    15 posts
    Member since:
    Oct 2010

    Posted 24 Oct 2011 Link to this post

    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
  2. BECK
    BECK avatar
    15 posts
    Member since:
    Oct 2010

    Posted 25 Oct 2011 Link to this post

    Really? No help at all?
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Oct 2011 Link to this post

    Hello Beck,

    I will prepare a datatable sample shorty.

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Oct 2011 Link to this post

    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
  6. BECK
    BECK avatar
    15 posts
    Member since:
    Oct 2010

    Posted 26 Oct 2011 Link to this post

    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?
  7. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 27 Oct 2011 Link to this post

    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
Back to Top
UI for WinForms is Visual Studio 2017 Ready