Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
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 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.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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.