zoukankan      html  css  js  c++  java
  • c#

    Winform的DatagridView是不支持下拉树的,所以需要扩展

    废话不多说,直接贴代码

    首先需要对comBox扩展,下拉内容变成TreeView

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

    }
    然后需要扩展DataGridViewTextBoxCell为DataGridViewTreeViewCell
    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 "";
    }
    }
    }
    }
    接着扩展DataGridViewColumn 支持DataGridViewTreeViewColumn 下拉树类型
    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;
    }
    }
    }
    }

    最后形成一个新的空间,一个DataGridViewEditingControl
    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;
    }
    }
    }
    }

    以上四步就得到了扩展的基础类,要想使用,必须引用这些类,这样在DataGridView的属性栏的columnType中就会将扩展的DataGridViewTreeViewColumn类型包含进来。
    然后在对DataGridView的初始化操作,就可以添加该类型了

    接下来是测试一下

    需要初始化

    private void dgrdViewDataType_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
    var dgvTree = e.Control as DataGridViewTreeViewEditingControl;
    if (dgvTree != null)
    {
    var root = new TreeNode("评价", 0, 0);
    root.Nodes.Add("烃源岩有机岩相带评价图");
    root.Nodes.Add("有利储存段评价表");
    root.Nodes.Add("储层综合评价图");
    root.Nodes.Add("盖层评价图");
    dgvTree.TreeView.Nodes.Clear();
    dgvTree.TreeView.Nodes.Add(root);
    dgvTree.TreeView.ExpandAll();
    }
    }

    这样每次点击下拉的时候,就会读取数据赋值,并显示下拉树。


    以上就达到了,在DataGridView中显示下拉树的效果,但是还需要能对下拉树查询,这个还有待扩展!
    ————————————————
    版权声明:本文为CSDN博主「GRACE_ETERNITY」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/Shiyaru1314/article/details/51920494

  • 相关阅读:
    girdview
    c#中&&,||的应用
    ToString()和Convert.ToString()的区别
    日期格式化
    线程间操作ui
    基于k3cloud做的东西
    格式化金额字段添加千位符
    SQL 分页查询
    xammp 配置虚拟主机
    jQuery事件对象event的属性和方法
  • 原文地址:https://www.cnblogs.com/ljs-13/p/12108657.html
Copyright © 2011-2022 走看看