We help IT Professionals succeed at work.

Win Form DataTable Display Properties

mac-will asked
Medium Priority
Last Modified: 2013-12-17
Hello all,

I have a DataGridView that is bound to a BindingSource.  I do some manual processing and create several DataTables I then set the datasource of the BindingSource to the desired DataTable.

This works well and the DataGridView updates and displays the data I want to display.

What I am looking for is a way to associate display properties, such as column width, with each datatable.

So for example if DataTable_1 has 3 columns; "c1", "c2", "c3"  and DataTable_2 has 2 columns: "b1", "b2"

I want to be able to associate properties like c1's width is 25px so that when I set the DataSource to DataTable_1  I don't have to iterate through the columns of the DataGridView and set each column property.

Any ideas?


Watch Question

anarki_jimbelSenior Developer

Honestly I don't quite understand the question. Because there are now width properties for DataColumn class you need to set widt for the DataGridView  somehow. And you need to set it explicitly for each column. I can't see any other way. You may create a procedure that e.g. count a number of columns and sets width according to this number, or you may store this width somewhere else but anyway you need to set this width explicitly column by column. Or I miss something?


I guess the question boils down to: Can you store a column width and/or hidden property in a DataTable such that a DataGridView will automatically display the dataTable this way?

Sounds like it is not possible which would mean when I change the DataTable I will need to iterate through the DataGridView columns and set their properties individually.
Senior Developer
I believe it's possible. I have written small example using DataColumn..::.ExtendedProperties Property :


See the snippet - should be quite clear. If not - ask questions.

        DataTable table = new DataTable();
        DataColumn col1 = new DataColumn("One",typeof(int) );
        DataColumn col2 = new DataColumn("Two", typeof(string));
        DataColumn col3 = new DataColumn("Three", typeof(string));
        DataColumn col4 = new DataColumn("Four", typeof(int));
        private void Form1_Load(object sender, EventArgs e)
            // set additional property for a column
            col1.ExtendedProperties.Add("Width", 40);
            col2.ExtendedProperties.Add("Width", 60);
            col3.ExtendedProperties.Add("Width", 80);
            col4.ExtendedProperties.Add("Width", 100);
            DataRow row = table.NewRow();
            row[0] = 123;
            row[1] = "qwe";
            row[2] = "tli";
            row[3] = 456;
            this.dataGridView1.DataSource = table;
            // set width for gridview columns using data from Datatable columns
            for(int i = 0; i<table.Columns.Count; i++)
                dataGridView1.Columns[i].Width = (int)table.Columns[i].ExtendedProperties["Width"];

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thank you.

This isn't quite automatic but this definitely will work as a work around.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.