Use the xml configuration file to configure the column information of the DataGridView

Source: Internet
Author: User

Project requirements:

You can manage multiple types of devices in device management. The properties of each type of device are different. For example:

The column information of the fault and LTU device is different.

Solution:

1. Define the configuration information of Columns with strong types and store them in xml files.

The PropertyName corresponds to the T attribute name in List <T>, and PropertyText indicates whether the column name Visibled is displayed.

 

2. read to the cache when the project starts

T is a strongly-defined DataTable.

Read configuration files

1 /// <summary>
2 // read the configuration file
3 /// </summary>
4 /// <param name = "fileName"> </param>
5 /// <param name = "tableName"> </param>
6 /// <returns> </returns>
7 public static T GetConfigDT <T> (string fileName, string tableName) where T: DataTable, new ()
8 {
9 T configDt = new T ();
10 configDt. TableName = tableName;
11 configDt. ReadXml (fileName );
12 return configDt;
13}

 

3. to load the DataGridView data, first initialize the column information and then set the data source

This. dataGridViewX _ terminal. InitColumn ("DEV_LTU"); // initialize the column
This. dataGridViewX _ terminal. DataSource = DevTableAction. Instance. SelectLTU (); // read the data source List <t> Format

Initialize a column

1 /// <summary>
2 // initialize the column
3 /// </summary>
4 /// <param name = "dataGridView"> </param>
5 /// <param name = "dt"> </param>
6 public static void InitColumn (this DevComponents. DotNetBar. Controls. DataGridViewX dataGridView, string tableName)
7 {
8 if (ColumnConfigHelper. ColumnConfigDS. Tables. Contains (tableName ))
9 {
10 ColumnConfigDS. ColumnConfigDataTable dt = ColumnConfigHelper. ColumnConfigDS. Tables [tableName] as ColumnConfigDS. ColumnConfigDataTable;
11 dataGridView. Columns. Clear ();
12 if (dt! = Null)
13 {
14
15 dataGridView. AutoGenerateColumns = false;
16
17 List <maid> columnList = new List <maid> ();
18 foreach (ColumnConfigDS. ColumnConfigRow dr in dt. Rows)
19 {
20 if (dr. Visibled = false)
21. continue;
22 DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn ();
23 column. DataPropertyName = dr. PropertyName;
24 column. HeaderText = dr. PropertyText;
25 if (dr. Width <0)
26 column. AutoSizeMode = maid;
27 else
28 column. Width = dr. Width;
29 columnList. Add (column );
30}
31 dataGridView. Columns. AddRange (columnList. ToArray ());
32}
33}
34 else
35 {
36 MessageBox. Show ("the table does not exist" + tableName + "configuration file ");
37}
38}

 

I haven't written any essays for a long time. I always learn from others. I am so sorry.

 

 

 

 

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.