我想在DATABRID中的每一列中都加以个BUTTON,可总是不成功!!
int count = dataGrid1.TableStyles[0].GridColumnStyles.Count;
DataGridTextBoxColumn dgtb1 = (DataGridTextBoxColumn) dataGrid1.TableStyles[0].GridColumnStyles[0];
Button buttona = new Button();
buttona.Text="adfadf";
dgtb1.TextBox.Controls.Add(buttona);
int count = dataGrid1.TableStyles[0].GridColumnStyles.Count;
DataGridTextBoxColumn dgtb1 = (DataGridTextBoxColumn) dataGrid1.TableStyles[0].GridColumnStyles[0];
Button buttona = new Button();
buttona.Text="adfadf";
dgtb1.TextBox.Controls.Add(buttona);
解决方案 »
- 「学与玩」使用分页方式读取超大文件的性能试验
- 泛型判断和文件编码模式问题
- C#生成Excel慢~~~~~啊
- 关于"Windows Communication Foundation之旅"的例程DuplexSample.rar
- 大家好,最近在采用 ZedGraph winform开发时遇到两个问题,希望能解答解答?
- 在winform中打开一个窗口,关闭当前窗口,而焦点在新窗口
- 用C或VC++怎样编写DLL给C#(在VS2005中)调用?
- 高手门帮我看看这个函数怎么错了。取日期的
- 好奇怪的Session 問題.
- 还是关于排序问题!急
- 怎么水晶报表好像打不开带密码的Access文件,有人打开过吗?
- 请问关于ADO.NET的问题
看不懂了,你是winform ??
你应该写自己的DataGridColumnStyle ,手动填充GridColumnStyles,例如ComboBox控件和日期控件就很常用,下面是一种DateTime控件列,本人初学时写的,有参考.net的DataGridTextBoxColumn,
在你机器上可能需要稍做修改才会编译成功,仅供参考,using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel ;
using System.Reflection ;
using System.Drawing.Design;namespace Controls
{
public class DataGridDateTimePickerColumn : DataGridColumnStyle
{
// Fields
private DateTimePicker ctrl;
private int editRow;
private string format;
private IFormatProvider formatInfo;
private string oldValue;
private MethodInfo parseMethod;
private TypeConverter typeConverter;
private int xMargin;
private int yMargin;public DataGridDateTimePickerColumn() : this((PropertyDescriptor) null, (string) null)
{
}
public DataGridDateTimePickerColumn(PropertyDescriptor prop, string format)
{
this.xMargin = 2;
this.yMargin = 1; this.format = null;
this.formatInfo = null;
this.oldValue = null;
this.editRow = -1;
this.ctrl = new DateTimePicker();
this.ctrl.Visible = false;
base.innerControl =ctrl;
this.Format = format;
}
protected override void Abort(int rowNum)
{
this.RollBack();
this.HideEditBox();
this.EndEdit();
}protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
this.ctrl.Bounds = Rectangle.Empty;
if (this.ctrl.IsNavigateMode)
{
return true;
}
try
{
object obj1 = this.ctrl.Text;
if (this.NullText.Equals(obj1) || obj1 ==DBNull.Value )
{
obj1 = System.DBNull.Value;
this.ctrl.Text = this.NullText;
}
else if (((this.format != null) && (this.format.Length != 0)) && (this.parseMethod != null) && (this.FormatInfo != null))
{
object[] objArray1 = new object[2] { this.ctrl.Text, this.FormatInfo } ;
obj1 = this.parseMethod.Invoke(null, objArray1);
if (obj1 is IFormattable)
{
this.ctrl.Text = ((IFormattable) obj1).ToString(this.format, this.formatInfo);
}
else
{
this.ctrl.Text = obj1.ToString();
}
}
else if ((this.typeConverter != null) && this.typeConverter.CanConvertFrom(typeof(string)))
{
obj1 = this.typeConverter.ConvertFromString(this.ctrl.Text);
this.ctrl.Text = this.typeConverter.ConvertToString(obj1);
}
this.SetColumnValueAtRow(dataSource, rowNum, obj1);
return true;
}
catch (Exception ex)
{
System.Console.WriteLine ("在Commit单元格时:" + GetAllErrMessage (ex) );
this.RollBack();
return false;
}
finally
{
this.EndEdit();
}
}/// <summary>
/// 通知列必需放弃它承载的控件的焦点
/// </summary>
protected override void ConcedeFocus()
{
base.ConcedeFocus ();
this.ctrl.Bounds = Rectangle.Empty;
}protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
Rectangle rectangle1 = bounds;
this.ctrl.Enabled = !((readOnly || this.ReadOnly) || this.DataGridTableStyle.ReadOnly);
this.ctrl.Text = this.GetText(this.GetColumnValueAtRow(source, rowNum));
if (this.ctrl.Enabled && (instantText != null))
{
this.DataGridTableStyle.DataGrid.ColumnStartedEdit(bounds);
this.ctrl.IsNavigateMode = false;
this.ctrl.Text = instantText;
}
if (cellIsVisible)
{
this.ctrl.Bounds = bounds;
this.ctrl.Visible = true;
}
else
{
this.ctrl.Bounds = rectangle1;
this.ctrl.Visible = false;
}
this.ctrl.RightToLeft = this.DataGridTableStyle.DataGrid.RightToLeft;
this.ctrl.Focus ();
this.editRow = rowNum;
if (this.ctrl.Enabled)
{
this.oldValue = this.ctrl.Text;
}
if (this.ctrl.Visible)
{
this.DataGridTableStyle.DataGrid.Invalidate(rectangle1);
}
}protected void EndEdit()
{
this.ctrl.IsNavigateMode = true;
this.Invalidate();
}protected override void EnterNullValue()
{
if (!this.ReadOnly && this.ctrl.Visible && this.ctrl.IsNavigateMode)
{
this.ctrl.Text = this.NullText;
this.ctrl.IsNavigateMode = false;
if ((this.DataGridTableStyle != null) && (this.DataGridTableStyle.DataGrid != null))
{
this.DataGridTableStyle.DataGrid.ColumnStartedEdit(this.ctrl.Bounds);
}
}
}
protected override int GetMinimumHeight()
{
return ((base.FontHeight + this.yMargin) + 3);
}protected override int GetPreferredHeight(Graphics g, object value)
{
return this.ctrl.Height + this.yMargin * 2;
}protected override Size GetPreferredSize(Graphics g, object value)
{ Size size1 = Size.Ceiling(g.MeasureString(this.GetText(value), this.DataGridTableStyle.DataGrid.Font));
size1.Width += ((this.xMargin * 2) + 1);
size1.Height = System.Math.Max (size1.Height + this.yMargin * 2 ,this.ctrl.Height + this.yMargin * 2);
return size1;
}
private string GetText(object value)
{
if (value is DBNull || value ==null)
{
return this.NullText;
}
try
{
if (((this.format != null) && (this.format.Length != 0)) && (value is IFormattable))
{
return ((IFormattable) value).ToString(this.format, this.formatInfo);
}
else if ((this.typeConverter != null) && this.typeConverter.CanConvertTo(typeof(string)))
{
return ((string) this.typeConverter.ConvertTo(value, typeof(string)));
}
throw new Exception ();
}
catch
{
return value.ToString();
}
}protected void HideEditBox()
{
bool flag1 = this.ctrl.Focused;
this.ctrl.Visible = false;
if ((flag1 && (this.DataGridTableStyle != null)) && (this.DataGridTableStyle.DataGrid != null) && this.DataGridTableStyle.DataGrid.CanFocus)
{
this.DataGridTableStyle.DataGrid.Focus ();
}
}//a
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
{
this.Paint(g, bounds, source, rowNum, false); //Call b
}//b
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
{
string text1 = this.GetText(this.GetColumnValueAtRow(source, rowNum));
this.PaintText(g, bounds, text1, alignToRight); //Call c
}//c
protected void PaintText(Graphics g, Rectangle bounds, string text, bool alignToRight)
{
Brush backBrush, foreBrush;
foreBrush=new System.Drawing.SolidBrush(base.ForeColor );
backBrush=new System.Drawing.SolidBrush(base.BackColor );
this.PaintText(g, bounds, text,backBrush,foreBrush , alignToRight); //call e
}//d
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
try
{
string text1 = this.GetText(this.GetColumnValueAtRow(source, rowNum));
if (!this.DataGridTableStyle.DataGrid.IsSelected (rowNum))
{
foreBrush=new System.Drawing.SolidBrush(base.InnerControl.ForeColor);
backBrush=new System.Drawing.SolidBrush(base.InnerControl.BackColor);
}
this.PaintText(g, bounds, text1, backBrush, foreBrush, alignToRight); //call e
}
catch(System.Exception ex)
{
System.Console.WriteLine ("在绘制单元格时发生错误:{0}",GetAllErrMessage (ex) );
base.Paint (g,bounds,source,rowNum,backBrush,foreBrush,alignToRight);
}
}
//e
protected void PaintText(Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
{
try
{
Rectangle rectangle1 = textBounds;
StringFormat format1 = new StringFormat(); if (alignToRight)
{
format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
format1.Alignment = (this.Alignment == HorizontalAlignment.Left) ? StringAlignment.Near : ((this.Alignment == HorizontalAlignment.Center) ? StringAlignment.Center : StringAlignment.Far);
format1.FormatFlags |= StringFormatFlags.NoWrap;
g.FillRectangle(backBrush, rectangle1);
rectangle1.Offset(0, 2 * this.yMargin);
rectangle1.Height -= (2 * this.yMargin);
g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, (RectangleF) rectangle1, format1);
format1.Dispose();
}
catch(System.Exception ex)
{
System.Console.WriteLine ("在绘制单元格时发生错误:{0}",GetAllErrMessage (ex) );
}
}
protected override void ReleaseHostedControl()
{
if (this.ctrl.Parent != null)
{
this.ctrl.Parent.Controls.Remove(this.ctrl);
}
}private void RollBack()
{
this.ctrl.Text = this.oldValue;
}protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (this.ctrl.Parent != null)
{
this.ctrl.Parent.Controls.Remove(this.ctrl);
}
if (value != null)
{
value.Controls.Add(this.ctrl);
}
this.ctrl.SetDataGrid(value);
}protected override void UpdateUI(CurrencyManager source, int rowNum, string instantText)
{
this.ctrl.Text = this.GetText(this.GetColumnValueAtRow(source, rowNum));
if (this.ctrl.Enabled && (instantText != null))
{
this.ctrl.Text = instantText;
}
}public string Format
{
get
{
return this.format;
}
set
{
if (value == null)
{
value = string.Empty;
}
if ((this.format == null) || !this.format.Equals(value))
{
this.format = value;
if (((this.format.Length == 0) && (this.typeConverter != null)) && !this.typeConverter.CanConvertFrom(typeof(string)))
{
this.ReadOnly = true;
}
this.Invalidate();
}
}
}
public IFormatProvider FormatInfo
{
get
{
return this.formatInfo;
}
set
{
this.formatInfo = value;
}
}[DefaultValue(null)]
public override PropertyDescriptor PropertyDescriptor
{
set
{ base.PropertyDescriptor = value;
if ((this.PropertyDescriptor != null) && (this.PropertyDescriptor.PropertyType != typeof(object)))
{
this.typeConverter = TypeDescriptor.GetConverter(this.PropertyDescriptor.PropertyType);
Type[] typeArray1 = new Type[2] { typeof(string), typeof(IFormatProvider) } ;
this.parseMethod = this.PropertyDescriptor.PropertyType.GetMethod("Parse", typeArray1);
}
}
}public override bool ReadOnly
{
get
{
return base.ReadOnly;
}
set
{
if ((value || !string.Empty.Equals(this.format)) || (this.typeConverter == null) || this.typeConverter.CanConvertFrom(typeof(string)))
{
base.ReadOnly = value;
}
}
}[Browsable(false)]
public virtual DateTimePicker DateTimePicker
{
get
{
return this.ctrl;
}
}}
}
.....
<Columns>
<asp:TemplateColumn HeaderText="按钮">
<ItemTemplate>
<asp:Button id="bildentarr" onclick="bildentarr_Click" runat="server" Text="生成数据" BorderStyle="Outset"
BackColor="#ccccff" ForeColor="#0066ff"></asp:Button>
<asp:Button id="delentarr" onclick="delentarr_Click" runat="server" Text="删除数据" BorderStyle="Outset"
BackColor="#ccccff" ForeColor="#0066ff" Visible="False"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="按钮二">
<ItemTemplate>
<asp:Button id="frshcomarr" onclick="frshcomarr_Click" runat="server" Text="建立矩阵" BorderStyle="Outset"
BackColor="#ccccff" ForeColor="#0066ff"></asp:Button>
<asp:Button id="delcomarr" onclick="delcomarr_Click" runat="server" Text="删除矩阵" BorderStyle="Outset"
BackColor="#ccccff" ForeColor="#0066ff" Visible="False"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
后台:
... private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem))
{
Button frshcomarr=(Button)e.Item.FindControl("frshcomarr");
Button delcomarr=(Button)e.Item.FindControl("delcomarr");
Button bildentarr=(Button)e.Item.FindControl("bildentarr");
Button delentarr=(Button)e.Item.FindControl("delentarr"); frshcomarr.Click +=new System.EventHandler(this.frshcomarr_Click);
delcomarr.Click +=new System.EventHandler(this.frshcomarr_Click);...