c# - Winform DatagridView上显示下拉树
2021-01-21 17:13
标签:height hang 协议 obj details 格式 读取 dash 综合 Winform的DatagridView是不支持下拉树的,所以需要扩展 废话不多说,直接贴代码 首先需要对comBox扩展,下拉内容变成TreeView using System.Drawing; 最后形成一个新的空间,一个DataGridViewEditingControl 以上四步就得到了扩展的基础类,要想使用,必须引用这些类,这样在DataGridView的属性栏的columnType中就会将扩展的DataGridViewTreeViewColumn类型包含进来。 接下来是测试一下 需要初始化 private void dgrdViewDataType_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 这样每次点击下拉的时候,就会读取数据赋值,并显示下拉树。 c# - Winform DatagridView上显示下拉树 标签:height hang 协议 obj details 格式 读取 dash 综合 原文地址:https://www.cnblogs.com/ljs-13/p/12108657.html
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;
}
}
}
}
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);
}
///
/// 在Cell被编辑的时候光标显示
///
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
}
///
/// 获取或设置所在的DataGridView
///
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
///
/// 获取或设置格式化后的值
///
public object EditingControlFormattedValue
{
set
{
Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
get
{
return this.Text;
}
}
///
/// 获取控件的Text值
///
/// 错误上下文
///
public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return Text;
}
///
/// 编辑键盘
///
///
///
///
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;
}
}
///
/// 控件所在行
///
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
///
/// 设置样式
///
///
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
///
/// 是否值发生了变化
///
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
this.valueChanged = value;
}
}
}
}
然后在对DataGridView的初始化操作,就可以添加该类型了
{
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
文章标题:c# - Winform DatagridView上显示下拉树
文章链接:http://soscw.com/index.php/essay/45065.html