FixDBManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Collections;
  10. namespace Common
  11. {
  12. /// <summary>
  13. /// FixDBManager 的摘要说明。
  14. /// </summary>
  15. public sealed class FixDBManager
  16. {
  17. public FixDBManager()
  18. {
  19. //
  20. // TODO: 在此处添加构造函数逻辑
  21. //
  22. }
  23. public static object[] FixValues(object[] values)
  24. {
  25. for ( int i = 0 ;i< values.Length ;i++)
  26. {
  27. if (values[i] == null)
  28. {
  29. values[i] = DBNull.Value ;
  30. }
  31. else if(values[i].GetType().FullName == "System.String")
  32. {
  33. values[i] = ((string)values[i]).Length != 0? values[i]: DBNull.Value;
  34. }
  35. }
  36. return values;
  37. }
  38. public static string CheckNullStr(object obj)
  39. {
  40. if(obj == null || obj.GetType() == typeof(System.DBNull))
  41. {
  42. return "";
  43. }
  44. else
  45. {
  46. return Convert.ToString(obj);
  47. }
  48. }
  49. public static int CheckNullInt(object obj)
  50. {
  51. if(obj == null || obj.GetType() == typeof(System.DBNull))
  52. {
  53. return 0;
  54. }
  55. else
  56. {
  57. return Convert.ToInt32(obj);
  58. }
  59. }
  60. public static System.Decimal CheckNullDecimal(object obj)
  61. {
  62. if(obj == null || obj.GetType() == typeof(System.DBNull))
  63. {
  64. return 0;
  65. }
  66. else
  67. {
  68. return Convert.ToDecimal(obj);
  69. }
  70. }
  71. public static void Init_ComboControl(System.Windows.Forms.Control ComboControl, string TableName, string _displayMember, string _valueMember, ref System.Data.DataSet dst)
  72. {
  73. try
  74. {
  75. switch (ComboControl.GetType().ToString())
  76. {
  77. case "Infragistics.Win.UltraWinGrid.UltraCombo":
  78. if (!dst.Tables.Contains(TableName))
  79. {
  80. MessageBox.Show("绑定表:" + TableName + "不存在,不能对控件" + ComboControl.Name + "进行初始化!");
  81. return;
  82. }
  83. System.Data.DataTable newTable = dst.Tables[TableName].Copy();
  84. newTable.TableName = TableName;
  85. System.Data.DataRow newRow = newTable.NewRow();
  86. for (int i = 0; i < dst.Tables[TableName].Columns.Count; i++)
  87. {
  88. newRow[i] = "";
  89. }
  90. newTable.Rows.Add(newRow);
  91. newTable.AcceptChanges();
  92. ((Infragistics.Win.UltraWinGrid.UltraCombo)ComboControl).DataSource = new System.Data.DataView(newTable);
  93. ((Infragistics.Win.UltraWinGrid.UltraCombo)ComboControl).DisplayMember = _displayMember;
  94. ((Infragistics.Win.UltraWinGrid.UltraCombo)ComboControl).ValueMember = _valueMember;
  95. foreach (System.Data.DataColumn col in dst.Tables[TableName].Columns)
  96. {
  97. if (col.ColumnName != _displayMember)
  98. {
  99. ((Infragistics.Win.UltraWinGrid.UltraCombo)ComboControl).DisplayLayout.Bands[TableName].Columns[col.ColumnName].Hidden = true;
  100. }
  101. else
  102. {
  103. ((Infragistics.Win.UltraWinGrid.UltraCombo)ComboControl).DisplayLayout.Bands[TableName].Columns[col.ColumnName].Band.ColHeadersVisible = false;
  104. ((Infragistics.Win.UltraWinGrid.UltraCombo)ComboControl).DisplayLayout.Bands[TableName].Columns[col.ColumnName].Width = ((Infragistics.Win.UltraWinGrid.UltraCombo)ComboControl).Width;
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. catch (System.Exception ex)
  111. {
  112. System.Diagnostics.Debug.WriteLine(ex.ToString());
  113. }
  114. }
  115. public static string splitArray(object obj, char splitor)
  116. {
  117. IList checkedItems = null;
  118. string checkedItem = "";
  119. StringBuilder str = new StringBuilder();
  120. checkedItems = obj as IList;
  121. if (checkedItems != null)
  122. {
  123. for (int i = 0; i < checkedItems.Count; i++)
  124. {
  125. // Each item in the list will be the value in the row that corresponds
  126. // to the ValueMember of the combo
  127. str = str.Append(splitor).Append((string)checkedItems[i]);
  128. }
  129. }
  130. checkedItem = str.ToString();
  131. return checkedItem;
  132. }
  133. }
  134. }