• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#——WinformDatagridView上显示下拉树

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#主要字典集合性能对比[转]发布时间:2022-07-10
下一篇:
C#:ZedGraph画图控件(待补充)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap