none
DataColumn.Caption vs DataColumn.ColumnName RRS feed

  • Question

  • I would think intuitively that DataColumnCaption would be the string that is used to name the columns of a dataviewgrid when bound. However, in my application it seems the caption name isn't used for anything and that the ColumnName property is always used for the dataviewgrid's column headers.

    What is the use for the caption property?
    What is the difference between these two properties?

    1) If I gave coumn names in the designer what is _supposed_ to happen to the column names when you bind a datasource?
    2) When the grid is bound to a table, what is _supposed_ to happen to the column names when the table column names are changed?
    3) If no column names are ever defined in the designer or in the program, do they come from table column.columnname or table.column.caption, or niether?


    I've for two undesired behaviors in my application. One is that I get double the number of columns when binding my grid to a the dataTable instead of a set of column matching the dataTable I bound to. One set is the names I have for the grid in the designer and another is the names I gave to the columnName property of the columns for the table I am binding. The types match, so I can't figure out this behavior.

    Another is, that I cannot figure out if I should have to name the columns of the grid or just name the columns of the table.
    the grid displays two views, that have the same values types. I just need to change the column headers and can't figure out if changing table columns or grid columns or both is necesarry. For example a column might be "2009 Budget" for one view, if the user selected 2010 in a cmb box I might change the header to "2010 Budget"

    For now,
    I defined grid coulmns in the designer
    I explicitly clear the grid before binding
    I explicitly clear the table before recreating it.
    I set all the properties for the table columns one by one programitically
    I bind the table
    I set all the properties of the grid columns again.

    It fixes it, but I don't think that should be necessary, should it? Seems like I am being repitituous without cause.




    Monday, November 30, 2009 5:10 AM

Answers

  • I believe the reason you are getting double numbers of columns cause you need to tell your datagridview not to generate columns when you bind and clear columns on datagridview  

            With Me.DataGridView1
                .AutoGenerateColumns = False
                .Columns.Clear()
                .DataSource = dtProd
            End With
    you can assign your caption columns on the datatable or datagridview column headers.

            Dim TxtBoxColumn As DataGridViewTextBoxColumn
            'Populate DataGridView Columns
            TxtBoxColumn = New DataGridViewTextBoxColumn
            With TxtBoxColumn
                .DataPropertyName = dtProd.Columns("ProductName").ColumnName.ToString()
                .Name = dtProd.Columns("ProductName").ColumnName.ToString()
                .HeaderText = "Product Name"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .DefaultCellStyle.BackColor = Color.Silver
                Me.DataGridView1.Columns.Add(TxtBoxColumn)
                'AutoSize Column 
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .Visible = True
                .ReadOnly = False
            End With
    
            TxtBoxColumn = New DataGridViewTextBoxColumn
            With TxtBoxColumn
                .DataPropertyName = dtProd.Columns("CategoryName").ColumnName.ToString()
                .Name = dtProd.Columns("CategoryName").ColumnName.ToString()
                .HeaderText = "CategoryName"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .DefaultCellStyle.BackColor = Color.Silver
                Me.DataGridView1.Columns.Add(TxtBoxColumn)
                'AutoSize Column 
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .Visible = True
                .ReadOnly = True
            End With
    




    John
    Monday, November 30, 2009 11:06 AM