C#仿QQ皮肤-ComboBoxDataGridView

C#仿QQ皮肤-实现原理系列文章导航
http://www.cnblogs.com/sufei/archive/2010/03/10/1682847.html

大家还是先来看看效果吧

C#仿QQ皮肤-ComboBoxDataGridView

下面我们一起来看看是怎么样实现的


这个其实是控件里是没有的,那天在博客上看到一个同志在写这个控件,所性抄一点思想,在结合我和控件就加上了,还挺好用的,呵呵,有了这个控件大家就不用再为绑定一个表那个字段面苦了,可以一下自全部绑定,实现很简单大家一起来看看吧

第一步我们新建一个组件名称 为ComboBoxDataGridView

其实就是dataGridViewHost 和ToolStripDropDown之间的相互配合

这个控件是继承自我皮肤里的控件Combox这个大家一会儿看代码

我们要声明一个委托来调用 构造DataGridViw的方法

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> /// /声明一个委托
public delegate void ComboBoxDataGridViewScroll( object sender, bool vscroll);
public event ComboBoxDataGridViewScrollOnScroll;
private const int WM_HSCROLL = 0x114 ;
private const int WM_VSCROLL = 0x115 ;



public ComboBoxDataGridView()
{
this .SetStyle(ControlStyles.OptimizedDoubleBuffer, true );
DrawDataGridView();
this .OnScroll += new ComboBoxDataGridViewScroll(ComboBoxDataGridView_OnScroll);
}

我们先来看看DrawDataGridView();
方法吧

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> private void DrawDataGridView()
{
CRD.WinUI.Editors.DataGridViewdataGridView
= new CRD.WinUI.Editors.DataGridView();
dataGridView.RowHeadersVisible
= false ;
dataGridView.RowTemplate.Resizable
= DataGridViewTriState.False; // 固定行高不允许调节
dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView.BackgroundColor
= SystemColors.ActiveCaptionText;
dataGridView.BorderStyle
= BorderStyle.None;
dataGridView.ReadOnly
= true ;
dataGridView.AllowUserToAddRows
= false ;
dataGridView.SelectionMode
= DataGridViewSelectionMode.FullRowSelect;
dataGridView.Click
+= new EventHandler(dataGridView_Click);

// 设置DataGridView的数据源
CRD.WinUI.Forms.EntryFormfrmDataSource = new CRD.WinUI.Forms.EntryForm();
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);
}

有关DatagridView和Combox的实现 代码大家请参考我的皮肤系统,呵呵

下面我把代码都放上来各们看看吧也提提建议

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace CRD.WinUI.Misc
{
public partial class ComboBoxDataGridView:CRD.WinUI.Misc.ComboBox
{
// 成员变量
private const int WM_LBUTTONDOWN = 0x201 ,WM_LBUTTONDBLCLK = 0x203 ;
ToolStripControlHostdataGridViewHost;
ToolStripDropDowndropDown;
private string m_sDefaultColumn;
private bool m_blPopupAutoSize = false ;
private DataGridViewRowm_dgvRow;
public event EventHandlerAfterSelector;
/**/
/// /声明一个委托
public delegate void ComboBoxDataGridViewScroll( object sender, bool vscroll);
public event ComboBoxDataGridViewScrollOnScroll;
private const int WM_HSCROLL = 0x114 ;
private const int WM_VSCROLL = 0x115 ;



public ComboBoxDataGridView()
{
this .SetStyle(ControlStyles.OptimizedDoubleBuffer, true );
DrawDataGridView();
this .OnScroll += new ComboBoxDataGridViewScroll(ComboBoxDataGridView_OnScroll);
}

public void ComboBoxDataGridView_OnScroll( object sender, bool vscroll)
{
this .dataGridViewHost.Focus();
}


[Description(
" 设置DataGridView属性 " ),Browsable( true ),Category( " N8 " )]
public DataGridViewDataGridView
{
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 DefaultColumn
{
set
{
m_sDefaultColumn
= value;
}
get
{
if (m_sDefaultColumn == null )
{
return String.Empty;
}
else
{
return m_sDefaultColumn;
}
}
}

// 绘制DataGridView以及下拉DataGridView
private void DrawDataGridView()
{
CRD.WinUI.Editors.DataGridViewdataGridView
= new CRD.WinUI.Editors.DataGridView();
dataGridView.RowHeadersVisible
= false ;
dataGridView.RowTemplate.Resizable
= DataGridViewTriState.False; // 固定行高不允许调节
dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView.BackgroundColor
= SystemColors.ActiveCaptionText;
dataGridView.BorderStyle
= BorderStyle.None;
dataGridView.ReadOnly
= true ;
dataGridView.AllowUserToAddRows
= false ;
dataGridView.SelectionMode
= DataGridViewSelectionMode.FullRowSelect;
dataGridView.Click
+= new EventHandler(dataGridView_Click);

// 设置DataGridView的数据源
CRD.WinUI.Forms.EntryFormfrmDataSource = new CRD.WinUI.Forms.EntryForm();
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);
}

public void dataGridView_Click( object sender,EventArgse)
{
PopupGridView(e);
}

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,EventArgse)
{
PopupGridView(e);
}

// PopupGridView


/**/
/// <summary>
/// 弹出下拉表格并触发选择后事件
/// </summary>
/// <paramname="e"></param>
private void PopupGridView(EventArgse)
{
if (DataGridView.SelectedRows.Count > 0 )
{
m_dgvRow
= DataGridView.SelectedRows[ 0 ];
if (m_sDefaultColumn != String.Empty)
{
string []sColumnList = m_sDefaultColumn.Split( ' , ' );
foreach ( string sColumn in sColumnList)
{
if (DataGridView.Columns.Contains(sColumn))
{
Items.Clear();
Items.Add(m_dgvRow.Cells[sColumn].Value.ToString());
SelectedIndex
= 0 ;
}
}
}

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

// 重写方法


protected override void WndProc( ref Messagem)
{
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN || m.Msg == 0x114 || m.Msg == 0x115 )
{
ShowDropDown();
if (OnScroll != null )
{
OnScroll(
this ,m.Msg == 0x115 );
}
return ;
}
base .WndProc( ref m);

}

protected override void Dispose( bool disposing)
{
if (disposing)
{
if (dropDown != null )
{
dropDown.Dispose();
dropDown
= null ;
}
}
base .Dispose(disposing);
}
}
}

欢迎大家转载,如有转载请注明文章来自:http://sufei.cnblogs.com/

签名:做一番一生引以为豪的事业;在有生之年报答帮过我的人;并有能力帮助需要帮助的人;

QQ:361983679 Email:[email protected] MSN:[email protected]


你可能感兴趣的:(datagridview)