请教一个dataGridView的自定义Column的问题。 dataGridView中提供了几个默认的DataGridViewColumn,比如DataGridViewButtonColumn。我想设置另外的DataGridViewColumn的改怎么做。比如calendar,或者我自己的控件。最好有代码,谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 www.microsoft.com/china/msdn/archives/ library/dnaspp/html/creatingcustomcolumns.aspwww.pcdog.com/p/html/20041219/191220046713_1.htm asp.net的例子 http://mech.ncu.edu.cn/99star/blogview.asp?logID=67 可以用代码来实现也可以用编辑模板。我感觉用代码自己写方便 <Columns> <asp:BoundField DataField="GRD_ID" HeaderText="序号" InsertVisible="False" ReadOnly="True" SortExpression="GRD_ID" /> <asp:BoundField DataField="GRD_NAME" HeaderText="年级名" SortExpression="GRD_NAME" /> <asp:TemplateField HeaderText="部名" SortExpression="GRD_DEPID"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("DEP_NAME") %>' Width="121px"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="DEP_DropDownList" DataSourceID="SqlDataSource1" DataTextField="DEP_NAME" DataValueField="DEP_ID" SelectedValue='<%# Bind("DEP_ID") %>' runat="server"></asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CancelText="取消" DeleteText="删除" EditText="编辑" UpdateText="更新" /> </Columns> using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;namespace SourceCenter{ public class CalendarCell : DataGridViewTextBoxCell { public CalendarCell() : base() { } public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) { CalendarEditingControl cnnc; CalendarColumn cnncl; DataGridViewColumn dgvc; base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); cnnc = DataGridView.EditingControl as CalendarEditingControl; dgvc = this.OwningColumn; if (dgvc is CalendarColumn) { cnncl = dgvc as CalendarColumn; cnnc.Value = (this.Value != null) ? DateTime.Parse(this.Value.ToString()) : DateTime.Now; } } public override Type EditType { get { return typeof(CalendarEditingControl); } } } public class CalendarColumn : DataGridViewColumn { public CalendarColumn() : base(new CalendarCell()) { } public override DataGridViewCell CellTemplate { get { return base.CellTemplate; } set { // Only cell types that derive from CalendarCell are supported as the cell template. if (value != null && !value.GetType().IsAssignableFrom(typeof(CalendarCell))) { string s = "Cell type is not based upon the CalendarCell.";//CustomColumnMain.GetResourceManager().GetString("excNotCalendar"); throw new InvalidCastException(s); } base.CellTemplate = value; } } } public class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl { protected int rowIndex; protected DataGridView dataGridView; protected bool valueChanged = false; public CalendarEditingControl() { this.Format = DateTimePickerFormat.Short; //this.BorderStyle = BorderStyle.FixedSingle; //this.Maximum = 800; } protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); NotifyDataGridViewOfValueChange(); } protected virtual void NotifyDataGridViewOfValueChange() { this.valueChanged = true; if (this.dataGridView != null) { this.dataGridView.NotifyCurrentCellDirty(true); } } public object EditingControlFormattedValue { set { this.Value = DateTime.Parse(value.ToString()); NotifyDataGridViewOfValueChange(); } get { return this.Value.ToShortDateString(); } } public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) { return this.Value.ToShortDateString(); } public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey) { return !dataGridViewWantsInputKey; } public void PrepareEditingControlForEdit(bool selectAll) { if (selectAll) this.Select(true,true); } public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle) { //this.Font = dataGridViewCellStyle.Font; //this.ForeColor = dataGridViewCellStyle.ForeColor; //this.BackColor = dataGridViewCellStyle.BackColor; //this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment); } public int EditingControlRowIndex { get { return this.rowIndex; } set { this.rowIndex = value; } } public bool EditingControlValueChanged { get { return valueChanged; } set { this.valueChanged = value; } } public bool RepositionEditingControlOnValueChange { get { return false; } } public Cursor EditingPanelCursor { get { return Cursors.IBeam; } } public DataGridView EditingControlDataGridView { get { return this.dataGridView; } set { this.dataGridView = value; } } }} treeview高度问题 数据库访问问题 请教一个函数指针?委托?的问题 怎么用这个系统? ADO.net数据库应用问题 如何让一个程序后台运行? borderstyle问题 写了个 listview 延时加载 缩略图的 代码,但是老是陷入死循环,求指点 C#中如何自动登录到需要cookie的网站 ◆现在大家都使用什么测试管理软件?◆ 求助:弹出一个“公共语言运行库调试服务”对话框,怎么回事? c#中一个string中,从某个字符或者字符串 开始全部去掉 怎么写的?
www.pcdog.com/p/html/20041219/191220046713_1.htm asp.net的例子
也可以用编辑模板。
我感觉用代码自己写方便
<Columns>
<asp:BoundField DataField="GRD_ID" HeaderText="序号" InsertVisible="False" ReadOnly="True"
SortExpression="GRD_ID" />
<asp:BoundField DataField="GRD_NAME" HeaderText="年级名" SortExpression="GRD_NAME" />
<asp:TemplateField HeaderText="部名" SortExpression="GRD_DEPID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DEP_NAME") %>' Width="121px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DEP_DropDownList" DataSourceID="SqlDataSource1" DataTextField="DEP_NAME" DataValueField="DEP_ID" SelectedValue='<%# Bind("DEP_ID") %>' runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CancelText="取消" DeleteText="删除" EditText="编辑" UpdateText="更新" />
</Columns>
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace SourceCenter
{ public class CalendarCell : DataGridViewTextBoxCell
{
public CalendarCell()
: base()
{
} public override void InitializeEditingControl(int rowIndex,
object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
CalendarEditingControl cnnc;
CalendarColumn cnncl;
DataGridViewColumn dgvc; base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle); cnnc = DataGridView.EditingControl as CalendarEditingControl; dgvc = this.OwningColumn;
if (dgvc is CalendarColumn)
{
cnncl = dgvc as CalendarColumn;
cnnc.Value = (this.Value != null) ? DateTime.Parse(this.Value.ToString()) : DateTime.Now;
}
} public override Type EditType
{
get
{
return typeof(CalendarEditingControl);
}
} } public class CalendarColumn : DataGridViewColumn
{
public CalendarColumn()
: base(new CalendarCell())
{
} public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
} set
{
// Only cell types that derive from CalendarCell are supported as the cell template.
if (value != null && !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
{
string s = "Cell type is not based upon the CalendarCell.";//CustomColumnMain.GetResourceManager().GetString("excNotCalendar");
throw new InvalidCastException(s);
} base.CellTemplate = value;
}
}
}
public class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
protected int rowIndex;
protected DataGridView dataGridView;
protected bool valueChanged = false; public CalendarEditingControl()
{
this.Format = DateTimePickerFormat.Short;
//this.BorderStyle = BorderStyle.FixedSingle;
//this.Maximum = 800;
} protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
} protected virtual void NotifyDataGridViewOfValueChange()
{
this.valueChanged = true;
if (this.dataGridView != null)
{
this.dataGridView.NotifyCurrentCellDirty(true);
}
}
public object EditingControlFormattedValue
{
set
{
this.Value = DateTime.Parse(value.ToString());
NotifyDataGridViewOfValueChange();
}
get
{
return this.Value.ToShortDateString();
} } public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.Value.ToShortDateString();
} public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
return !dataGridViewWantsInputKey;
} public void PrepareEditingControlForEdit(bool selectAll)
{
if (selectAll) this.Select(true,true);
} public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
//this.Font = dataGridViewCellStyle.Font;
//this.ForeColor = dataGridViewCellStyle.ForeColor;
//this.BackColor = dataGridViewCellStyle.BackColor;
//this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
} public int EditingControlRowIndex
{
get
{
return this.rowIndex;
} set
{
this.rowIndex = value;
}
} public bool EditingControlValueChanged
{
get
{
return valueChanged;
} set
{
this.valueChanged = value;
}
} public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
} public DataGridView EditingControlDataGridView
{
get
{
return this.dataGridView;
} set
{
this.dataGridView = value;
}
}
}
}