.net的各种控件可不可以相互叠加?比如说,我现在用一个datagrid,里面某个cell可不可以用datepickcontrol?

longaway 2004-07-22 10:28:30
其实,主要意思就是如何定制控件?

大家多帮忙!多提提思路,或者例子,源码。
先行谢过。
...全文
178 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengfanpp 2004-08-01
  • 打赏
  • 举报
回复
收藏!
lehehe 2004-08-01
  • 打赏
  • 举报
回复
呵呵,楼上的几位都谈的不错!!我想也是可以的,只是具体操作上可能会有一些问题,得多多实践啊!!
marvelstack 2004-07-31
  • 打赏
  • 举报
回复
如果是在windows下。同加一个combobox差不多吧,楼主可以参考一下。
http://blog.csdn.net/zhzuo/archive/2004/05/31/22036.aspx
exboy 2004-07-22
  • 打赏
  • 举报
回复
可以啊,写程序控制一下。

主要在 ItemCreated 和 ItemDataBound 事件中处理

private void dataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
e.Item.Cells[0].Controls.Add(new LiteralControl("一个控件"));
}
}
上面的程序在datagrid 控件中的每一行的第一列加了一个 LiteralControl控件,其他的控件跟加这个控件的原理一样
598 2004-07-22
  • 打赏
  • 举报
回复
把TimePicker做成列样式,其它类似(怎么用列样式不用我再说了吧)

我贴再此
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public class DataGridTimePickerColumnStyle : 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 DataGridTimePickerColumnStyle() : 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);
}

/// <summary>
/// 此方法被重写,使当双击列边线,设置单元格的大小(与其中的TimePicker适应)
/// </summary>
/// <param name="g"></param>
/// <param name="value"></param>
/// <returns></returns>
protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myDateTimePicker.PreferredHeight + 4);
}

/// <summary>
/// 设置单元格的最小高度(与其中的TimePicker适应)
/// </summary>
/// <returns></returns>
protected override int GetMinimumHeight()
{
return myDateTimePicker.PreferredHeight + 4;
}

/// <summary>
/// 此方法被重写,使当双击行边线,设置单元格的高度(与其中的TimePicker适应)
/// </summary>
/// <param name="g"></param>
/// <param name="value"></param>
/// <returns></returns>
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);
}
}

17,747

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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