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.