自己创建以个 ComboBox的继承类,实现在他的下拉列表中显示一张表(datagrid)
class GridCombobox:ComboBox
{
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
ToolStripControlHost gridviewHost;
ToolStripDropDown dropDown;
public GridCombobox( )
{
DataGrid datagrid = new DataGrid();
datagrid.BorderStyle = BorderStyle.None;
datagrid.ColumnHeadersVisible = false;
datagrid.DataSource = mypublic.datatable;//这句已经把datagrid与数据源连接起来,而且数据源有正确的数据
datagrid.BackgroundColor = Color.White;
gridviewHost = new ToolStripControlHost(datagrid);
dropDown = new ToolStripDropDown();
dropDown.Width = this.Width;
dropDown.Items.Add(gridviewHost);
}
public DataGrid DataGrid
{
get { return gridviewHost.Control as DataGrid; }
}
private void ShowDropDown()
{
if (dropDown != null)
{
gridviewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
dropDown.Show(this, 0, this.Height);
}
}
}
但是我在一个Form上创建了类GridCombobox的一个对象,这个对象的下拉列表中总是没有数据呢??????
class GridCombobox:ComboBox
{
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
ToolStripControlHost gridviewHost;
ToolStripDropDown dropDown;
public GridCombobox( )
{
DataGrid datagrid = new DataGrid();
datagrid.BorderStyle = BorderStyle.None;
datagrid.ColumnHeadersVisible = false;
datagrid.DataSource = mypublic.datatable;//这句已经把datagrid与数据源连接起来,而且数据源有正确的数据
datagrid.BackgroundColor = Color.White;
gridviewHost = new ToolStripControlHost(datagrid);
dropDown = new ToolStripDropDown();
dropDown.Width = this.Width;
dropDown.Items.Add(gridviewHost);
}
public DataGrid DataGrid
{
get { return gridviewHost.Control as DataGrid; }
}
private void ShowDropDown()
{
if (dropDown != null)
{
gridviewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
dropDown.Show(this, 0, this.Height);
}
}
}
但是我在一个Form上创建了类GridCombobox的一个对象,这个对象的下拉列表中总是没有数据呢??????
一开始里面是用的datagridview,这样可以实现,但是做出来很丑,不代滚动条,而且右边与下边有很大缝隙》
From frmDataSource = new Form();
frmDataSource.Controls.Add(datagrid)