• c#——Winform DatagridView上显示下拉树



    using System.Drawing;
    using System.Windows.Forms;
    namespace WindowsApplication23
    {
    public class ComboBoxTreeView : ComboBox
    {
    private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
    ToolStripControlHost treeViewHost;
    ToolStripDropDown dropDown;
    public ComboBoxTreeView()
    {
    TreeView treeView = new TreeView();
    treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
    treeView.BorderStyle = BorderStyle.None;

    treeViewHost = new ToolStripControlHost(treeView);
    dropDown = new ToolStripDropDown();
    dropDown.Width = this.Width;
    dropDown.Items.Add(treeViewHost);
    }
    public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
    this.Text = TreeView.SelectedNode.Text;
    dropDown.Close();
    }
    public TreeView TreeView
    {
    get { return treeViewHost.Control as TreeView; }
    }
    private void ShowDropDown()
    {
    if (dropDown != null)
    {
    treeViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
    dropDown.Show(this, 0, this.Height);
    }
    }
    protected override void WndProc(ref Message m)
    {
    if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
    {
    ShowDropDown();
    return;
    }
    base.WndProc(ref m);
    }
    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    if (dropDown != null)
    {
    dropDown.Dispose();
    dropDown = null;
    }
    }
    base.Dispose(disposing);
    }
    }

    }

    using System;
    using System.Windows.Forms;

    namespace WindowsApplication23
    {
    public class DataGridViewTreeViewCell:DataGridViewTextBoxCell
    {
    public DataGridViewTreeViewCell()
    {

    }
    public override void InitializeEditingControl(int rowIndex, object
    initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
    base.InitializeEditingControl(rowIndex, initialFormattedValue,
    dataGridViewCellStyle);
    DataGridViewTreeViewEditingControl ctl =
    DataGridView.EditingControl as DataGridViewTreeViewEditingControl;
    ctl.Text = (string)this.Value;
    }

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

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

    public override object DefaultNewRowValue
    {
    get
    {
    return "";
    }
    }
    }
    }

    using System;
    using System.Windows.Forms;

    namespace WindowsApplication23
    {
    public class DataGridViewTreeViewColumn:DataGridViewColumn
    {
    public DataGridViewTreeViewColumn():
    base(new DataGridViewTreeViewCell())
    {

    }
    public override DataGridViewCell CellTemplate
    {
    get
    {
    return base.CellTemplate;
    }
    set
    {
    if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewTreeViewCell)))
    {
    throw new InvalidCastException("不是DataGridViewTreeViewCell");
    }
    base.CellTemplate = value;
    }
    }
    }

    using System.Windows.Forms;

    namespace WindowsApplication23
    {
    public class DataGridViewTreeViewEditingControl:ComboBoxTreeView,IDataGridViewEditingControl
    {
    protected int rowIndex;
    protected DataGridView dataGridView;
    protected bool valueChanged = false;

    public DataGridViewTreeViewEditingControl()
    {

    }
    //重写基类
    protected override void OnTextChanged(System.EventArgs e)
    {
    base.OnTextChanged(e);
    NotifyDataGridViewOfValueChange();
    }
    // 当text值发生变化时,通知DataGridView
    private void NotifyDataGridViewOfValueChange()
    {
    valueChanged = true;
    dataGridView.NotifyCurrentCellDirty(true);
    }
    /// <summary>
    /// 在Cell被编辑的时候光标显示
    /// </summary>
    public Cursor EditingPanelCursor
    {
    get
    {
    return Cursors.IBeam;
    }
    }
    /// <summary>
    /// 获取或设置所在的DataGridView
    /// </summary>
    public DataGridView EditingControlDataGridView
    {
    get
    {
    return dataGridView;
    }

    set
    {
    dataGridView = value;
    }
    }

    /// <summary>
    /// 获取或设置格式化后的值
    /// </summary>
    public object EditingControlFormattedValue
    {
    set
    {
    Text = value.ToString();
    NotifyDataGridViewOfValueChange();
    }
    get
    {
    return this.Text;
    }

    }
    /// <summary>
    /// 获取控件的Text值
    /// </summary>
    /// <param name="context">错误上下文</param>
    /// <returns></returns>
    public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
    return Text;
    }

    /// <summary>
    /// 编辑键盘
    /// </summary>
    /// <param name="keyData"></param>
    /// <param name="dataGridViewWantsInputKey"></param>
    /// <returns></returns>
    public bool EditingControlWantsInputKey(
    Keys key, bool dataGridViewWantsInputKey)
    {
    // Let the DateTimePicker handle the keys listed.
    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;
    }
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {
    }
    public virtual bool RepositionEditingControlOnValueChange
    {
    get
    {
    return false;
    }
    }
    /// <summary>
    /// 控件所在行
    /// </summary>
    public int EditingControlRowIndex
    {
    get
    {
    return this.rowIndex;
    }

    set
    {
    this.rowIndex = value;
    }
    }
    /// <summary>
    /// 设置样式
    /// </summary>
    /// <param name="dataGridViewCellStyle"></param>
    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
    this.Font = dataGridViewCellStyle.Font;
    this.ForeColor = dataGridViewCellStyle.ForeColor;
    this.BackColor = dataGridViewCellStyle.BackColor;
    }
    /// <summary>
    /// 是否值发生了变化
    /// </summary>
    public bool EditingControlValueChanged
    {
    get
    {
    return valueChanged;
    }

    set
    {
    this.valueChanged = value;
    }
    }
    }

    https://blog.csdn.net/shiyaru1314/article/details/51920494

  • 相关阅读:
    IntelliJ IDEA配置Tomcat 与安装Tomcat失败原因
    IntelliJ IDEA创建JavaWeb工程及配置Tomcat部署
    IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体
    as 插件GsonFormat用法(json字符串快速生成javabean)
    AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
    arraylist和linkedlist
    BNUOJ 3958 MAX Average Problem
    ZOJ 5579 Stean
    HDU 3401 Trade
    HDU 1695 GCD
  • 原文地址:https://www.cnblogs.com/bjchaofan/p/9665377.html
Copyright © 2020-2023  润新知