DataGird的显示问题

gjzhou 2004-11-25 03:10:48
怎么才能在单击datagrid的一列中的一个单元格时弹出一个listBox之类的控件,并将选择的值赋给相应单元格,是在WinForm下。
...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
97ce_twinkle 2004-12-21
  • 打赏
  • 举报
回复
虽然已经结贴了,留段代码,给以后的搜索到的人参考吧

微软自带的帮助里的,往datagrid单元格里面加一个timepicker控件。

public class DataGridTimePickerColumn : DataGridColumnStyle
{
private DateTimePicker myDateTimePicker = new DateTimePicker();
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;

public DataGridTimePickerColumn() : base()
{
myDateTimePicker.Visible = false;
}

protected override void Abort(int rowNum)
{
isEditing = false;
myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);
Invalidate();
}

protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
myDateTimePicker.Bounds = Rectangle.Empty;

myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);

if (!isEditing)
return true;

isEditing = false;

try
{
DateTime value = myDateTimePicker.Value;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}

Invalidate();
return true;
}

protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
DateTime value = (DateTime)
GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myDateTimePicker.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
myDateTimePicker.Value = value;
myDateTimePicker.Visible = true;
myDateTimePicker.ValueChanged +=
new EventHandler(TimePickerValueChanged);
}
else
{
myDateTimePicker.Value = value;
myDateTimePicker.Visible = false;
}

if (myDateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}

protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myDateTimePicker.PreferredHeight + 4);
}

protected override int GetMinimumHeight()
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override int GetPreferredHeight(Graphics g,
object value)
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
DateTime date = (DateTime)
GetColumnValueAtRow(source, rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(date.ToString("d"),
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}

protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (myDateTimePicker.Parent != null)
{
myDateTimePicker.Parent.Controls.Remove
(myDateTimePicker);
}
if (value != null)
{
value.Controls.Add(myDateTimePicker);
}
}

private void TimePickerValueChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(myDateTimePicker);
}
}
gjzhou 2004-12-13
  • 打赏
  • 举报
回复
唉,怎么会这样呢!!!!!!!!
gjzhou 2004-11-26
  • 打赏
  • 举报
回复
谢谢各位,我做了一个了,有人再给个源码参考一下吗???
goodlucktome 2004-11-25
  • 打赏
  • 举报
回复
up
allanli 2004-11-25
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3434/3434227.xml?temp=.986416
gjzhou 2004-11-25
  • 打赏
  • 举报
回复
怎么定位?
zhanghw20030303 2004-11-25
  • 打赏
  • 举报
回复
如果弹出的话也是用窗体作为容器!不能够直接弹出。

110,539

社区成员

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

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

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