Win Form DataTable Display Properties

Posted on 2009-02-12
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?


Question by:mac-will
  • 2
  • 2
LVL 30

Expert Comment

ID: 23627536
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?

Author Comment

ID: 23664394
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.
LVL 30

Accepted Solution

anarki_jimbel earned 2000 total points
ID: 23665207
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


Author Comment

ID: 23672948
Thank you.

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


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question