We help IT Professionals succeed at work.

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

IT_Steve
IT_Steve asked
on
Medium Priority
241 Views
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?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

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

Bob
Commented:
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
        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(myGridTableStyle)

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

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

Ask the Experts

Author

Commented:
Amyhxu -

Thank you ...that was exactly what I needed. I like this easy solution.
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.

OR

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.