请各版主支招,DataGridView自定义列类型问题,始终调不好,我快要跳楼了

jcyluck 2012-09-12 12:28:21
向各版主求救。
我写了一个自定义控件,现在想所这个自定义控件加到DataGridView里,变成一个列类型,从网上下了一部分代码,但始终达不到我需要的结果,主要的问题是,
1.如果单元格没有值,从日期控件中选择日期后,日期不能在单元格中显示。
2.如果单元格有值,从日期控件中选择日期后,选择的值也不能及时在单元格中更新。

估计这两个问题同属一个问题,但我才疏学浅啊,调了两天都没调好,几近崩溃。万般无奈,还请各位支招,帮我看看问题出在哪里了,不胜感激。

代码下载地址:
http://download.csdn.net/detail/jcyluck/4564305
...全文
462 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
lishizhi 2013-04-09
  • 打赏
  • 举报
回复
终于搞定了,谢谢Sandy945
y_keven 2012-12-14
  • 打赏
  • 举报
回复
jcyluck 2012-09-18
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 的回复:]
上面的代码是用DateTimePicker实现的,效果应该和你预期的差不多。

你的代码我调试了一下,第一个问题是由于事件不对导致的,可以解决。
第二个问题是值没有回填,具体原因暂时不知。
我时间有限,就用DateTimePicker写了个。
[/Quote]

谢谢,我再研究一下。
jcyluck 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 的回复:]
建第一个类,如名为:numtextbox(继承自DataGridViewTextBoxEditingControl)
重写PrepareEditingControlForEdit过程,在这里隐藏基类,并将自定义控件加入基类的父控件内,在这个类里你在自定义控件的值改变时将新值赋给基类的text属性
---------------------------------------
建第二个类,如名……
[/Quote]


谢谢您的答复,我再仔细研究一下,如果还有问题,再QQ请教您。
阿非 2012-09-17
  • 打赏
  • 举报
回复
上面的代码是用DateTimePicker实现的,效果应该和你预期的差不多。

你的代码我调试了一下,第一个问题是由于事件不对导致的,可以解决。
第二个问题是值没有回填,具体原因暂时不知。
我时间有限,就用DateTimePicker写了个。
阿非 2012-09-17
  • 打赏
  • 举报
回复

public class CustomCalendar_Cell : DataGridViewTextBoxCell
{
public CustomCalendar_Cell()
: base()
{
// Use the short date format.
this.Style.Format = "d";
}

public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
if (this.Value != null)
{
ctl.Value = DateTime.Parse(this.Value.ToString());
}
else
{
ctl.Value = DateTime.Now;
}
}

public override Type EditType
{
get
{
// Return the type of the editing contol that UCL_Datetime_Cell uses.
return typeof(CalendarEditingControl);
}
}

public override Type ValueType
{
get
{
// Return the type of the value that UCL_Datetime_Cell contains.
return typeof(DateTime);
}
}

public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return DateTime.Now;
}
}
}


public class CustomCalendar_Column : DataGridViewColumn
{
public CustomCalendar_Column()
: base(new CustomCalendar_Cell())
{
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CustomCalendar_Cell.
if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomCalendar_Cell)))
{
throw new InvalidCastException("Must be a CustomCalendar_Cell");
}
base.CellTemplate = value;
}
}
public override object Clone()
{
CustomCalendar_Column col = (CustomCalendar_Column)base.Clone();

return col;
}

}
阿非 2012-09-17
  • 打赏
  • 举报
回复

public class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public CalendarEditingControl()
{
this.Format = DateTimePickerFormat.Short;
}

// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.Value.ToShortDateString();
}
set
{
if (value is String)
{
try
{
// This will throw an exception of the string is
// null, empty, or not in the format of a date.
this.Value = DateTime.Parse((String)value);
}
catch
{
// In the case of an exception, just use the
// default value so we're not left with a null
// value.
this.Value = DateTime.Now;
}
}
}
}

// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}

// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}

// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}

// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey;
}
}

// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}

// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}

// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}

// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}

// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}

protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
LAN_YT 2012-09-15
  • 打赏
  • 举报
回复
怎么还没跳,等的我都心急。
zj_zwl 2012-09-15
  • 打赏
  • 举报
回复
对了,上面的代码是假定你的自定义控件值为int32类型,思路是一样的.这是不用实现接口,比较简单的实现方法
zj_zwl 2012-09-15
  • 打赏
  • 举报
回复
建第一个类,如名为:numtextbox(继承自DataGridViewTextBoxEditingControl)
重写PrepareEditingControlForEdit过程,在这里隐藏基类,并将自定义控件加入基类的父控件内,在这个类里你在自定义控件的值改变时将新值赋给基类的text属性
---------------------------------------
建第二个类,如名为:numtextboxcell(继承自DataGridViewTextBoxCell)
重写EditType属性,返回numtextbox类型,重写DefaultNewRowValue属性,让它返回0,重写ValueType属性,让它返回int32类型
-----------------------------------------
建第三个类,如名为dgvnumtextboxcolumn(继承自DataGridViewTextBoxColumn),并加上DataGridViewColumnDesignTimeVisible(True)属性.
重写CellTemplate属性,让它返回numtextboxcell类型
------------------------------------------
好了,设计时也可以加入你自定义的列~~
如果还搞不明白,加我QQ:894526138
jcyluck 2012-09-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
文件下载不了,404.
[/Quote]

您再试试这个地址看看,好像有改变
http://download.csdn.net/detail/jcyluck/3965450
jcyluck 2012-09-14
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]
DataGridView单元格编辑时其实是承载了一个改造过的textbox类,你加载的自定义控件的值改变后要把值赋给这个textbox类,这样就可以了,效果就是这要:
http://hiphotos.baidu.com/zj_zwl/pic/item/d33756177773126c4890a74f.jpg
[/Quote]

谢谢您的答复,您说的逻辑我也理解,并且我也想达到这样的效果,关键是达不到啊。。。您能贡献点源码出来让我研究一下吗?谢谢
jcyluck 2012-09-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
文件下载不了,404.
[/Quote]

终于看到您的身影了啊,这个好像是论坛的问题吧,我有时也出现404呢,您有空再帮我看看。谢谢了。。。
zj_zwl 2012-09-14
  • 打赏
  • 举报
回复
DataGridView单元格编辑时其实是承载了一个改造过的textbox类,你加载的自定义控件的值改变后要把值赋给这个textbox类,这样就可以了,效果就是这要:
http://hiphotos.baidu.com/zj_zwl/pic/item/d33756177773126c4890a74f.jpg
阿非 2012-09-14
  • 打赏
  • 举报
回复
文件下载不了,404.
jcyluck 2012-09-13
  • 打赏
  • 举报
回复
晕菜得很,居然没人理我,还得自己顶起。。。求高人。。。
jcyluck 2012-09-12
  • 打赏
  • 举报
回复
唉呀,sandy945还在,以前帮我看过这里面那个控件的问题,那问题到现在还没解决,只有用其他方法替代了,如果有空,再帮我看看个问题,谢谢。
jcyluck 2012-09-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
问题1:dataGridView中的属性editmode要改为直接键入EditOnEnter
问题2:month_DateSelected赋值方法有问题
[/Quote]

谢谢,与第一个问题无关,问题2,好像也没有关系,这个控件在其他地方用得正常,只是添加到dataGridView中才出现这问题。
ryenlove 2012-09-12
  • 打赏
  • 举报
回复
问题1:dataGridView中的属性editmode要改为直接键入EditOnEnter
问题2:month_DateSelected赋值方法有问题
ryenlove 2012-09-12
  • 打赏
  • 举报
回复
问题1:dataGridView中的属性editmode要改为直接键入EditOnEnter
问题2:month_DateSelected赋值方法有问题
加载更多回复(3)

110,537

社区成员

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

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

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