怎样在dataGridView的列头添加combobox

idaydayup 2011-03-17 02:21:37
我想在datagridview每个列的列头添加一个combobox,但是不知道怎么绘制combobox

我用这个方法添加了checkbox列头

class datagridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell
{
Point checkBoxLocation;
Size checkBoxSize;
bool _checked = false;
Point _cellLocation = new Point();
System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
public event datagridviewcheckboxHeaderEventHander OnCheckBoxClicked;
//绘制列头checkbox
protected override void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds,
System.Drawing.Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
dataGridViewElementState, value,
formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
Point p = new Point();
Size s = CheckBoxRenderer.GetGlyphSize(graphics,
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
p.X = cellBounds.Location.X +
(cellBounds.Width / 2) - (s.Width / 2) - 1;//列头checkbox的X坐标
p.Y = cellBounds.Location.Y +
(cellBounds.Height / 2) - (s.Height / 2);//列头checkbox的Y坐标
_cellLocation = cellBounds.Location;
checkBoxLocation = p;
checkBoxSize = s;
if (_checked)
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.CheckedNormal;
else
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox
(graphics, checkBoxLocation, _cbState);
}
}

但是不知道怎样用这个方法绘制一个combobox,我在网上找到下面这个方法,但是好像只能绘制出个箭头

protected override void Paint(
Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// Use the base method to paint the default appearance.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
cellState, value, formattedValue,
errorText, cellStyle, advancedBorderStyle, paintParts);
// Retrieve the current button bounds.
Rectangle buttonBounds = DropDownButtonBounds;
// Continue only if the buttonBounds is big enough to draw.
if (buttonBounds.Width < 1 || buttonBounds.Height < 1) return;
if (Application.RenderWithVisualStyles)
{
ComboBoxState state = ComboBoxState.Normal;
//if (dropDownListBoxShowing)
//{
// state = ComboBoxState.Pressed;
//}
ComboBoxRenderer.DrawDropDownButton(
graphics, buttonBounds, state);
}
else
{
// Determine the pressed state in order to paint the button
// correctly and to offset the down arrow.
Int32 pressedOffset = 0;
PushButtonState state = PushButtonState.Normal;
//if (dropDownListBoxShowing)
//{
// state = PushButtonState.Pressed;
// pressedOffset = 1;
//}
ButtonRenderer.DrawButton(graphics, buttonBounds, state);
graphics.DrawPolygon(SystemPens.ControlText, new Point[] {
new Point(
buttonBounds.Width / 2 +
buttonBounds.Left - 1 + pressedOffset,
buttonBounds.Height * 3 / 4 +
buttonBounds.Top - 1 + pressedOffset),
new Point(
buttonBounds.Width / 4 +
buttonBounds.Left + pressedOffset,
buttonBounds.Height / 2 +
buttonBounds.Top - 1 + pressedOffset),
new Point(
buttonBounds.Width * 3 / 4 +
buttonBounds.Left - 1 + pressedOffset,
buttonBounds.Height / 2 +
buttonBounds.Top - 1 + pressedOffset)
});
}

}



谁有比较好的办法实现呢?
...全文
343 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
idaydayup 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hj_daxian 的回复:]

引用 6 楼 zerodegrees 的回复:
不需要绘制,datatridvie里本身就有combobox。
类型是DataGridViewComboBoxColumn。
我觉得系统自带的combobox应该能满足你的需求了。
你好好看看datagridview的ColumnType吧


+
[/Quote]DataGridViewComboBoxColumn 是设置列的格式的 但是列头上没法用这个添加combobox吧?
idaydayup 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hello_wys 的回复:]

在窗体的编辑界面,在DataGridView上右键,添加列,在列的类型里选择datagridviewcomboboxcolumn这个就可以了啊
就把一个下拉框添加进来了
可以给它附加数据源代码:
C# code
string sql = "select ck_name from WH_cangku";
DataSet ds = new DataSet();
……
[/Quote]DataGridViewComboBoxColumn 是设置列的格式的 但是列头上没法用这个添加combobox吧?
Just4life 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fangxinggood 的回复:]

[/Quote]

right
nishierzhizhu 2011-03-17
  • 打赏
  • 举报
回复
列头不是列,四楼的应该可以吧
Uplookingsh 2011-03-17
  • 打赏
  • 举报
回复
的确是不需要绘制,因为datatridvie里本身就有combobox。类型是DataGridViewComboBoxColumn。
正如LS所说的系统本身自带的combobox就能满足你的需求了。
LZ还是要好好看看的 慢慢就会更好的 加油哈~
http://www.uplookingsh.com
哥本哈根 2011-03-17
  • 打赏
  • 举报
回复
在窗体的编辑界面,在DataGridView上右键,添加列,在列的类型里选择datagridviewcomboboxcolumn这个就可以了啊
就把一个下拉框添加进来了
可以给它附加数据源代码:
 string sql = "select ck_name from  WH_cangku";
DataSet ds = new DataSet();
ds = Maticsoft.DBUtility.DbHelperSQL.Query(sql);//这里是执行sql语句的
this.Column19.DataSource = ds.Tables[0];
this.Column19.DisplayMember = "ck_name";
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zerodegrees 的回复:]
不需要绘制,datatridvie里本身就有combobox。
类型是DataGridViewComboBoxColumn。
我觉得系统自带的combobox应该能满足你的需求了。
你好好看看datagridview的ColumnType吧
[/Quote]

+
zerodegrees 2011-03-17
  • 打赏
  • 举报
回复
不需要绘制,datatridvie里本身就有combobox。
类型是DataGridViewComboBoxColumn。
我觉得系统自带的combobox应该能满足你的需求了。
你好好看看datagridview的ColumnType吧
机器人 2011-03-17
  • 打赏
  • 举报
回复
勇敢的心515 2011-03-17
  • 打赏
  • 举报
回复
winform不是很清楚,应该也可以吧。
idaydayup 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jolyloving 的回复:]
直接在datagridview里加各模版列,里面放一个combobox就好啦
[/Quote]
这是winform 不是webform的啊 哪来的模板列呢?
刘婷婷 2011-03-17
  • 打赏
  • 举报
回复
直接在datagridview里加各模版列,里面放一个combobox就好啦

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧