How to customize your own DataGrid

Source: Internet
Author: User
Tags date
How to customize your own DataGrid

Author: Hu Xiaodong
The following example shows how to customize the DataGrid, including How to bind a DataTable to a DataGrid, and how to hide some columns in the DataGrid.

First, if you only set the DataSource property of the DataGrid to be a DataTable, then the TableStyle of the DataGrid will be Defaulttablestyle, and its Tablestyle.count property is zero. So, if you want your DataGrid to have a different table style, you have to add your own table style.

The following example sets the data source property of a DataGrid and then declares a new DataGridTableStyle and four datagridcolumnstyles. This is because there are six fields in _perioddata.tables [accountingperioddata.accountingperiod_table], but I want to show only four, and the first one must be read-only, Of course I want to change the header Text for each field, so I use this function to implement it. Note that we can't set HeaderText to 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 why. :-)//Initialize DataGrid when the form is opened.
private void Initializedatagrid ()
Set the DataGrid ' s datasource to a DataTable
Dtgperiods.datasource = _perioddata.tables

Create a DataGridTableStyle and set its MappingName is accounting_table
DataGridTableStyle Mydtgtablestyle = new DataGridTableStyle ();
Mydtgtablestyle.mappingname = accountingperioddata.accountingperiod_table;

Create DataGridColumnStyles, set their properties
Add these datagridcollumnstyles into
The DataGridTableStyle defined above one by one
DataGridColumnStyle Periodcodestyle = new DataGridTextBoxColumn ();
Periodcodestyle.mappingname = Accountingperioddata.period_field;
Periodcodestyle.headertext = "The time of the Year";
Periodcodestyle.readonly = true;

DataGridColumnStyle Startdatestyle = new DataGridTextBoxColumn ();
Startdatestyle.mappingname = Accountingperioddata.startdate_field;
Startdatestyle.headertext = "Opening date";

DataGridColumnStyle Enddatestyle = new DataGridTextBoxColumn ();
Enddatestyle.mappingname = Accountingperioddata.enddate_field;
Enddatestyle.headertext = "Final Date";

DataGridColumnStyle Openstyle = new DataGridTextBoxColumn ();
Openstyle.mappingname = Accountingperioddata.open_field;
Openstyle.headertext = "Do not open";

Add the DataGridTableStyle into the DataGrid ' s datasource.

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: 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.