How do I choose which properties to display when data binding to the DataGridView?

Linking a List collection to a datagrid. How can you restrict which properties are displayed?

DataGridViewAirport.DataSource = GlobalDisplayAirports

      

0


source to share


3 answers


Disable AutoGenerateColumns and then you can explicitly create the columns you want. For example an example:

<asp:GridView ID="myGrid" runat="server" AutoGenerateColumns="False" CellSpacing="0">
    <Columns>
     <asp:BoundField DataField="Total" HeaderText="Amount" DataFormatString="{0:C}"/>

    </Columns>

      



Another option is to hide columns after data binding, but this above is the best approach.

+2


source


Depending on your data source, you can mark the properties that you do not want to show, then you can leave the autogenerate columns.

I believe this is correct. VB:

<System.ComponentModel.Browsable(false)> _

      

in C # it is



[System.ComponentModel.Browsable(false)]

      

Another useful attribute is

[DisplayName("Total Amount")]

      

0


source


We have set a motive for all of our classes to create a TailorDGV method, which we pass a datagrid reference to the method that it then returns.

This method then iterates over and hides all columns. It then turns on the required columns and either sets a fixed width or sets up an automatic system to fill the grid with the columns that are visible.

        With dgvControl
            For i As Integer = 0 To .ColumnCount - 1
                .Columns(i).Visible = False
            Next
        End With

      

You can also set the position of the column in the grid using the DisplayIndex property of the grid column object. This property is null based.

0


source







All Articles