下載XPTable提供示例:下載
當我們使用C#開發Windows應用程序,通常都用到DataGridView控件,毫無疑問,DataGrideView控件只提供了一些基本能滿足我們使用的功能,而且如果在DataGridView里添加CheckBox、下拉框、等等相關控件時,使用起來十分麻煩,如果閣下覺得使用微軟件提供的DataGridView十分麻煩時,這里小弟推薦 XPTable 給大家使用,XPTable為 Codeproject 提供開源的第三方控件,其功能十分強大。截圖可以看出XPTable里可以添加各式各樣的控件。
XPTable 提供的幾個Demo可以點擊這里下載:下載Demo.
如下是介紹我在項目里使用XPTable的一些分享:
1、先下載XPTable編譯文件:XPTable.DLL。(點擊下載)
2、將XPTable,添加到工具箱里,添加完成后將有三個控件:Table、TableModel、ColumnModel(
添加到工具箱的方法: A、在工具箱上點右鍵選擇“添加選項卡”(寫上自己想寫的名字,例如:第三控件),B、再在第三控件 這個選項卡上右鍵點“選擇項”
在出現的窗體上點瀏覽,到自己的.dll文件,選擇該文件。)
如圖:
3、將相應三個控件添加到Main窗口里如圖:
4、寫代碼初始化XPTable 如下:
string table = this.TableName; if (string.IsNullOrEmpty(table)) return; DataTable dt = GetDBInfoBLL.GetTBConfiguration(table); dt.Columns.Remove("表名"); dt.Columns.Remove("默認值"); dt.Columns.Remove("主鍵"); dt.Columns.Remove("表說明"); dt.Columns.Remove("字段序號"); dt.Columns.Add("添加說明", Type.GetType("System.String")); dt.Columns.Add("表頭說明", Type.GetType("System.String")); ///選擇框 添加 CheckBoxColumn checkbox_Add = new CheckBoxColumn("添加", 80); ///選擇框 修改 CheckBoxColumn checkbox_Update = new CheckBoxColumn("修改", 80); ///選擇框 列表 CheckBoxColumn checkbox_List = new CheckBoxColumn("列表", 80); ///選擇框 搜索 CheckBoxColumn checkbox_Search = new CheckBoxColumn("搜索", 80); //下拉框 ComboBoxColumn combobox_search = new ComboBoxColumn("搜索類型", 80); ComboBoxCellEditor searchEditor = new ComboBoxCellEditor(); searchEditor.DropDownStyle = DropDownStyle.DropDownList; searchEditor.Items.AddRange(new string[] { "大於", "小於", "等於", "相同", "Other" }); combobox_search.Editor = searchEditor; //下拉框 驗證 ComboBoxColumn combobox_Verificat = new ComboBoxColumn("驗證", 100); ComboBoxCellEditor VerificatEditor = new ComboBoxCellEditor(); VerificatEditor.DropDownStyle = DropDownStyle.DropDownList; VerificatEditor.Items.AddRange(new string[] { "不為空", "Classical", "Comedy", "Rock", "Other" }); combobox_Verificat.Editor = VerificatEditor; //字段說明 TextColumn text_name = new TextColumn("字段", 100); //字段說明 TextColumn text_desc = new TextColumn("字段說明", 154); //字段 標識 TextColumn text_Ident = new TextColumn("標識", 40); //字段 類型 TextColumn text_type = new TextColumn("類型", 62); //字段 長度 TextColumn text_Length = new TextColumn("長度", 50); //字段 允許為空 TextColumn text_allowEmpty = new TextColumn("為空", 50); //字段 添加說明 TextColumn text_addDesc = new TextColumn("添加說明", 130); //字段,表頭 TextColumn text_tableHeader = new TextColumn("表頭", 130); this.table.ColumnModel = new ColumnModel(new Column[] { //字段名 text_name, //添加選擇框 checkbox_Add, //添加驗證下拉框 combobox_Verificat, //修改選擇框 checkbox_Update, //列表選擇框 checkbox_List, //搜索選擇框 checkbox_Search, //搜索類型下拉框 combobox_search, //字段說明· text_desc, //標識 text_Ident, //類型 text_type, //長度 text_Length, //允許為空 text_allowEmpty, //添加說明 text_addDesc, //表頭說明 text_tableHeader }); //行數 Row[] RowList = new Row[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { //字段名 string name = dt.Rows[i][0].ToString(); if (string.IsNullOrEmpty(name)) continue; //字段說明 string fieldDesc = dt.Rows[i][1].ToString(); //標識 string Ident = dt.Rows[i][2].ToString(); //字段類型 string fieldType = dt.Rows[i][3].ToString(); //字段長度 string fieldLength = dt.Rows[i][4].ToString(); //允許為空 string allowEmpty = dt.Rows[i][5].ToString(); //添加說明 string addDesc = dt.Rows[i][6].ToString(); //表頭說明 string tableHeader = dt.Rows[i][7].ToString(); Row r = new Row(new Cell[]{ //名稱 new Cell(name), //添加選擇框 new Cell(name,true), //添加驗證下拉框 new Cell("無"), //修改選擇框 new Cell(name,true), //列表選擇框 new Cell(name,true), //搜索選擇框 new Cell(name,false), //搜索類型下拉框 new Cell("無"), //字段說明· new Cell(fieldDesc), //標識 new Cell(Ident), //類型 new Cell(fieldType), //長度 new Cell(fieldLength), //允許為空 new Cell(allowEmpty), //添加說明 new Cell(addDesc), //表頭說明 new Cell(tableHeader) }); RowList[i] = r; } this.table.TableModel = new TableModel(RowList); this.table.BeginEditing += new XPTable.Events.CellEditEventHandler(table_BeginEditing); this.table.TableModel.RowHeight = 21; this.table.EndUpdate();
info.AddDes =this.table.TableModel.Rows[i].Cells[12].Text; info.TableHeader =this.table.TableModel.Rows[i].Cells[13].Text; InfoList.Add(info); }
小弟用XPTable做的Table截圖如下:
InfoList = new List<CreateInfo>(); for (int i = 0; i < table.RowCount; i++) { CreateInfo info = new CreateInfo(); info.Field = this.table.TableModel.Rows[i].Cells[0].Text; info.Add =this.table.TableModel.Rows[i].Cells[1].Checked; info.Verificat =this.table.TableModel.Rows[i].Cells[2].Text; info.Update =this.table.TableModel.Rows[i].Cells[3].Checked; info.FieldList =this.table.TableModel.Rows[i].Cells[4].Checked; info.SearchField =this.table.TableModel.Rows[i].Cells[5].Checked; info.SearchType =this.table.TableModel.Rows[i].Cells[6].Text; info.FieldDes =this.table.TableModel.Rows[i].Cells[7].Text; info.Ident =this.table.TableModel.Rows[i].Cells[8].Text; info.FieldType =this.table.TableModel.Rows[i].Cells[9].Text; info.FieldLength =this.table.TableModel.Rows[i].Cells[10].Text; info.AllowNull =this.table.TableModel.Rows[i].Cells[11].Text; info.AddDes =this.table.TableModel.Rows[i].Cells[12].Text; info.TableHeader =this.table.TableModel.Rows[i].Cells[13].Text; InfoList.Add(info); }
大家可以根據自己的需要來做...有問題可以留言討論。