Win Form DataTable Display Properties

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

    Expert Comment

    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?
    LVL 8

    Author Comment

    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 29

    Accepted Solution

    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

    LVL 8

    Author Comment

    Thank you.

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


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    C# primary key 9 46
    another way  of structuring the sqlcommand if parameter is an OUTPUT 1 21
    report c# 9 57
    Handling arrays 14 28
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now