.Net强大的列表控件XPTable .

XPTable的大名,想必C#开发的人都有所耳闻,使用下来确实强大,在表格中添加下拉列表、进度条、图标等非常容易,灵活方便。当添加大量数据时,和.Net自带的ListView对比过,速度快很多!



XPTable最重要的是开源,可根据自己的需要修改,有bug也可想办法解决。我就对其进行了若干处改进,解决了部分bug。源代码写的非常标准,架构设计也很值得借鉴,研读源代码本身就是个学习提高的过程。真心感谢将如此完美的代码公开分享的人,作为点滴回报,也将自己修改后的源码放出,供大家参考,和原作者的贡献比起来,我这点小小的修改就如沧海一粟,不足为道了。


我修改过的代码和解决的问题列示如下(下载我的源代码,在项目中搜索chenbo,就能看到修改注释):


1、...\Models\Table.cs  Line 2118,2153

解决问题:在某些情况下(任意调整窗口大小,XPTable的Anchor设置为随窗口大小自适应调整)会抛System.ArgumentOutOfRangeException异常,vScrollBar.LargeChange不能小于0


2、...\Models\Table.cs  Line 5598,5606

解决问题:在列头Resizing状态下双击鼠标,应根据该列数据中最长的一行调整当前列宽度,目前仅对TextColumn和NumberColumn有效


3、...\Models\Table.cs  Line 6134

解决问题:在列头Resizing状态下单击鼠标,避免OnMouseUp激发列宽的调整。应该双击或者调整宽度后才能激发


4、...\Models\Table.cs  Line 6373

解决问题:根据原代码,如果Table允许多选,选中多行后,点鼠标右键将自动选择鼠标所在行,修改后,多选的行依然选中

这个问题借鉴了“恶猫的尾巴”的代码:http://emao.me/tag/XpTable/,在此感谢!


5、...\Models\Table.cs  Line 6627

解决问题:鼠标在列头为Resizing图标时,移动到数据区域不会自动变为默认图标


6、...\Models\Table.cs  Line 7229

解决问题:解决列头的对齐方式始终是靠左的问题


7、...\Renderers\NumberCellRenderer.cs  Line 661

解决问题:为了实现Table.cs的函数CalColumnWidth中实现对NumberColumn列格式化数据宽度的计算


8、...  刚下载源码后即发现的问题,好像是某个函数col参数有问题,具体不记得哪个文件哪行代码了



我修改后的源代码和Dll下载链接(VS2008 .NetFramework 2.0)

原作者源代码CodeProject链接


自己写了控制类(TableCtrl.cs)来操作XPTable,使用起来更方便

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5. using System.Windows.Forms;  
  6. using System.Runtime.InteropServices;  
  7.   
  8.   
  9. using XPTable;  
  10. using XPTable.Models;  
  11. using XPTable.Editors;  
  12.   
  13.   
  14. namespace ProgramModule  
  15. {  
  16.     struct Key  
  17.     {  
  18.         public string name;  
  19.         public object value;  
  20.     };  
  21.   
  22.   
  23.     class TableKey  
  24.     {  
  25.         private List<Key> m_keys;  
  26.   
  27.   
  28.         public TableKey()  
  29.         {  
  30.             m_keys = new List<Key>();  
  31.         }  
  32.   
  33.   
  34.         public void AddKey(string name, object value)  
  35.         {  
  36.             Key key;  
  37.             key.name = name;  
  38.             key.value = value;  
  39.             m_keys.Add(key);  
  40.         }  
  41.   
  42.   
  43.         public int Count  
  44.         {  
  45.             get  
  46.             {  
  47.                 return m_keys.Count;  
  48.             }  
  49.         }  
  50.   
  51.   
  52.         public string GetKeyName(int index)  
  53.         {  
  54.             if (index < 0 || index >= Count)  
  55.             {  
  56.                 return "";  
  57.             }  
  58.             else  
  59.             {  
  60.                 return m_keys[index].name;  
  61.             }  
  62.         }  
  63.   
  64.   
  65.         public object GetKeyValue(int index)  
  66.         {  
  67.             if (index < 0 || index >= Count)  
  68.             {  
  69.                 return null;  
  70.             }  
  71.             else  
  72.             {  
  73.                 return m_keys[index].value;  
  74.             }  
  75.         }  
  76.     };  
  77.   
  78.   
  79.     static class TableCtrl  
  80.     {  
  81.         const int COLOR_WINDOW = 5;  
  82.   
  83.   
  84.         [DllImport("user32.dll", CharSet = CharSet.Auto)]  
  85.         private static extern int GetSysColor(int nIndex);  
  86.   
  87.   
  88.         public static Color GetSysWindowColor()  
  89.         {  
  90.             Color color;  
  91.   
  92.   
  93.             int iColor = GetSysColor(COLOR_WINDOW);  
  94.             color = Color.FromArgb(255, Color.FromArgb(iColor));  
  95.             return color;  
  96.         }  
  97.   
  98.   
  99.         public static void InitTable(ref Table table)  
  100.         {  
  101.             try  
  102.             {  
  103.                 table.NoItemsText = "";  
  104.                 table.GridLines = GridLines.Both;  
  105.                 table.ColumnModel = new ColumnModel();  
  106.                 table.TableModel = new TableModel();  
  107.                 table.TableModel.RowHeight = 18;  
  108.   
  109.   
  110.                 TextColumn textColKey = new TextColumn("key", 70);  
  111.                 textColKey.Visible = false;  
  112.                 table.ColumnModel.Columns.Add(textColKey);  
  113.   
  114.   
  115.                 table.FullRowSelect = true;  
  116.                 table.HideSelection = false;  
  117.   
  118.   
  119.                 table.BackColor = GetSysWindowColor();  
  120.                 table.SortedColumnBackColor = Color.FromArgb(100, Color.WhiteSmoke);  
  121.             }  
  122.             catch (System.Exception ex)  
  123.             {  
  124.                 GlobalFunction.MsgBoxException(ex.Message, "InitTable");  
  125.             }  
  126.         }  
  127.   
  128.   
  129.         public static void AddColumn(ref Table table, Column column)  
  130.         {  
  131.             column.Editable = false;  
  132.             column.Sortable = true;  
  133.             table.ColumnModel.Columns.Add(column);  
  134.         }  
  135.   
  136.   
  137.         public static void AddColumn(ref Table table, Column column, bool editable, bool sortable)  
  138.         {  
  139.             column.Editable = editable;  
  140.             column.Sortable = sortable;  
  141.             table.ColumnModel.Columns.Add(column);  
  142.         }  
  143.   
  144.   
  145.         public static void AddColumn(ref Table table, Column column, bool editable, bool sortable, ColumnAlignment alignment)  
  146.         {  
  147.             column.Editable = editable;  
  148.             column.Sortable = sortable;  
  149.             column.Alignment = alignment;  
  150.             table.ColumnModel.Columns.Add(column);  
  151.         }  
  152.   
  153.   
  154.         public static void AddNumColumn(ref Table table, NumberColumn column, bool editable, bool sortable, bool showUpDownButton, double minValue, double MaxValue, string format, ColumnAlignment alignment)  
  155.         {  
  156.             column.Editable = editable;  
  157.             column.Sortable = sortable;  
  158.             column.ShowUpDownButtons = showUpDownButton;  
  159.             column.Minimum = Convert.ToDecimal(minValue);  
  160.             column.Maximum = Convert.ToDecimal(MaxValue);  
  161.             column.Format = format;  
  162.             column.Alignment = alignment;  
  163.             table.ColumnModel.Columns.Add(column);  
  164.         }  
  165.   
  166.   
  167.         //public static int AddNewRow(ref Table table)   
  168.         //{   
  169.         //    Row row = new Row();   
  170.         //    for (int i = 0; i < table.ColumnModel.Columns.Count; i++ )   
  171.         //    {   
  172.         //        row.Cells.Add(new Cell());   
  173.         //    }   
  174.         //    return table.TableModel.Rows.Add(row);   
  175.         //}   
  176.   
  177.   
  178.         public static Row AddNewRow(ref Table table)  
  179.         {  
  180.             try  
  181.             {  
  182.                 Row row = new Row();  
  183.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  184.                 {  
  185.                     row.Cells.Add(new Cell());  
  186.                 }  
  187.                 table.TableModel.Rows.Add(row);  
  188.                 return row;  
  189.             }  
  190.             catch (System.Exception e)  
  191.             {  
  192.                 GlobalFunction.MsgBoxException(e.Message, "AddNewRow");  
  193.                 return null;  
  194.             }  
  195.         }  
  196.   
  197.   
  198.         public static Row AddNewRow(ref Table table, string key)  
  199.         {  
  200.             try  
  201.             {  
  202.                 foreach (Row row1 in table.TableModel.Rows)  
  203.                 {  
  204.                     if (row1.Cells[0].Text == key)  
  205.                     {  
  206.                         GlobalFunction.MsgBoxError("key is not unique");  
  207.                         return null;  
  208.                     }  
  209.                 }  
  210.   
  211.   
  212.                 Row row = new Row();  
  213.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  214.                 {  
  215.                     row.Cells.Add(new Cell());  
  216.                 }  
  217.                 row.Cells[0].Text = key;  
  218.                 table.TableModel.Rows.Add(row);  
  219.                 return row;  
  220.             }  
  221.             catch (System.Exception e)  
  222.             {  
  223.                 GlobalFunction.MsgBoxException(e.Message, "AddNewRow");  
  224.                 return null;  
  225.             }  
  226.         }  
  227.   
  228.   
  229.         public static void RemoveRow(ref Table table, int rowIndex)  
  230.         {  
  231.             try  
  232.             {  
  233.                 table.TableModel.Rows.RemoveAt(rowIndex);  
  234.             }  
  235.             catch (System.Exception e)  
  236.             {  
  237.                 MessageBox.Show("RemoveRow:" + e.Message);  
  238.             }  
  239.         }  
  240.   
  241.   
  242.         public static void RemoveRow(ref Table table, Row row)  
  243.         {  
  244.             try  
  245.             {  
  246.                 table.TableModel.Rows.Remove(row);  
  247.             }  
  248.             catch (System.Exception e)  
  249.             {  
  250.                 GlobalFunction.MsgBoxException(e.Message, "RemoveRow");  
  251.             }  
  252.         }  
  253.   
  254.   
  255.         public static void RemoveRowAll(ref Table table)  
  256.         {  
  257.             try  
  258.             {  
  259.                 table.TableModel.Rows.Clear();  
  260.             }  
  261.             catch (System.Exception e)  
  262.             {  
  263.                 MessageBox.Show("RemoveRowAll:" + e.Message);  
  264.             }  
  265.         }  
  266.   
  267.   
  268.         public static void SetTableValue(ref Table table, int rowIndex, string colName, object value)  
  269.         {  
  270.             try  
  271.             {  
  272.                 Column targetCol = null;  
  273.                 int colIndex = -1;  
  274.   
  275.   
  276.                 if (rowIndex < 0 || rowIndex >= table.TableModel.Rows.Count)  
  277.                 {  
  278.                     return;  
  279.                 }  
  280.   
  281.   
  282.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  283.                 {  
  284.                     if (table.ColumnModel.Columns[i].Text == colName)  
  285.                     {  
  286.                         targetCol = table.ColumnModel.Columns[i];  
  287.                         colIndex = i;  
  288.                         break;  
  289.                     }  
  290.                 }  
  291.   
  292.   
  293.                 if (colIndex == -1)  
  294.                 {  
  295.                     return;  
  296.                 }  
  297.   
  298.   
  299.                 if (targetCol is TextColumn || targetCol is ComboBoxColumn || targetCol is ButtonColumn)  
  300.                 {  
  301.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Text = value.ToString();  
  302.                 }  
  303.                 else if (targetCol is CheckBoxColumn)  
  304.                 {  
  305.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Checked = (bool)value;  
  306.                 }  
  307.                 else if (targetCol is ImageColumn)  
  308.                 {  
  309.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Image = (Image)value;  
  310.                 }  
  311.                 else if (targetCol is NumberColumn)  
  312.                 {  
  313.                     if (GlobalFunction.IsNumeric(value.ToString()))  
  314.                     {  
  315.                         table.TableModel.Rows[rowIndex].Cells[colIndex].Data = Convert.ToDouble(value);  
  316.                     }  
  317.                 }  
  318.                 else  
  319.                 {  
  320.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Data = value;  
  321.                 }  
  322.             }  
  323.             catch (System.Exception e)  
  324.             {  
  325.                 GlobalFunction.MsgBoxException(e.Message, "SetTableValue");  
  326.             }  
  327.         }  
  328.   
  329.   
  330.         public static void SetTableValue(ref Table table, int rowIndex, int colIndex, object value)  
  331.         {  
  332.             try  
  333.             {  
  334.                 if (colIndex <= 0)  
  335.                 {  
  336.                     return;  
  337.                 }  
  338.   
  339.   
  340.                 Column targetCol = table.ColumnModel.Columns[colIndex];   
  341.   
  342.   
  343.                 if (targetCol is TextColumn || targetCol is ComboBoxColumn || targetCol is ButtonColumn)  
  344.                 {  
  345.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Text = value.ToString();  
  346.                 }  
  347.                 else if (targetCol is CheckBoxColumn)  
  348.                 {  
  349.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Checked = (bool)value;  
  350.                 }  
  351.                 else if (targetCol is ImageColumn)  
  352.                 {  
  353.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Image = (Image)value;  
  354.                 }  
  355.                 else if (targetCol is NumberColumn)  
  356.                 {  
  357.                     if (GlobalFunction.IsNumeric(value.ToString()))  
  358.                     {  
  359.                         table.TableModel.Rows[rowIndex].Cells[colIndex].Data = Convert.ToDouble(value);  
  360.                     }  
  361.                 }  
  362.                 else  
  363.                 {  
  364.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Data = value;  
  365.                 }  
  366.             }  
  367.             catch (System.Exception e)  
  368.             {  
  369.                 GlobalFunction.MsgBoxException(e.Message, "SetTableValue");  
  370.             }  
  371.         }  
  372.   
  373.   
  374.         public static void SetTableValue(ref Table table, Row row, string colName, object value)  
  375.         {  
  376.             try  
  377.             {  
  378.                 Column targetCol = null;  
  379.                 int colIndex = -1;  
  380.   
  381.   
  382.                 if (row == null)  
  383.                 {  
  384.                     return;  
  385.                 }  
  386.   
  387.   
  388.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  389.                 {  
  390.                     if (table.ColumnModel.Columns[i].Text == colName)  
  391.                     {  
  392.                         targetCol = table.ColumnModel.Columns[i];  
  393.                         colIndex = i;  
  394.                         break;  
  395.                     }  
  396.                 }  
  397.   
  398.   
  399.                 if (colIndex == -1)  
  400.                 {  
  401.                     return;  
  402.                 }  
  403.   
  404.   
  405.                 if (targetCol is TextColumn || targetCol is ComboBoxColumn || targetCol is ButtonColumn)  
  406.                 {  
  407.                     row.Cells[colIndex].Text = value.ToString();  
  408.                 }  
  409.                 else if (targetCol is CheckBoxColumn)  
  410.                 {  
  411.                     row.Cells[colIndex].Checked = (bool)value;  
  412.                 }  
  413.                 else if (targetCol is ImageColumn)  
  414.                 {  
  415.                     row.Cells[colIndex].Image = (Image)value;  
  416.                 }  
  417.                 else if (targetCol is NumberColumn)  
  418.                 {  
  419.                     string val = "";  
  420.   
  421.   
  422.                     if (GlobalFunction.FormatNumber(value.ToString(), 8, ref val))  
  423.                     {  
  424.                         row.Cells[colIndex].Data = Convert.ToDouble(val);  
  425.   
  426.   
  427.                     }  
  428.                     //if (GlobalFunction.IsNumeric(value.ToString()))   
  429.                     //{   
  430.                     //}   
  431.                 }  
  432.                 else  
  433.                 {  
  434.                     row.Cells[colIndex].Data = value;  
  435.                 }  
  436.             }  
  437.             catch (System.Exception e)  
  438.             {  
  439.                 GlobalFunction.MsgBoxException(e.Message, "SetTableValue");  
  440.             }  
  441.   
  442.   
  443.         }  
  444.   
  445.   
  446.         public static void SetTableValueCheckbox(ref Table table, int rowIndex, string colName, string text, bool check)  
  447.         {  
  448.             try  
  449.             {  
  450.                 Column targetCol = null;  
  451.                 int colIndex = -1;  
  452.   
  453.   
  454.                 if (rowIndex < 0 || rowIndex >= table.TableModel.Rows.Count)  
  455.                 {  
  456.                     return;  
  457.                 }  
  458.   
  459.   
  460.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  461.                 {  
  462.                     if (table.ColumnModel.Columns[i].Text == colName)  
  463.                     {  
  464.                         targetCol = table.ColumnModel.Columns[i];  
  465.                         colIndex = i;  
  466.                         break;  
  467.                     }  
  468.                 }  
  469.   
  470.   
  471.                 if (colIndex == -1)  
  472.                 {  
  473.                     return;  
  474.                 }  
  475.   
  476.   
  477.                 if (targetCol is CheckBoxColumn)  
  478.                 {  
  479.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Text = text;  
  480.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Checked = check;  
  481.                 }  
  482.             }  
  483.             catch (System.Exception e)  
  484.             {  
  485.                 GlobalFunction.MsgBoxException(e.Message, "SetTableValueCheckbox");  
  486.             }  
  487.   
  488.   
  489.         }  
  490.   
  491.   
  492.         public static void SetTableValueCheckbox(ref Table table, Row row, string colName, string text, bool check)  
  493.         {  
  494.             try  
  495.             {  
  496.                 Column targetCol = null;  
  497.                 int colIndex = -1;  
  498.   
  499.   
  500.                 if (row == null)  
  501.                 {  
  502.                     return;  
  503.                 }  
  504.   
  505.   
  506.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  507.                 {  
  508.                     if (table.ColumnModel.Columns[i].Text == colName)  
  509.                     {  
  510.                         targetCol = table.ColumnModel.Columns[i];  
  511.                         colIndex = i;  
  512.                         break;  
  513.                     }  
  514.                 }  
  515.   
  516.   
  517.                 if (colIndex == -1)  
  518.                 {  
  519.                     return;  
  520.                 }  
  521.   
  522.   
  523.                 if (targetCol is CheckBoxColumn)  
  524.                 {  
  525.                     row.Cells[colIndex].Text = text;  
  526.                     row.Cells[colIndex].Checked = check;  
  527.                 }  
  528.             }  
  529.             catch (System.Exception e)  
  530.             {  
  531.                 GlobalFunction.MsgBoxException(e.Message, "SetTableValueCheckbox");  
  532.             }  
  533.         }  
  534.   
  535.   
  536.         public static void SetTableValueImage(ref Table table, int rowIndex, string colName, string text, Image image)  
  537.         {  
  538.             try  
  539.             {  
  540.                 Column targetCol = null;  
  541.                 int colIndex = -1;  
  542.   
  543.   
  544.                 if (rowIndex < 0 || rowIndex >= table.TableModel.Rows.Count)  
  545.                 {  
  546.                     return;  
  547.                 }  
  548.   
  549.   
  550.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  551.                 {  
  552.                     if (table.ColumnModel.Columns[i].Text == colName)  
  553.                     {  
  554.                         targetCol = table.ColumnModel.Columns[i];  
  555.                         colIndex = i;  
  556.                         break;  
  557.                     }  
  558.                 }  
  559.   
  560.   
  561.                 if (colIndex == -1)  
  562.                 {  
  563.                     return;  
  564.                 }  
  565.   
  566.   
  567.                 if (targetCol is ImageColumn)  
  568.                 {  
  569.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Text = text;  
  570.                     table.TableModel.Rows[rowIndex].Cells[colIndex].Image = image;  
  571.                 }  
  572.             }  
  573.             catch (System.Exception e)  
  574.             {  
  575.                 GlobalFunction.MsgBoxException(e.Message, "SetTableValueImage");  
  576.             }  
  577.         }  
  578.   
  579.   
  580.         public static void SetTableValueImage(ref Table table, Row row, string colName, string text, Image image)  
  581.         {  
  582.             try  
  583.             {  
  584.                 Column targetCol = null;  
  585.                 int colIndex = -1;  
  586.   
  587.   
  588.                 if (row == null)  
  589.                 {  
  590.                     return;  
  591.                 }  
  592.   
  593.   
  594.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  595.                 {  
  596.                     if (table.ColumnModel.Columns[i].Text == colName)  
  597.                     {  
  598.                         targetCol = table.ColumnModel.Columns[i];  
  599.                         colIndex = i;  
  600.                         break;  
  601.                     }  
  602.                 }  
  603.   
  604.   
  605.                 if (colIndex == -1)  
  606.                 {  
  607.                     return;  
  608.                 }  
  609.   
  610.   
  611.                 if (targetCol is ImageColumn)  
  612.                 {  
  613.                     row.Cells[colIndex].Text = text;  
  614.                     row.Cells[colIndex].Image = image;  
  615.                 }  
  616.             }  
  617.             catch (System.Exception e)  
  618.             {  
  619.                 GlobalFunction.MsgBoxException(e.Message, "SetTableValueImage");  
  620.             }  
  621.         }  
  622.   
  623.   
  624.         public static object GetTableValue(Table table, int row, int col)  
  625.         {  
  626.             try  
  627.             {  
  628.                 Column targetCol = null;  
  629.                 int colIndex = -1;  
  630.   
  631.   
  632.                 if (row < 0 || row >= table.TableModel.Rows.Count)  
  633.                 {  
  634.                     return null;  
  635.                 }  
  636.   
  637.   
  638.                 if (col < 0 || col >= table.ColumnModel.Columns.Count)  
  639.                 {  
  640.                     return null;  
  641.                 }  
  642.   
  643.   
  644.                 targetCol = table.ColumnModel.Columns[col];  
  645.                 colIndex = col;  
  646.   
  647.   
  648.                 if (colIndex == -1)  
  649.                 {  
  650.                     return null;  
  651.                 }  
  652.   
  653.   
  654.                 if (targetCol is TextColumn || targetCol is ComboBoxColumn || targetCol is ButtonColumn)  
  655.                 {  
  656.                     if (table.TableModel.Rows[row].Cells[colIndex].Text == null)  
  657.                     {  
  658.                         return "";  
  659.                     }  
  660.                     else  
  661.                     {  
  662.                         return table.TableModel.Rows[row].Cells[colIndex].Text;  
  663.                     }  
  664.   
  665.   
  666.                 }  
  667.                 else if (targetCol is CheckBoxColumn)  
  668.                 {  
  669.                     return table.TableModel.Rows[row].Cells[colIndex].Checked;  
  670.                 }  
  671.                 else  
  672.                 {  
  673.                     return table.TableModel.Rows[row].Cells[colIndex].Data;  
  674.                 }  
  675.             }  
  676.             catch (System.Exception e)  
  677.             {  
  678.                 GlobalFunction.MsgBoxException(e.Message, "GetTableValue");  
  679.                 return null;  
  680.             }  
  681.         }  
  682.   
  683.   
  684.         public static string GetTableValueString(Table table, int row, int col)  
  685.         {  
  686.             try  
  687.             {  
  688.                 Column targetCol = null;  
  689.                 int colIndex = -1;  
  690.   
  691.   
  692.                 if (row < 0 || row >= table.TableModel.Rows.Count)  
  693.                 {  
  694.                     return "";  
  695.                 }  
  696.   
  697.   
  698.                 if (col < 0 || col >= table.ColumnModel.Columns.Count)  
  699.                 {  
  700.                     return "";  
  701.                 }  
  702.   
  703.   
  704.                 targetCol = table.ColumnModel.Columns[col];  
  705.                 colIndex = col;  
  706.   
  707.   
  708.                 if (colIndex == -1)  
  709.                 {  
  710.                     return "";  
  711.                 }  
  712.   
  713.   
  714.                 if (targetCol is TextColumn || targetCol is ComboBoxColumn || targetCol is ButtonColumn)  
  715.                 {  
  716.                     if (table.TableModel.Rows[row].Cells[colIndex].Text == null)  
  717.                     {  
  718.                         return "";  
  719.                     }  
  720.                     else  
  721.                     {  
  722.                         return table.TableModel.Rows[row].Cells[colIndex].Text;  
  723.                     }  
  724.   
  725.   
  726.                 }  
  727.                 else if (targetCol is CheckBoxColumn)  
  728.                 {  
  729.                     return table.TableModel.Rows[row].Cells[colIndex].Checked.ToString();  
  730.                 }  
  731.                 else  
  732.                 {  
  733.                     return table.TableModel.Rows[row].Cells[colIndex].Data.ToString();  
  734.                 }  
  735.             }  
  736.             catch (System.Exception e)  
  737.             {  
  738.                 GlobalFunction.MsgBoxException(e.Message, "GetTableValueString");  
  739.                 return "";  
  740.             }  
  741.         }  
  742.   
  743.   
  744.         public static object GetTableValue(Table table, int row, string colName)  
  745.         {  
  746.             try  
  747.             {  
  748.                 Column targetCol = null;  
  749.                 int colIndex = -1;  
  750.   
  751.   
  752.                 if (row < 0 || row >= table.TableModel.Rows.Count)  
  753.                 {  
  754.                     return null;  
  755.                 }  
  756.   
  757.   
  758.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  759.                 {  
  760.                     if (table.ColumnModel.Columns[i].Text == colName)  
  761.                     {  
  762.                         targetCol = table.ColumnModel.Columns[i];  
  763.                         colIndex = i;  
  764.                         break;  
  765.                     }  
  766.                 }  
  767.   
  768.   
  769.                 if (colIndex == -1)  
  770.                 {  
  771.                     return null;  
  772.                 }  
  773.   
  774.   
  775.                 if (targetCol is TextColumn || targetCol is ComboBoxColumn || targetCol is ButtonColumn)  
  776.                 {  
  777.                     if (table.TableModel.Rows[row].Cells[colIndex].Text == null)  
  778.                     {  
  779.                         return "";  
  780.                     }  
  781.                     else  
  782.                     {  
  783.                         return table.TableModel.Rows[row].Cells[colIndex].Text;  
  784.                     }  
  785.   
  786.   
  787.                 }  
  788.                 else if (targetCol is CheckBoxColumn)  
  789.                 {  
  790.                     return table.TableModel.Rows[row].Cells[colIndex].Checked;  
  791.                 }  
  792.                 else  
  793.                 {  
  794.                     return table.TableModel.Rows[row].Cells[colIndex].Data;  
  795.                 }  
  796.             }  
  797.             catch (System.Exception e)  
  798.             {  
  799.                 GlobalFunction.MsgBoxException(e.Message, "GetTableValue");  
  800.                 return null;  
  801.             }  
  802.         }  
  803.   
  804.   
  805.         public static string GetTableValueString(Table table, int row, string colName)  
  806.         {  
  807.             try  
  808.             {  
  809.                 Column targetCol = null;  
  810.                 int colIndex = -1;  
  811.   
  812.   
  813.                 if (row < 0 || row >= table.TableModel.Rows.Count)  
  814.                 {  
  815.                     return "";  
  816.                 }  
  817.   
  818.   
  819.                 for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  820.                 {  
  821.                     if (table.ColumnModel.Columns[i].Text == colName)  
  822.                     {  
  823.                         targetCol = table.ColumnModel.Columns[i];  
  824.                         colIndex = i;  
  825.                         break;  
  826.                     }  
  827.                 }  
  828.   
  829.   
  830.                 if (colIndex == -1)  
  831.                 {  
  832.                     return "";  
  833.                 }  
  834.   
  835.   
  836.                 if (targetCol is TextColumn || targetCol is ComboBoxColumn || targetCol is ButtonColumn)  
  837.                 {  
  838.                     if (table.TableModel.Rows[row].Cells[colIndex].Text == null)  
  839.                     {  
  840.                         return "";  
  841.                     }  
  842.                     else  
  843.                     {  
  844.                         return table.TableModel.Rows[row].Cells[colIndex].Text;  
  845.                     }  
  846.   
  847.   
  848.                 }  
  849.                 else if (targetCol is CheckBoxColumn)  
  850.                 {  
  851.                     return table.TableModel.Rows[row].Cells[colIndex].Checked.ToString();  
  852.                 }  
  853.                 else  
  854.                 {  
  855.                     return table.TableModel.Rows[row].Cells[colIndex].Data.ToString();  
  856.                 }  
  857.             }  
  858.             catch (System.Exception e)  
  859.             {  
  860.                 GlobalFunction.MsgBoxException(e.Message, "GetTableValueString");  
  861.                 return "";  
  862.             }  
  863.         }  
  864.   
  865.   
  866.         public static Row GetTableRowByKey(ref Table table, string key)  
  867.         {  
  868.             try  
  869.             {  
  870.                 foreach (Row row in table.TableModel.Rows)  
  871.                 {  
  872.                     if (row.Cells[0].Text == key)  
  873.                     {  
  874.                         return row;  
  875.                     }  
  876.                 }  
  877.   
  878.   
  879.                 return null;  
  880.             }  
  881.             catch (System.Exception e)  
  882.             {  
  883.                 GlobalFunction.MsgBoxException(e.Message, "GetTableRowByKey");  
  884.                 return null;  
  885.             }  
  886.         }  
  887.   
  888.   
  889.         public static int GetTableRowByKey(ref Table table, TableKey keys)  
  890.         {  
  891.             try  
  892.             {  
  893.                 int i, j;  
  894.                 int row = -1;  
  895.                 int keyCount;  
  896.   
  897.   
  898.                 keyCount = keys.Count;  
  899.                 if (keyCount <= 0)  
  900.                 {  
  901.                     return -1;  
  902.                 }  
  903.                 for (i = 0; i < table.TableModel.Rows.Count; i++)  
  904.                 {  
  905.                     for (j = 0; j < keyCount; j++)  
  906.                     {  
  907.                         if (!object.Equals(GetTableValue(table, i, keys.GetKeyName(j)), keys.GetKeyValue(j)))  
  908.                         {  
  909.                             break;  
  910.                         }  
  911.                     }  
  912.                     if (j == keyCount)  
  913.                     {  
  914.                         row = i;  
  915.                         break;  
  916.                     }  
  917.                 }  
  918.   
  919.   
  920.                 return row;  
  921.             }  
  922.             catch (System.Exception e)  
  923.             {  
  924.                 GlobalFunction.MsgBoxException(e.Message, "GetTableRowByKey");  
  925.                 return -1;  
  926.             }  
  927.         }  
  928.   
  929.   
  930.         public static int GetColumnIndexByName(Table table, string strColName)  
  931.         {  
  932.             try  
  933.             {  
  934.                 if (table.ColumnModel.Columns.Count <= 0)  
  935.                 {  
  936.                     return -1;  
  937.                 }  
  938.                 else  
  939.                 {  
  940.                     for (int i = 0; i < table.ColumnModel.Columns.Count; i++)  
  941.                     {  
  942.                         if (table.ColumnModel.Columns[i].Text == strColName)  
  943.                         {  
  944.                             return i;  
  945.                         }  
  946.                     }  
  947.   
  948.   
  949.                     return -1;  
  950.                 }  
  951.             }  
  952.             catch (System.Exception ex)  
  953.             {  
  954.                 GlobalFunction.MsgBoxException(ex.Message, "GetColumnIndexByName");  
  955.                 return -1;  
  956.             }  
  957.         }  
  958.   
  959.   
  960.         public static void SetCellBackColor(ref Table table, int row, string strColName, Color color)  
  961.         {  
  962.             try  
  963.             {  
  964.                 if (row >= table.TableModel.Rows.Count || row < 0)  
  965.                 {  
  966.                     return;  
  967.                 }  
  968.   
  969.   
  970.                 int col = GetColumnIndexByName(table, strColName);  
  971.                 if (col == -1)  
  972.                 {  
  973.                     return;  
  974.                 }  
  975.                 table.TableModel.Rows[row].Cells[col].BackColor = color;  
  976.             }  
  977.             catch (System.Exception ex)  
  978.             {  
  979.                 GlobalFunction.MsgBoxException(ex.Message, "SetCellBackColor");  
  980.   
  981.   
  982.             }  
  983.         }  
  984.   
  985.   
  986.         public static void SetRowBackColor(ref Table table, int row, Color color)  
  987.         {  
  988.             try  
  989.             {  
  990.                 if (row >= table.TableModel.Rows.Count || row < 0)  
  991.                 {  
  992.                     return;  
  993.                 }  
  994.   
  995.   
  996.                 table.TableModel.Rows[row].BackColor = color;  
  997.                 //for (int col = 0; col < table.ColumnModel.Columns.Count; col++)   
  998.                 //{   
  999.                 //    table.TableModel.Rows[row].Cells[col].BackColor = color;   
  1000.                 //}   
  1001.             }  
  1002.             catch (System.Exception ex)  
  1003.             {  
  1004.                 GlobalFunction.MsgBoxException(ex.Message, "SetRowBackColor");  
  1005.   
  1006.   
  1007.             }  
  1008.         }  
  1009.   
  1010.   
  1011.         public static void SetCellForeColor(ref Table table, int row, string strColName, Color color)  
  1012.         {  
  1013.             try  
  1014.             {  
  1015.                 if (row >= table.TableModel.Rows.Count || row < 0)  
  1016.                 {  
  1017.                     return;  
  1018.                 }  
  1019.   
  1020.   
  1021.                 int col = GetColumnIndexByName(table, strColName);  
  1022.                 if (col == -1)  
  1023.                 {  
  1024.                     return;  
  1025.                 }  
  1026.                 table.TableModel.Rows[row].Cells[col].ForeColor = color;  
  1027.             }  
  1028.             catch (System.Exception ex)  
  1029.             {  
  1030.                 GlobalFunction.MsgBoxException(ex.Message, "SetCellForeColor");  
  1031.   
  1032.   
  1033.             }  
  1034.         }  
  1035.   
  1036.   
  1037.         public static void SetRowForeColor(ref Table table, int row, Color color)  
  1038.         {  
  1039.             try  
  1040.             {  
  1041.                 if (row >= table.TableModel.Rows.Count || row < 0)  
  1042.                 {  
  1043.                     return;  
  1044.                 }  
  1045.   
  1046.   
  1047.                 table.TableModel.Rows[row].ForeColor = color;  
  1048.                 //for (int col = 0; col < table.ColumnModel.Columns.Count; col++)   
  1049.                 //{   
  1050.                 //    table.TableModel.Rows[row].Cells[col].ForeColor = color;   
  1051.                 //}   
  1052.             }  
  1053.             catch (System.Exception ex)  
  1054.             {  
  1055.                 GlobalFunction.MsgBoxException(ex.Message, "SetRowForeColor");  
  1056.   
  1057.   
  1058.             }  
  1059.         }  
  1060.   
  1061.   
  1062.         public static void SetColumnBackColor(ref Table table, string strColName, Color color)  
  1063.         {  
  1064.             try  
  1065.             {  
  1066.                 if (table.TableModel.Rows.Count <= 0)  
  1067.                 {  
  1068.                     return;  
  1069.                 }  
  1070.   
  1071.   
  1072.                 int col = GetColumnIndexByName(table, strColName);  
  1073.                 if (col == -1)  
  1074.                 {  
  1075.                     return;  
  1076.                 }  
  1077.                 foreach (Row row in table.TableModel.Rows)  
  1078.                 {  
  1079.                     row.Cells[col].BackColor = color;  
  1080.                 }  
  1081.             }  
  1082.             catch (System.Exception ex)  
  1083.             {  
  1084.                 GlobalFunction.MsgBoxException(ex.Message, "SetColumnBackColor");  
  1085.   
  1086.   
  1087.             }  
  1088.         }  
  1089.   
  1090.   
  1091.         public static void SetColumnForeColor(ref Table table, string strColName, Color color)  
  1092.         {  
  1093.             try  
  1094.             {  
  1095.                 if (table.TableModel.Rows.Count <= 0)  
  1096.                 {  
  1097.                     return;  
  1098.                 }  
  1099.   
  1100.   
  1101.                 int col = GetColumnIndexByName(table, strColName);  
  1102.                 if (col == -1)  
  1103.                 {  
  1104.                     return;  
  1105.                 }  
  1106.                 foreach (Row row in table.TableModel.Rows)  
  1107.                 {  
  1108.                     row.Cells[col].ForeColor = color;  
  1109.                 }  
  1110.             }  
  1111.             catch (System.Exception ex)  
  1112.             {  
  1113.                 GlobalFunction.MsgBoxException(ex.Message, "SetColumnForeColor");  
  1114.             }  
  1115.         }  
  1116.   
  1117.   
  1118.         public static void SelectRow(ref Table table, int rowIndex)  
  1119.         {  
  1120.             try  
  1121.             {  
  1122.                 if (rowIndex >= 0 && rowIndex < table.TableModel.Rows.Count)  
  1123.                 {  
  1124.                     table.TableModel.Selections.SelectCells(rowIndex, 0, rowIndex, table.ColumnModel.Columns.Count - 1);  
  1125.                 }  
  1126.             }  
  1127.             catch (System.Exception ex)  
  1128.             {  
  1129.                 GlobalFunction.MsgBoxException(ex.Message, "SelectRow");  
  1130.             }  
  1131.         }  
  1132.   
  1133.   
  1134.         public static void Sort(ref Table table, string strColName, SortOrder order, bool stable)  
  1135.         {  
  1136.             try  
  1137.             {  
  1138.                 if (table.TableModel.Rows.Count <= 0)  
  1139.                 {  
  1140.                     return;  
  1141.                 }  
  1142.   
  1143.   
  1144.                 int col = GetColumnIndexByName(table, strColName);  
  1145.                 if (col == -1)  
  1146.                 {  
  1147.                     return;  
  1148.                 }  
  1149.                 else  
  1150.                 {  
  1151.                     table.Sort(col, order, stable);  
  1152.                 }  
  1153.             }  
  1154.             catch (System.Exception ex)  
  1155.             {  
  1156.                 GlobalFunction.MsgBoxException(ex.Message, "Sort");  
  1157.             }  
  1158.         }  
  1159.     }  
  1160. }  
XPTable的大名,想必C#开发的人都有所耳闻,使用下来确实强大,在表格中添加下拉列表、进度条、图标等非常容易,灵活方便。 XPTable最重要的是开源,可根据自己的需要修改,有bug也可想办法解决,我就对其进行了若干处改进,使其更好用了。源代码写的非常标准,架构设计也很值得借鉴,研读源代码也是个学习提高的过程。真心感谢将如此完美的代码公开分享的人,最为点滴回报,也将自己修改后的源码放出,供大家参考,和原作者的贡献比起来,我这点小小的修改就如沧海一粟,不足为道了。 我修改过的代码和解决的问题列示如下: 1、...\Models\Table.cs Line 2118,2153 解决问题:否则在某些情况下(任意调整窗口大小,XPTable的Anchor设置为随窗口大小自适应调整)会抛System.ArgumentOutOfRangeException异常,vScrollBar.LargeChange不能小于0 2、...\Models\Table.cs Line 5598,5606 解决问题:在列头Resizing状态下双击鼠标,应根据该列数据中最长的一行调整当前列宽度,目前仅对TextColumn和NumberColumn有效 3、...\Models\Table.cs Line 6134 解决问题:在列头Resizing状态下单击鼠标,避免OnMouseUp激发列宽的调整。应该双击或者调整宽度后才能激发 4、...\Models\Table.cs Line 6373 解决问题:根据原代码,如果Table允许多选,选中多行后,点鼠标右键将自动选择鼠标所在行,修改后,多选的行依然选中 这个问题借鉴了“恶猫的尾巴”的代码:http://emao.me/tag/XpTable/,在此感谢! 5、...\Models\Table.cs Line 6627 解决问题:鼠标在列头为Resizing图标时,移动到数据区域不会自动变为默认图标 6、...\Models\Table.cs Line 7229 解决问题:解决列头的对齐方式始终是靠左的问题 7、...\Renderers\NumberCellRenderer.cs Line 661 解决问题:为了实现Table.cs的函数CalColumnWidth中实现对NumberColumn列格式化数据宽度的计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值