8,834
社区成员
发帖
与我相关
我的任务
分享
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "ID";
column.Name = "编号";
dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Name";
column.Name = "项目";
dataGridView1.Columns.Add(column);
column = new CustomColumn();
column.DataPropertyName = "Mode";
column.Name = "显示模式";
dataGridView1.Columns.Add(column);
DataModel model = new DataModel
{
ID = "TG1",
Name = "Illegal Response非法响应错误",
Mode = ShowMode.No,
};
// 自定义单元格
public class CustomCell : DataGridViewCell
{
public override Type ValueType => typeof(CustomControl);
public override Type FormattedValueType {
get { return typeof(CustomCell); }
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
if ((context & DataGridViewDataErrorContexts.ClipboardContent) != 0)
{
return this.Description;
}
object obj2 = base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter,
formattedValueTypeConverter, context);
if ((obj2 == null) && (cellStyle.NullValue == null))
{
return null;
}
return obj2;
}
public object Description { get; set; }
}
// 自定义列
public class CustomColumn : DataGridViewColumn
{
public CustomColumn() : base(new CustomCell()){
}
}
// 定义的复合控件
public partial class CustomControl : UserControl
{
private ShowMode currentMode;
public CustomControl()
{
InitializeComponent();
}
public ShowMode Mode
{
get { return currentMode; }
set
{
currentMode = value;
if (currentMode == ShowMode.No)
{
this.pictureBox1.Hide();
this.pictureBox2.Hide();
this.pictureBox3.Hide();
}
else if (currentMode == ShowMode.Progressing)
{
this.pictureBox1.Show();
this.pictureBox2.Hide();
this.pictureBox3.Hide();
}
else if (currentMode == ShowMode.Success)
{
this.pictureBox1.Hide();
this.pictureBox2.Show();
this.pictureBox3.Hide();
}
else if (currentMode == ShowMode.Fail)
{
this.pictureBox1.Hide();
this.pictureBox2.Hide();
this.pictureBox3.Show();
}
}
}
}
// mode枚举:无,执行中,执行成功,执行失败,用于指示自定义复合控件显示对应图标
public enum ShowMode
{
No,
Progressing,
Success,
Fail,
}
// 实体对象
public class DataModel : INotifyPropertyChanged
{
private ShowMode mode;
public string ID { get; set; }
public string Name { get; set; }
public ShowMode Mode
{
get { return mode;}
set
{
mode = value;
OnPropertyChanged("Mode");
}
}
public List<DataModel> Childrens { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}