Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在datagrid视图中绘制图像的自定义组合框_C#_.net_Winforms_Datagridview_Combobox - Fatal编程技术网

C# 在datagrid视图中绘制图像的自定义组合框

C# 在datagrid视图中绘制图像的自定义组合框,c#,.net,winforms,datagridview,combobox,C#,.net,Winforms,Datagridview,Combobox,我是这个自定义datagridview列机制的新手。我希望在datagridview中有一个组合框,允许用户选择不同的线条样式(使用DashStyle)。我发现的教程要么不适用于组合框,要么不使用绘图 通过使用以下代码重写OnDrawItem(),我已经可以创建一个工作的自定义独立组合框 但我在创建自定义datagridview组合框列时遇到问题 我希望comboboxcell的值返回DashStyle 我在表单加载时显示绘制的项目时也遇到问题。将默认启动值设置为Dashstyle.Solid会

我是这个自定义datagridview列机制的新手。我希望在datagridview中有一个组合框,允许用户选择不同的线条样式(使用DashStyle)。我发现的教程要么不适用于组合框,要么不使用绘图

通过使用以下代码重写OnDrawItem(),我已经可以创建一个工作的自定义独立组合框

但我在创建自定义datagridview组合框列时遇到问题

  • 我希望comboboxcell的值返回DashStyle
  • 我在表单加载时显示绘制的项目时也遇到问题。将默认启动值设置为Dashstyle.Solid会在组合框中写入“Solid”。当我点击它时,它会触发绘图项目
  • 以下是我迄今为止根据web上的其他示例编写的代码:

    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; }
      }
    
    }
    

    我很感激任何关于我做错了什么以及这是如何工作的信息

    创建一个接受字符串“solid”并返回DashStyle.solid的方法

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