讨论:在UserControl上放置DataGridView后在vss设计编辑DataGridView的Columns

 

网上找到了方法,但是稍微差了一点点

http://www.developersdex.com/vb/message.asp?p=1120&r=5501708

上面得网址是2006年同样遇到相同问题的兄弟。由于英文不好(能看不能写),按照上面的操作做了。成功了一大步,添加的时候Column的变量添加到了Init....函数中,没有添加到类的private变量因此设计的时候有看不到了,但是运行的时候好用。手动挪出来的是好用的。

下面是代码, UserControl上放置了一个DataGridView,name-> dgvX_Data

  
[DesignerSerializer(typeof(MyCodeDomSerializer),typeof(CodeDomSerializer))]
public partial class CommonReveal : UserControl
{
public CommonReveal()
{
InitializeComponent();
  }

 
[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DescriptionAttribute("Sets the DataGridViewX Columns")]
public System.Windows.Forms.DataGridViewColumnCollection InnerDGVColumns
{
get { return this.dgvX_Data.Columns; }
}

class MyCollectionEditor : CollectionEditor
{
public MyCollectionEditor(Type type)
: base(type)
{ }

protected override Type CreateCollectionItemType()
{
return typeof(DataGridViewColumn);
}
protected override Type[] CreateNewItemTypes()
{
Type[] types = new Type[] { typeof(DataGridViewTextBoxColumn),
typeof(DataGridViewComboBoxColumn),
typeof(DataGridViewCheckBoxColumn),
typeof(DataGridViewButtonColumn),
typeof(DataGridViewImageColumn),
typeof(DataGridViewLinkColumn)};
return types;
}

protected override object CreateInstance(Type itemType)
{
DataGridViewColumn column = itemType.GetConstructor(Type.EmptyTypes).Invoke(null) as DataGridViewColumn;
return column;
}
}

public class MyCodeDomSerializer : CodeDomSerializer
{

public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
{
CodeDomSerializer baseClassSerializer =
(CodeDomSerializer)manager.GetSerializer(typeof(CommonReveal).BaseType,typeof(CodeDomSerializer));

return baseClassSerializer.Deserialize(manager, codeObject);
}

public override object Serialize(IDesignerSerializationManager manager, object value)
{
CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(CommonReveal).BaseType,typeof(CodeDomSerializer));

// serialize the UserControl
object codeObject = baseClassSerializer.Serialize(manager, value);

if (codeObject is CodeStatementCollection)
{
CodeStatementCollection statements = (CodeStatementCollection)codeObject;

// The code statement collection is valid, so add a comment.
string commentText = "This comment was added to this object by a custom serializer.";
CodeCommentStatement comment = new CodeCommentStatement(commentText);

statements.Insert(0, comment);
// serialize the inner DataGridView's columns

if (value is CommonReveal)
{
DataGridViewColumnCollection innercolumns = (value as CommonReveal).InnerDGVColumns;
// declare the variable collection of columns in the inner DataGridView
List<CodeVariableReferenceExpression> parameter_list = new List<CodeVariableReferenceExpression>();

CodeArrayCreateExpression createArray = null;
CodeMethodInvokeExpression methodcall = null;

int i = 1;

CodeStatementCollection col_Statements = null;

foreach (DataGridViewColumn col in innercolumns)
{
/// serialize each column
col_Statements = new CodeStatementCollection();

CodeObjectCreateExpression col_ObjectCreate = new CodeObjectCreateExpression(col.GetType());

CodeVariableDeclarationStatement col_VariableDeclaration = new CodeVariableDeclarationStatement(col.GetType(), "column" + i.ToString());


CodeAssignStatement col_Assign_Create = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString()), col_ObjectCreate);
// serialize the Width property of the column
CodeAssignStatement col_Assign_width = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".Width"), new CodePrimitiveExpression(col.Width));

CodeFieldReferenceExpression col_FieldReference = base.SerializeToExpression(manager, col) as CodeFieldReferenceExpression;
if (col_FieldReference == null)
{
col_Statements.Add(col_VariableDeclaration);
col_Statements.Add(col_Assign_Create);
col_Statements.Add(col_Assign_width);
parameter_list.Add(new CodeVariableReferenceExpression("column" + i.ToString()));
}

///

statements.AddRange(col_Statements);
i++;
}

CodeFieldReferenceExpression target = base.SerializeToExpression(manager, value) as CodeFieldReferenceExpression;
// if the designer hasn't all the columns to the inner DataGridView's column collection, add them by ourselves.
if (target != null && parameter_list.Count > 0)
{
createArray = new CodeArrayCreateExpression(typeof(DataGridViewColumn), parameter_list.ToArray());
methodcall = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression(target.FieldName + ".InnerDGVColumns"), "AddRange", createArray);
statements.Add(methodcall);
}
}
}
return codeObject;
}
}

出现的问题:


private void InitializeComponent()
{

//将代码生成到这里了 

System.Windows.Forms.DataGridViewTextBoxColumn column1;
  System.Windows.Forms.DataGridViewTextBoxColumn column2;
   System.Windows.Forms.DataGridViewTextBoxColumn column3;


System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmClientInfo));
this.commonReveal1 = new Changhong.CompositeReveal.CommonReveal();
this.column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.SuspendLayout();
// This comment was added to this object by a custom serializer.
//
// commonReveal1
//
this.commonReveal1.DataGridViewXName = null;
this.commonReveal1.Dock = System.Windows.Forms.DockStyle.Fill;
this.commonReveal1.InnerDGVColumns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.column1,
this.column2,
this.column3});
this.commonReveal1.LeftLabelItem = "货主列表";
this.commonReveal1.Location = new System.Drawing.Point(0, 0);
this.commonReveal1.Name = "commonReveal1";
this.commonReveal1.Size = new System.Drawing.Size(631, 437);
this.commonReveal1.TabIndex = 0;
this.commonReveal1.EventAdd += new System.EventHandler(this.commonReveal1_EventAdd);
//
// column1
//
this.column1.Name = "column1";
this.column1.ReadOnly = true;
//
// column2
//
this.column2.HeaderText = "中国";
this.column2.Name = "column2";
this.column2.ReadOnly = true;
//
// column3
//
this.column3.Name = "column3";
this.column3.ReadOnly = true;
//
// frmClientInfo
//
this.ClientSize = new System.Drawing.Size(631, 437);
this.Controls.Add(this.commonReveal1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "frmClientInfo";
this.Text = "货主信息";
this.Load += new System.EventHandler(this.frmClientInfo_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.frmClientInfo_FormClosed);
this.ResumeLayout(false);

}

#endregion

private Changhong.CompositeReveal.CommonReveal commonReveal1;
 

//下面是手动移动的,不移动的话设计的时候就没有了但是运行的时候好用 
 private System.Windows.Forms.DataGridViewTextBoxColumn column1;
private System.Windows.Forms.DataGridViewTextBoxColumn column2;
private System.Windows.Forms.DataGridViewTextBoxColumn column3;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值