前一段时间,我写个两个控件,
ComboBoxTreeView(下拉列表框弹出树) 与ToolStripComboBoxTreeView(下拉列表框工具条弹出树) ,不知道对大家在开发的过程中是否有帮助,今天继续发布VS2005下的ComboBox系列控件中的ComboBoxDataGridView (下拉列表弹出DataGridView),思路与ComboBoxTreeView差不多,唯一不同的是,在数据源设置的地方,大家可以看代码,也希望大家能够踊跃留言,把你们的问题说出来,以便我能开发更好的更实用的控件给大家.在以后的几天当中,我将陆续发布VS2005下的一些控件,请大家关注,好了,下面大家请看附图:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
namespace WindowsApplication21
{
public class ComboBoxDataGridView : ComboBox
{
成员变量#region 成员变量
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
ToolStripControlHost dataGridViewHost;
ToolStripDropDown dropDown;
private string m_sDefaultColumn;
private string m_Separator = "|";
private bool m_blPopupAutoSize = false;
private DataGridViewRow m_dgvRow;
public event EventHandler AfterSelector;
#endregion
构造函数#region 构造函数
public ComboBoxDataGridView()
{
DrawDataGridView();
}
#endregion
属性#region 属性
[Description("设置DataGridView属性"), Browsable(true), Category("N8")]
public DataGridView DataGridView
{
get
{
return dataGridViewHost.Control as DataGridView;
}
}
[Description("下拉表格尺寸是否为自动"), Browsable(true), Category("N8")]
public bool PopupGridAutoSize
{
set
{
m_blPopupAutoSize = value;
}
}
[Description("分割符号"), Browsable(true), Category("N8")]
public string SeparatorChar
{
set
{
m_Separator = value;
}
}
[Description("设置默认值"), Browsable(true), Category("N8")]
public string DefaultColumn
{
set
{
m_sDefaultColumn = value;
}
get
{
if (m_sDefaultColumn == null)
{
return String.Empty;
}
else
{
return m_sDefaultColumn;
}
}
}
#endregion
方法#region 方法
绘制DataGridView以及下拉DataGridView#region 绘制DataGridView以及下拉DataGridView
private void DrawDataGridView()
{
DataGridView dataGridView = new DataGridView();
dataGridView.BackgroundColor = SystemColors.ActiveCaptionText;
dataGridView.BorderStyle = BorderStyle.None;
dataGridView.ReadOnly = true;
dataGridView.AllowUserToAddRows = false;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.DoubleClick += new EventHandler(dataGridView_DoubleClick);
//设置DataGridView的数据源
Form frmDataSource = new Form();
frmDataSource.Controls.Add(dataGridView);
frmDataSource.SuspendLayout();
dataGridViewHost = new ToolStripControlHost(dataGridView);
dataGridViewHost.AutoSize = m_blPopupAutoSize;
dropDown = new ToolStripDropDown();
dropDown.Width = this.Width;
dropDown.Items.Add(dataGridViewHost);
}
#endregion
public string GetDataProperty(string sColumn)
{
string sValue = "";
if (m_dgvRow != null)
{
if (DataGridView.Columns.Contains(sColumn))
{
sValue = m_dgvRow.Cells[sColumn].Value.ToString();
}
}
return sValue;
}
public void dataGridView_DoubleClick(object sender, EventArgs e)
{
PopupGridView(e);
}
/**//// <summary>
/// 弹出下拉表格并触发选择后事件
/// </summary>
/// <param name="e"></param>
private void PopupGridView(EventArgs e)
{
if (DataGridView.SelectedRows.Count > 0)
{
m_dgvRow = DataGridView.SelectedRows[0];
if (m_sDefaultColumn != String.Empty)
{
Text = "";
string[] sColumnList = m_sDefaultColumn.Split(',');
foreach (string sColumn in sColumnList)
{
if (DataGridView.Columns.Contains(sColumn))
{
Text += m_dgvRow.Cells[sColumn].Value.ToString() + m_Separator;
}
}
Text = Text.TrimEnd(m_Separator.ToCharArray());
}
else
{
Text = m_dgvRow.Cells[0].Value.ToString();
}
if (AfterSelector != null)
{
AfterSelector(this,e);
}
}
dropDown.Close();
}
private void ShowDropDown()
{
if (dropDown != null)
{
dataGridViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
dropDown.Show(this, 0, this.Height);
}
}
重写方法#region 重写方法
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);
}
#endregion
#endregion
}
}
下面的代码是客户端调用的方法:
在下面的演示程序中
/Files/CodeAnyWhere/ComboBoxDataGridView.rar