PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSWinFormDataGridView/CustomDataGridViewColumn/MainForm.cs

#
C# | 256 lines | 169 code | 39 blank | 48 comment | 11 complexity | 0ad889bfb1991b0ff933717db2c32b72 MD5 | raw file
  1. /************************************* Module Header **************************************\
  2. * Module Name: MaskedTextBoxColumn.cs
  3. * Project: CSWinFormDataGridView
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * This sample demonstrates the use of custom column definitions within the Windows Forms
  7. * DataGridView control.
  8. *
  9. * The Employee ID, SSN, State and Zip Code columns use MaskedTextBox controls for format
  10. * and validate their input.
  11. *
  12. * This source is subject to the Microsoft Public License.
  13. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  14. * All other rights reserved.
  15. *
  16. * History:
  17. * * 6/05/2009 3:00 PM Zhi-Xin Ye Created
  18. \******************************************************************************************/
  19. #region Using directives
  20. using System;
  21. using System.Collections.Generic;
  22. using System.ComponentModel;
  23. using System.Data;
  24. using System.Drawing;
  25. using System.Linq;
  26. using System.Text;
  27. using System.Windows.Forms;
  28. #endregion
  29. namespace CSWinFormDataGridView.CustomDataGridViewColumn
  30. {
  31. public partial class MainForm : Form
  32. {
  33. public MainForm()
  34. {
  35. InitializeComponent();
  36. }
  37. private void MainForm_Load(object sender, EventArgs e)
  38. {
  39. DataGridViewTextBoxColumn dgvTextBoxColumn;
  40. MaskedTextBoxColumn mtbColumn;
  41. //
  42. // Employee name.
  43. //
  44. dgvTextBoxColumn = new DataGridViewTextBoxColumn();
  45. dgvTextBoxColumn.HeaderText = "Name";
  46. dgvTextBoxColumn.Width = 120;
  47. this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
  48. //
  49. // Employee ID -- this will be of the format:
  50. // [A-Z][0-9][0-9][0-9][0-9][0-9]
  51. //
  52. // this is well sutied to using a MaskedTextBox column.
  53. //
  54. mtbColumn = new MaskedTextBoxColumn();
  55. mtbColumn.HeaderText = "Employee ID";
  56. mtbColumn.Mask = "L00000";
  57. mtbColumn.Width = 75;
  58. this.employeesDataGridView.Columns.Add(mtbColumn);
  59. //
  60. // [American] Social Security number, of the format:
  61. // ###-##-####
  62. //
  63. mtbColumn = new MaskedTextBoxColumn();
  64. mtbColumn.HeaderText = "SSN";
  65. mtbColumn.Mask = "000-00-0000";
  66. mtbColumn.Width = 75;
  67. this.employeesDataGridView.Columns.Add(mtbColumn);
  68. //
  69. // Address
  70. //
  71. dgvTextBoxColumn = new DataGridViewTextBoxColumn();
  72. dgvTextBoxColumn.HeaderText = "Address";
  73. dgvTextBoxColumn.Width = 150;
  74. this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
  75. //
  76. // City
  77. //
  78. dgvTextBoxColumn = new DataGridViewTextBoxColumn();
  79. dgvTextBoxColumn.HeaderText = "City";
  80. dgvTextBoxColumn.Width = 75;
  81. this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
  82. //
  83. // State
  84. //
  85. mtbColumn = new MaskedTextBoxColumn();
  86. mtbColumn.HeaderText = "State";
  87. mtbColumn.Mask = "LL";
  88. mtbColumn.Width = 40;
  89. this.employeesDataGridView.Columns.Add(mtbColumn);
  90. //
  91. // Zip Code #####-#### (+4 optional)
  92. //
  93. mtbColumn = new MaskedTextBoxColumn();
  94. mtbColumn.HeaderText = "Zip Code";
  95. mtbColumn.Mask = "00000-0000";
  96. mtbColumn.Width = 75;
  97. mtbColumn.ValidatingType = typeof(ZipCode);
  98. this.employeesDataGridView.Columns.Add(mtbColumn);
  99. //
  100. // Department Code
  101. //
  102. dgvTextBoxColumn = new DataGridViewTextBoxColumn();
  103. dgvTextBoxColumn.HeaderText = "Department";
  104. dgvTextBoxColumn.ValueType = typeof(int);
  105. dgvTextBoxColumn.Width = 75;
  106. this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
  107. }
  108. }
  109. // Type that represents a US Zipcode to demonstrate
  110. // the ValidatingType feature on the MaskedTextBox.
  111. #region ZipCode Class
  112. public class ZipCode
  113. {
  114. private int zipCode;
  115. private int plusFour;
  116. public ZipCode()
  117. {
  118. this.zipCode = 0;
  119. this.plusFour = 0;
  120. }
  121. public ZipCode(string in_zipCode)
  122. {
  123. parseFromString(in_zipCode, out zipCode, out plusFour);
  124. }
  125. public ZipCode(int in_ivalue)
  126. {
  127. this.zipCode = in_ivalue;
  128. this.plusFour = 0;
  129. }
  130. public static implicit operator ZipCode(string s)
  131. {
  132. return new ZipCode(s);
  133. }
  134. public static implicit operator ZipCode(int i)
  135. {
  136. return new ZipCode(i);
  137. }
  138. protected static void parseFromString
  139. (
  140. string in_string,
  141. out int out_zipCode,
  142. out int out_plusFour
  143. )
  144. {
  145. int zc = 0, pf = 0;
  146. char[] charray;
  147. int x = 0;
  148. if (in_string == null || in_string.Equals(""))
  149. {
  150. throw new ArgumentException("Invalid String");
  151. }
  152. charray = in_string.Trim().ToCharArray();
  153. for (x = 0; x < 5; x++)
  154. {
  155. if (!Char.IsDigit(charray[x]))
  156. {
  157. throw new ArgumentException("Invalid String");
  158. }
  159. zc = zc * 10 + numfromchar(charray[x]);
  160. }
  161. while (x < charray.Length && !Char.IsDigit(charray[x]))
  162. {
  163. x++;
  164. }
  165. if (x < charray.Length)
  166. {
  167. for (int y = x; y < 4; y++)
  168. {
  169. if (!Char.IsDigit(charray[y]))
  170. {
  171. throw new ArgumentException("Invalid ZipCode String");
  172. }
  173. pf = pf * 10 + numfromchar(charray[y]);
  174. }
  175. }
  176. out_zipCode = zc;
  177. out_plusFour = pf;
  178. }
  179. private static int numfromchar(char c)
  180. {
  181. switch (c)
  182. {
  183. case '0':
  184. return 0;
  185. case '1':
  186. return 1;
  187. case '2':
  188. return 2;
  189. case '3':
  190. return 3;
  191. case '4':
  192. return 4;
  193. case '5':
  194. return 5;
  195. case '6':
  196. return 6;
  197. case '7':
  198. return 7;
  199. case '8':
  200. return 8;
  201. case '9':
  202. return 9;
  203. default:
  204. throw new ArgumentException("invalid digit");
  205. }
  206. }
  207. public override string ToString()
  208. {
  209. StringBuilder sb = new StringBuilder(10);
  210. sb.Append(zipCode.ToString("00000"));
  211. sb.Append("-");
  212. sb.Append(plusFour.ToString("0000"));
  213. return sb.ToString();
  214. }
  215. }
  216. #endregion
  217. }