开发者

Get data for DataGridView from something else than properties

I ha开发者_开发百科ve a DataGridView with a DataSource. The DataGridView auto-generates columns for each property. I want to change this, so that it gets the data for the columns in a different way.

How can I determine how the DataGridView gets the columns and values from the items in the datasource?


It probably uses reflection, do you want to intercept and modify that behavior really?

usually when the default automatic column generation is not enough I disable it and create the columns from code either statically or parsing a config file which could be extended afterwards so to have new columns or differently rendered ones without rebuilding the whole project.


I guess this all depends on what you are looking to do. If you just want to bind to specific columns, and not auto-generate the columns based off of the properties in the DataSource object, then you would do the following:

 <asp:GridView ID="GridViewID" AutoGenerateColumns="false" runat="server">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
 </asp:GridView>

With "AutoGenerateColumns" set to false, and specific columns set, the GridView will only bind to the properties, in the "DataSource" object, that are tagged in the "DataField" property of the specified columns.

Now, if you are looking to alter these data values, or specifically modify the data as it is being bound, then you would want to attach to the "RowDataBound" event. That way, when the data is bound you can alter it as it attaches to each row.


With the DataGridView.VirtualMode property, you can supply your own datastore:

Virtual mode is designed for use with very large stores of data. When the VirtualMode property is true, you create a DataGridView with a set number of rows and columns and then handle the CellValueNeeded event to populate the cells. Virtual mode requires implementation of an underlying data cache to handle the population, editing, and deletion of DataGridView cells based on actions of the user. For more information about implementing virtual mode, see How to: Implement Virtual Mode in the Windows Forms DataGridView Control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜