How to customize your own DataGrid

Source: Internet
Author: User
How to customize your own DataGrid
--------------------------------------------------------------------------------
Author: Hu Xiaodong
The following example shows how to customize the DataGrid, including how to bind a able to a DataGrid, and how to hide some columns in The DataGrid.
First, if you only set the DataSource attribute of the DataGrid to A able, the TableStyle of The DataGrid will be DefaultTableStyle, and its TableStyle. Count attribute will be zero. Therefore, if you want your DataGrid to have a different table style, you must add your own table style.
The following example sets the data source attribute of a DataGrid and declares a new DataGridTableStyle and four DataGridColumnStyles. This is because in _ periodData. tables [AccountingPeriodData. accountingPeriod_TABLE] has six fields, but I only want to display four, and the first one must be read-only. Of course, I want to change the Header Text of each field, so I use this function to implement it. Note: we cannot set HeaderText as a string constant. We should get it from the source file.
By the way, I declare four DataGridColumnStyle objects, but update them to DataGridTextBoxColumn, which means you can also update them to a DataGidBoolColumn. Of course you should know the reason. :-) // Initialize DataGrid when the form is opened.
Private void InitializeDataGrid ()
{
// Set the DataGrid's datasource to a DataTable
DtgPeriods. DataSource = _ periodData. Tables
[AccountingPeriodData. AccountingPeriod_TABLE];
     
// Create a DataGridTableStyle and set its MappingName is Accounting_TABLE
DataGridTableStyle mydtgTableStyle = new DataGridTableStyle ();
MydtgTableStyle. MappingName = AccountingPeriodData. AccountingPeriod_TABLE;
     
// Create maid, set their properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.