修改Datagrid,在列上绑定其他控件。
2004-07-28 13:50
411 查看
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 DataGridTimePickerColumn : DataGridColumnStyle { private RichTextBox myDateTimePicker = new RichTextBox(); // 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; Invalidate(); } protected override bool Commit (CurrencyManager dataSource, int rowNum) { myDateTimePicker.Bounds = Rectangle.Empty; if (!isEditing) return true; isEditing = false; try { DateTime value = System.DateTime.Parse(myDateTimePicker.Text); 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.Text = value.ToString(); myDateTimePicker.Visible = true; } else { myDateTimePicker.Text = value.ToString(); 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); } } namespace DataGridColumnStyleExample { using System; using System.Data; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; public class MyForm : Form { private DataTable namesDataTable; private DataGrid grid = new DataGrid(); public MyForm() : base() { InitForm(); namesDataTable = new DataTable("NamesTable"); namesDataTable.Columns.Add(new DataColumn("Name")); DataColumn dateColumn = new DataColumn ("Date", typeof(DateTime)); dateColumn.DefaultValue = DateTime.Today; namesDataTable.Columns.Add(dateColumn); DataSet namesDataSet = new DataSet(); namesDataSet.Tables.Add(namesDataTable); grid.DataSource = namesDataSet; grid.DataMember = "NamesTable"; AddGridStyle(); AddData(); } private void AddGridStyle() { DataGridTableStyle myGridStyle = new DataGridTableStyle(); myGridStyle.MappingName = "NamesTable"; DataGridTextBoxColumn nameColumnStyle = new DataGridTextBoxColumn(); nameColumnStyle.MappingName = "Name"; nameColumnStyle.HeaderText= "??3?"; myGridStyle.GridColumnStyles.Add(nameColumnStyle); DataGridTimePickerColumn timePickerColumnStyle = new DataGridTimePickerColumn(); timePickerColumnStyle.MappingName = "Date"; timePickerColumnStyle.HeaderText = "Date"; timePickerColumnStyle.Width = 100; myGridStyle.GridColumnStyles.Add(timePickerColumnStyle); grid.TableStyles.Add(myGridStyle); } private void AddData() { DataRow dRow = namesDataTable.NewRow(); dRow["Name"] = "Name 1"; dRow["Date"] = new DateTime(2001, 12, 01); namesDataTable.Rows.Add(dRow); dRow = namesDataTable.NewRow(); dRow["Name"] = "Name 2"; dRow["Date"] = new DateTime(2001, 12, 04); namesDataTable.Rows.Add(dRow); dRow = namesDataTable.NewRow(); dRow["Name"] = "Name 3"; dRow["Date"] = new DateTime(2001, 12, 29); namesDataTable.Rows.Add(dRow); dRow = namesDataTable.NewRow(); dRow["Name"] = "Name 4"; dRow["Date"] = new DateTime(2001, 12, 13); namesDataTable.Rows.Add(dRow); dRow = namesDataTable.NewRow(); dRow["Name"] = "Name 5"; dRow["Date"] = new DateTime(2001, 12, 21); namesDataTable.Rows.Add(dRow); namesDataTable.AcceptChanges(); } private void InitForm() { this.Size = new Size(500, 500); grid.Size = new Size(350, 250); grid.TabStop = true; grid.TabIndex = 1; this.StartPosition = FormStartPosition.CenterScreen; this.Controls.Add(grid); } [STAThread] public static void Main() { MyForm myForm1= new MyForm(); myForm1.ShowDialog(); } } }
相关文章推荐
- WPF学习日志1---DataGrid绑定与修改更新(数据List & 数据库)[部分代码有摘录其他文章]
- ListView的item中EditText编辑(或者其他控件)修改本行数据
- VS2010环境下MFC使用DataGrid绑定数据源以及控件注册问题解决
- 学习context后自定义虚线圆角按钮,举一反三,其他控件也可修改
- silverlight的Datagrid控件列绑定属性笔记
- DataGrid中绑定控件实例
- WPF中如何在Xaml里通过Trigger修改该其他控件的属性
- 将字符串值的数据表数据绑定到 Windows 窗体 DataGrid 控件 (Visual Basic)
- EasyUI datagrid 明细表格中编辑框 事件绑定 及灵活计算 可根据此思路 扩展其他
- 数据绑定技术_单值数据绑定示例;将 DataTable,DataSet,DataView,DataReader 绑定到 DataGrid 控件示例;DataBinder.Eval;数组的值赋给ListBox1;Hashtable 绑定到;RadioButtonList;将XML 文件做为数据源绑定到控件
- WPF中控件绑定XML文件,实现修改XML文件即可以改变程序控件内容
- datalist的控件数据绑定以及修改删除
- 数据绑定到 DataGrid 控件后
- DataGridView控件用法(一)绑定数据-修改列值-序号列-交换列顺序
- EasyUI datagrid 明细表格中编辑框 事件绑定 及灵活计算 可根据此思路 扩展其他
- 如何通过使用 VisualC # 绑定到 ArrayList 对象或结构的 DataGrid 控件
- 在绑定控件中(datagrid,gridview..)加checkbox
- 如何修改 和某表绑定的DataGrid的列标题?
- DataGridView控件用法(一)绑定数据-修改列值-序号列-交换列顺序
- 框架 day49 BOS项目练习3(修复window控件BUG,添加/修改/作废取派员,datagrid使用,分页查询(DetachedCriteria离线),formatter函数)