DataGridView列使用自定义用户控件出现问题,请帮忙指点下

liguoa1 2009-09-22 11:38:59
我想在DataGridView列中加入自己定义的一个用户控件,做一些配置工作
我的用户控件主要有两个ComBox,要实现的效果就是COmBox的内容改变后,对应单元格可以获取改变后的内容
自定义控件代码如下
public partial class OperateUserControl : UserControl
{
private Content config;
public OperateUserControl()
{
InitializeComponent();
}
public Content Config
{
get
{
return config;
}
set
{
config = value;
}
}

private void OperateUserControl_Load(object sender, EventArgs e)
{
config = new Content();
comboBoxSort.DataBindings.Add("Text", config, "ContentType");
comboBoxValue.DataBindings.Add("Text", config, "ContentValue");
}
protected virtual void OnValueChanged(EventArgs eventargs)
{

}

}
public class Content : Object
{
string contentType;

public string ContentType
{
get { return contentType; }
set { contentType = value; }
}
string contentValue;

public string ContentValue
{
get { return contentValue; }
set { contentValue = value; }
}
}

实现自定义列的代码如下
public class DataGridViewConfigColumn : DataGridViewColumn
{
public DataGridViewConfigColumn()
: base(new DataGridViewConfigCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(DataGridViewConfigCell)))
{
throw new InvalidCastException("Must be a ConfigCell");
}
base.CellTemplate = value;
}
}
}

public class DataGridViewConfigCell : DataGridViewCell
{
public DataGridViewConfigCell()
: base()
{

}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
}
public override Type EditType
{
get
{
return typeof(DataGridViewConfigControl);
}
}
public override Type ValueType
{
get
{
return typeof(Content);
}
}
public override object DefaultNewRowValue
{
get
{
return base.DefaultNewRowValue;
}
}
}

class DataGridViewConfigControl : OperateUserControl, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public DataGridViewConfigControl()
{
//this.Format = DateTimePickerFormat.Short;
}

// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.Config;
}
set
{
Content newValue = value as Content;
if (newValue != null)
{
this.Config = newValue;
}
}
}

// Implements the IDataGridViewEditingControl.GetEditingControlFormattedValue
// method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}

// Implements the IDataGridViewEditingControl.ApplyCellStyleToEditingControl
// method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}

// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}

// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys in the next list.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}

// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparations is needed.
}

// Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange
// property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}

// Implements the IDataGridViewEditingControl.EditingControlDataGridView
// property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}

// Implements the IDataGridViewEditingControl.EditingControlValueChanged
// property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}

// Implements the IDataGridViewEditingControl.EditingPanelCursor
// property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}

protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView when the cell's contents was changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}

}

可以加入DataGridView列,并且编译成功通过
可是显示的时候列中的内容为空白
大虾们给帮帮忙了

谢谢
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liguoa1 2009-09-22
  • 打赏
  • 举报
回复
怎么没有回应呢,老大们都忙什么呢
liguoa1 2009-09-22
  • 打赏
  • 举报
回复
在线等,大家帮帮忙啦
feifeiyiwen 2009-09-22
  • 打赏
  • 举报
回复
好长,期待答案

110,498

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧