四、文件及函数
1、文件说明
(1)LQMenu.dbml:创建Linq To SQL类,使其对应数据库中的core_menu表
(2)MyFuncLib.cs:常见的公共函数类,将系统中经常用到的公共的函数放入其中
(3)BLMenu.cs:对LQMenu.dbml的数据操作层,用于存放数据增删改查函数,及其他与数据库有关的函数
(4)FormMenu.cs:窗体的主函数
2、FormMenu_Load函数
使用InitDgv函数来初始化DataGridView,当需要修改的时候可以直接在代码中进行修改。该函数还调用了MyFuncLib.InitDgv,以及后续的两个函数InitDgvColumns以及IsNumber。
(1)FormMenu.InitDgv()
/// <summary> 用代码的方式构造dataGridView,便于修改里面的内容 /// /// </summary> private void InitDGV() { StringBuilder sb = new StringBuilder(); sb.Append("Type=text,Width=100,Visible=true,ReadOnly=false,Frozen=true,Format=,NullValue=,"); sb.Append("Order=false,Alignment=left,MaxInputLength=30,Name=menuName,HeaderText=菜单名称"); sb.Append(";Type=text,Width=90,Visible=true,ReadOnly=false,Frozen=false,Format=,NullValue=,"); sb.Append("Order=false,Alignment=left,MaxInputLength=30,Name=subMenuName,HeaderText=二级菜单"); sb.Append(";Type=text,Width=100,Visible=true,ReadOnly=true,Frozen=false,Format=,NullValue=,"); sb.Append("Order=false,Alignment=left,MaxInputLength=30,Name=title,HeaderText=菜单文本"); sb.Append(";Type=checkbox,Width=70,Visible=true,ReadOnly=false,Frozen=false,Format=,NullValue=0,"); sb.Append("Order=false,Alignment=left,MaxInputLength=30,Name=isForm,HeaderText=是否窗体"); sb.Append(";Type=text,Width=100,Visible=true,ReadOnly=false,Frozen=false,Format=,NullValue=,"); sb.Append("Order=false,Alignment=left,MaxInputLength=100,Name=methodNames,HeaderText=方法列表"); sb.Append(";Type=text,Width=100,Visible=true,ReadOnly=true,Frozen=false,Format=,NullValue=,"); sb.Append("Order=false,Alignment=left,MaxInputLength=30,Name=fullName,HeaderText=完整名称"); sb.Append(";Type=checkbox,Width=70,Visible=true,ReadOnly=true,Frozen=false,Format=,NullValue=0,"); sb.Append("Order=false,Alignment=left,MaxInputLength=30,Name=hasChildren,HeaderText=包含下级"); sb.Append(";Type=checkbox,Width=70,Visible=true,ReadOnly=true,Frozen=false,Format=,NullValue=1,"); sb.Append("Order=false,Alignment=left,MaxInputLength=30,Name=isSys,HeaderText=系统菜单"); sb.Append(";Type=checkbox,Width=30,Visible=false,ReadOnly=false,Frozen=false,Format=,NullValue=,"); sb.Append("Order=false,Alignment=center,MaxInputLength=,Name=parentId,HeaderText=parentId"); sb.Append(";Type=text,Width=100,Visible=false,ReadOnly=false,Frozen=false,Format=,NullValue=0,"); sb.Append("Order=false,Alignment=left,MaxInputLength=,Name=ID_,HeaderText=ID_"); dataGridViewMenu.AutoGenerateColumns = false; dataGridViewMenu.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridViewMenu.RowHeadersWidth = 41; dataGridViewMenu.Columns.Clear(); dataGridViewMenu.SelectionMode = DataGridViewSelectionMode.FullRowSelect; MyFuncLib.InitDgv(dataGridViewMenu, sb.ToString()); }
(2)
/// <summary> 初始化dataGridView各列 /// /// </summary> /// <param name="d">DataGirdView</param> /// <param name="c">Column</param> /// <param name="type">type(如text,int,decimal,checkbox,date,datetime</param> /// <param name="maxInputLength">最大程度</param> /// <param name="source">数据源</param> private static void InitDgvColumns(DataGridView d, DataGridViewColumn c, string type, string maxInputLength, string source) { if ("text".Equals(type)) { #region text DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn(); c2.Name = c.Name; c2.DataPropertyName = c.DataPropertyName; c2.HeaderText = c.HeaderText; c2.Width = c.Width; c2.Visible = c.Visible; c2.ReadOnly = c.ReadOnly; c2.Frozen = c.Frozen; c2.DefaultCellStyle = c.DefaultCellStyle; c2.HeaderCell.Style = c.HeaderCell.Style; c2.SortMode = c.SortMode; c2.Tag = c.Tag; if (IsNumber(maxInputLength)) c2.MaxInputLength = int.Parse(maxInputLength); d.Columns.Add(c2); #endregion } else if ("decimal".Equals(type)) { #region decimal DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn(); c2.Name = c.Name; c2.DataPropertyName = c.DataPropertyName; c2.HeaderText = c.HeaderText; c2.Width = c.Width; c2.Visible = c.Visible; c2.ReadOnly = c.ReadOnly; c2.Frozen = c.Frozen; c2.DefaultCellStyle = c.DefaultCellStyle; c2.HeaderCell.Style = c.HeaderCell.Style; c2.SortMode = c.SortMode; c2.Tag = c.Tag; if (IsNumber(maxInputLength)) c2.MaxInputLength = int.Parse(maxInputLength); d.Columns.Add(c2); #endregion } else if ("int".Equals(type)) { #region int DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn(); c2.Name = c.Name; c2.DataPropertyName = c.DataPropertyName; c2.HeaderText = c.HeaderText; c2.Width = c.Width; c2.Visible = c.Visible; c2.ReadOnly = c.ReadOnly; c2.Frozen = c.Frozen; c2.DefaultCellStyle = c.DefaultCellStyle; c2.HeaderCell.Style = c.HeaderCell.Style; c2.SortMode = c.SortMode; c2.Tag = c.Tag; if (IsNumber(maxInputLength)) c2.MaxInputLength = int.Parse(maxInputLength); d.Columns.Add(c2); #endregion } else if ("date".Equals(type)) { #region date DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn(); c2.Name = c.Name; c2.DataPropertyName = c.DataPropertyName; c2.HeaderText = c.HeaderText; c2.Width = c.Width; c2.Visible = c.Visible; c2.ReadOnly = c.ReadOnly; c2.Frozen = c.Frozen; c2.DefaultCellStyle = c.DefaultCellStyle; c2.HeaderCell.Style = c.HeaderCell.Style; c2.SortMode = c.SortMode; c2.Tag = c.Tag; if (IsNumber(maxInputLength)) c2.MaxInputLength = int.Parse(maxInputLength); d.Columns.Add(c2); #endregion } else if ("datetime".Equals(type)) { #region datetime DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn(); c2.Name = c.Name; c2.DataPropertyName = c.DataPropertyName; c2.HeaderText = c.HeaderText; c2.Width = c.Width; c2.Visible = c.Visible; c2.ReadOnly = c.ReadOnly; c2.Frozen = c.Frozen; c2.DefaultCellStyle = c.DefaultCellStyle; c2.HeaderCell.Style = c.HeaderCell.Style; c2.SortMode = c.SortMode; c2.Tag = c.Tag; if (IsNumber(maxInputLength)) c2.MaxInputLength = int.Parse(maxInputLength); d.Columns.Add(c2); #endregion } else if ("checkbox".Equals(type) || "check".Equals(type)) { #region checkbox DataGridViewCheckBoxColumn c2 = new DataGridViewCheckBoxColumn(); c2.Name = c.Name; c2.DataPropertyName = c.DataPropertyName; c2.HeaderText = c.HeaderText; c2.Width = c.Width; c2.Visible = c.Visible; c2.ReadOnly = c.ReadOnly; c2.Frozen = c.Frozen; c2.HeaderCell.Style = c.HeaderCell.Style; c2.SortMode = c.SortMode; c2.Tag = c.Tag; c2.TrueValue = 1; c2.FalseValue = 0; d.Columns.Add(c2); #endregion } }