首页 > 代码库 > GridView 动态列上方添加相应的Combox等控件

GridView 动态列上方添加相应的Combox等控件

Dev中的Gridview控件添加简单的动态列并不复杂,但是有时候需要在在动态列上添加对应的Combox

 1  gvList.BeginUpdate(); 2 if (gcList.DataSource != null) 3 { 4    (gcList.DataSource as DataTable).Clear(); 5 } 6  gvList.Columns.Clear(); 7  8 //DataTable 形式绑定数据源 9 DataTable dt = new DataTable();10                 dt.Columns.Add("PatternPartCD", typeof(string));11                 dt.Columns.Add("PatternPartNm", typeof(string));12                 dt.Columns.Add("Size", typeof(Decimal));13                 dt.Columns.Add("TorlnceL", typeof(Decimal));14                 dt.Columns.Add("TorlnceR", typeof(Decimal));15 16                 //手动添加 Gridview列    17                 GridHelper.AddCol(gvList, _presenter._resourceService.GetResourceString("Code"), "PatternPartCD", "PatternPartCD", true, 1, 55, this._riCode);18                 GridHelper.AddCol(gvList, "* " + _presenter._resourceService.GetResourceString("PatternPartName"), "PatternPartNm", "PatternPartNm", true, 2, 180, null);19                 GridHelper.AddCol(gvList, _presenter._resourceService.GetResourceString("Standard") + _presenter._resourceService.GetResourceString("Size"), "Size", "Size", true                                     , 3, 70,  null);20                 GridHelper.AddCol(gvList, _presenter._resourceService.GetResourceString("TorlnceL"), "TorlnceL", "TorlnceL", true, 4, 70,  null);21                 GridHelper.AddCol(gvList, _presenter._resourceService.GetResourceString("TorlnceR"), "TorlnceR", "TorlnceR", true, 5, 70,  null);22 23 24 25 26                 for (i = 0; i < StyleSizeInfo.Count; i++)27                 {28                     dt.Columns.Add(StyleSizeInfo[i].SizeCD, typeof(decimal));                    
GridHelper.AddCol(gvList, StyleSizeInfo[i].SizeNm, StyleSizeInfo[i].SizeCD, StyleSizeInfo[i].SizeCD, true, j, 60, txtEdit);29 gvList.Columns[j].DisplayFormat.FormatString = "N2";30 gvList.Columns[j].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;31 gvList.Columns[j].Tag = "Tag";32 33 System.Windows.Forms.ComboBox cboSize = new System.Windows.Forms.ComboBox();34 35 cboSize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;36 cboSize.Enabled = true; cboSize.FormattingEnabled = true;37 cboSize.Location = new Point(462 + i * 60, 0);//重新定位控件的位置39 cboSize.Name = "cboBrand" + j.ToString();40 cboSize.DataSource = dtoList;41 cboSize.DisplayMember = "SizeNm";42 cboSize.ValueMember = "SizeCD"; 43 this.panelbtn2.Controls.Add(cboSize);44 45 dt.Columns.Add("PlusTol", typeof(Decimal));46 dt.Columns.Add("MinusTol", typeof(Decimal));47 dt.Columns.Add("Measurement", typeof(string));48 49 GridHelper.AddCol(gvList, _presenter._resourceService.GetResourceString("PlusTol"), "PlusTol", "PlusTol", true, j, 70, null);50 GridHelper.AddCol(gvList, _presenter._resourceService.GetResourceString("MinusTol"), "MinusTol", "MinusTol", true, j + 1, 70, null);51 GridHelper.AddCol(gvList, "* " + _presenter._resourceService.GetResourceString("PatternSizeChkMethod"), "Measurement", "Measurement", true, j + 2, 550, null);

注意:

1.最好将添加的控件单独放到一个panel上,方便重新定位或者遍历所有的新添加的控件:

foreach (Control item in panel1.Controls)// {   if (item is System.Windows.Forms.ComboBox)   {    (item as System.Windows.Forms.ComboBox).Name = "新名字";   } }

2.如果绑定的是有数据源的控价且控价数据源相同时,需要重新Copy数据源,否则可能会导致添加的控件变得一致:

 1     public class Employee : ICloneable//添加Copy方法到类内 2     { 3         public string name { get; set; } 4         public object Clone() 5         { 6             return this.MemberwiseClone(); 7         } 8     } 9 10 List<Employee >  EmployeeList = new List<Employee >11 12 foreach (Employee dto in EmployeeList )13  {14     dtoList.Add(dto.Clone() as IF_StyleSizeInfo);15  }
Clone 类

 

GridView 动态列上方添加相应的Combox等控件