2012-07-18 87 views
0

我对这个自定义datagridview列机制新不熟悉。我想要在datagridview中有一个组合框,这将允许用户选择不同的线条样式(使用DashStyle)。我发现的教程不是用于组合框或不使用绘图。自定义组合框与绘制图像到datagrid视图

我已经可以通过重写的OnDrawItem()做一个工作的定制独立的组合框,使用代码here

但我有麻烦一个自定义的datagridview组合框列。

  1. 我想让comboboxcell的值返回一个DashStyle。
  2. 我也遇到了在窗体加载时显示绘制项的问题。将默认启动值设置为Dashstyle.Solid将“Solid”写入组合框。当我点击它时,会触发平局项...

这里是我的代码到目前为止,基于网络上的其他例子:

public class CustomComboBoxColumn : DataGridViewComboBoxColumn 
{ 
    public CustomComboBoxColumn() 
    { 
    CustomComboBoxCell cbc = new CustomComboBoxCell(); 
    this.CellTemplate = cbc; 
    } 
} 

public class CustomComboBoxCell : DataGridViewComboBoxCell 
{ 
    public CustomComboBoxCell() 
    : base() { } 

    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
    { 
    base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); 

    var ctl = DataGridView.EditingControl as CustomComboBoxControl; 

    if (this.Value == null) 
     ctl.SelectedIndex = 0; 
    } 

    public override Type EditType 
    { 
    get { return typeof(CustomComboBoxControl); } 
    } 

    public override Type ValueType 
    { 
    get { return typeof(DashStyle); } 
    } 

    public override object DefaultNewRowValue 
    { 
    get { return DashStyle.Solid; } 
    } 

} 


public class CustomComboBoxControl : MyComboBox, IDataGridViewEditingControl 
{ 
    private int index_ = 0; 
    private DataGridView dataGridView_ = null; 
    private bool valueChanged_ = false; 

    public CustomComboBoxControl() : base() 
    { 
    this.SelectedIndexChanged += new EventHandler(ComboBoxControl_SelectedIndexChanged); 
    this.DrawMode = DrawMode.OwnerDrawVariable; 
    this.DropDownStyle = ComboBoxStyle.DropDownList; 
    } 


    public void ComboBoxControl_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    NotifyDataGridViewOfValueChange(); 
    } 


    protected virtual void NotifyDataGridViewOfValueChange() 
    { 
    this.valueChanged_ = true; 
    if (this.dataGridView_ != null) 
    { 
     this.dataGridView_.NotifyCurrentCellDirty(true); 
    } 
    } 

    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle) {  } 

    public DataGridView EditingControlDataGridView 
    { 
    get { return dataGridView_; } 
    set { dataGridView_ = value; } 
    } 

    public object EditingControlFormattedValue 
    { 
    get { return base.SelectedValue; } 
    set { base.SelectedValue = value; NotifyDataGridViewOfValueChange(); } 
    } 

    public int EditingControlRowIndex 
    { 
    get { return index_; } 
    set { index_ = value; } 
    } 

    public bool EditingControlValueChanged 
    { 
    get { return valueChanged_; } 
    set { valueChanged_ = value; } 
    } 

    public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey) 
    { 
    if (keyData == Keys.Return) 
     return true; 
    switch (keyData & Keys.KeyCode) 
    { 
     case Keys.Up: 
     case Keys.Down: 
     return true; 
     default: 
     return false; 
    } 
    } 

    public Cursor EditingPanelCursor 
    { 
    get { return base.Cursor; } 
    } 

    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) 
    { 
    var val = EditingControlFormattedValue; 
    if (val == null) 
     val = DashStyle.Solid; 
    return val.ToString(); 
    } 

    public void PrepareEditingControlForEdit(bool selectAll) { } 

    public bool RepositionEditingControlOnValueChange 
    { 
    get { return false; } 
    } 

} 

我欣赏的任何信息什么我做错了和它是如何工作...

回答

0

创造出把字符串“固体”的方法,并返回DashStyle.Solid

private DashStyle GetDashStyle(string style){ 
    switch(style) 
    { 
     case "Solid": 
     return DashStyle.Solid; 
    } 
}