How do I dynamically set up a DataGridTableStyle and DataGridTextBoxColumns when the number of columns from my datatable or dataview may change each time?

Posted on 2006-03-31
Last Modified: 2010-04-23
I want to eliminate Null from the cells. My research indicates I need to use DataGridTableStyle to accomplish this. But my columns may come and go depending on what data is available in my datatable or dataview. How do I dynamically do that when I seem to need a new DataGridTextBoxColumn for each column?
Question by:IT_Steve
    LVL 96

    Expert Comment

    by:Bob Learned
    You can eliminate nulls at the source, depending on what type of datasource you are binding to.

    LVL 14

    Accepted Solution

    Hi IT_Steve,

    Here's the easy way to do it without specifically setting up each DataFridTextBoxColumn:

         After you set the datasource of the datagrid, add the following code:

            Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
            myGridTableStyle.MappingName = DataGrid1.DataSource.TableName

            For Each dgtbc As DataGridTextBoxColumn In DataGrid1.TableStyles(0).GridColumnStyles
                dgtbc.NullText = ""

    Author Comment

    Amyhxu -

    Thank you ...that was exactly what I needed. I like this easy solution.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    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

    21 Experts available now in Live!

    Get 1:1 Help Now