[收藏]关于DataGrid模板列的文章
2006-10-28 10:43
323 查看
声明:本文为收藏内容
You have "StudentId", a foreign key to the "Id" field in "Student" table, and another field "BookName". Now, suppose you want to display the data of "Book" on a
Collapse
Notice that the case where the selected value is null should be handled. Null values generate
To use this class, simply add it to the project, construct an instance, and use its properties. This is a sample usage:
This is a sample usage:
Images of the button and button-pressed are attached with the class as well. You can add images to the solution, and choose "embedded resource" as build action in their properties. Then, in the class constructor, you must specify the location of the images in the assembly's resources:
This class uses handles, thus the constructor includes column number in its parameter. You must specify the
A button click event argument class is inserted in the same control's class. This is useful to manage the messages or actions when the button is clicked:
Note: You must define the button click event handler
Here is a sample on how to use this class from the
Collapse
Note: The handler for a button click is inserted in the
That's all. The user controls' classes are easy to use, as explained above. Hopefully, more user controls will be added later. Please let me know of any feedback or comment on this article or code.
Introduction
This article explains how to build some special user controls forDataGridcolumns on a Windows application. The special controls are:
DataGrid ComboBox,
DateTimePickerand
Buttoncolumns. In a
DataGrid, a column style is an object that defines what the column looks and behaves like, including such things as color, font, and the presence of controls that will handle linked field in a database with the use of a
ComboBox, a
CheckBoxand other controls. The .NET Framework includes two types of column-style classes by default: the
DataGridTextBoxColumnand
DataGridBoolColumnclasses. A quantity of jobs you may want to achieve with the Windows Forms
DataGridcontrol are, unexpectedly, more troublesome than you might expect. Principally, this is because the Windows Forms
DataGridcontrol is column-based, rather than cell-based. As a result, to attain most tasks, you have to work with the columns, not the cells themselves. Hence the need to create our own set of
DataGridColumnStyleobjects that define custom column styles for the Windows Forms
DataGridcontrol and add them to the
GridColumnStylesCollection. Below is an explanation of how those three user controls might be useful:
DataGrid ComboBox Column Style
Suppose you have a database as shown below:Student
| Book
|
DataGrid, and don't want to display "StudentId" as integer. In addition, you need to display the name, but any editing would update the "StudentId" field. The first solution would be to create two
DataTables with parent-child relation, and write many instructions to display the wanted fields correctly, and update the table as needed. This needs a lot of instructions and design. Another solution would be to use a combo box, with data source as the "Student" table, display member as "Name", and value member as "Id". And any edits would display the new name, but update the "StudentId" field. Using the
DataGridComboBoxColumnclass is easy to do that, and is added as a column to the
DataGrideasily.
DataGrid DateTimePicker Column Style
Let's take the same database example as above. In the table "Book", there's a field "DateTime" with date information. Suppose you want to edit this field in theDataGriddirectly. One way is to write the date, in the right format, in the text box column of the
DataGrid. This could cause errors if you mistyped the date, or in a different format. Another solution would be to use a
DateTimePickercontrol, and just choose the exact date from the control. Using the
DataGridTimePickerColumnallows you to achieve that easily.
DataGrid Button Column Style
You are viewing all rows in theDataGrid. Now, suppose you want to delete a row from the
DataSet. A simple way would be to click on a button on this row, and an event is fired to delete the row, then refresh the
DataSet. This can be easily done using
DataGridButtonColumn.
Using the code
DataGridComboBoxColumn
This class builds theComboBoxcolumn user control for
DataGrids for Windows applications. It inherits from
DataGridTextBoxColumn, since this is already an available control in the .NET Framework. What needs to be modified, depending on the application, is the
Leaveevent.
Collapse
private void comboBox_Leave(object sender, EventArgs e){DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;string s=null;//in case the selected value is null.try{if(!rowView.Row[this.comboBox.DisplayMember].GetType().FullName.Equals("System.DBNull"))s = (string) rowView.Row[this.comboBox.DisplayMember];elses="";}catch(Exception ex){MessageBox.Show(ex.Message);//s="";}SetColumnValueAtRow(this.cmanager, this.iCurrentRow, s);Invalidate();this.comboBox.Hide();this.DataGridTableStyle.DataGrid.Scroll -=new EventHandler(DataGrid_Scroll);}
Notice that the case where the selected value is null should be handled. Null values generate
System.DBNullas output. This has to be converted to an empty string.
To use this class, simply add it to the project, construct an instance, and use its properties. This is a sample usage:
RamiSaad.DataGridComboBoxColumn comboboxColStyle =new RamiSaad.DataGridComboBoxColumn();comboboxColStyle.ComboBox.DataSource=_dataSet.Tables["Student"];comboboxColStyle.ComboBox.DisplayMember =_dataSet.Tables["Student"].Columns["Name"].ColumnName;;comboboxColStyle.ComboBox.ValueMember =_dataSet.Tables["Student"].Columns["Id"].ColumnName;comboboxColStyle.HeaderText = "Student Id";comboboxColStyle.MappingName = _dataSet.Tables["Book"].Columns[i].ColumnName;tableStyle.GridColumnStyles.Add(comboboxColStyle);
Note: You must set the properties and
DataSourceof the
ComboBoxproperty of the user control first before setting its header text and mapping name of the column style.
DataGridTimePickerColumn
This class builds theDateTimePickercolumn user control for
DataGrids for Windows applications. It inherits from
DataGridTextBoxColumn, since this is already an available control in the .NET Framework.
This is a sample usage:
RamiSaad.DataGridTimePickerColumn timepickerColStyle =new RamiSaad.DataGridTimePickerColumn();timepickerColStyle.HeaderText = "Date";timepickerColStyle.MappingName =_dataSet.Tables["Book"].Columns[i].ColumnName;timepickerColStyle.Width=145;tableStyle.GridColumnStyles.Add(timepickerColStyle);
DataGridButtonColumn
This class builds theButtoncolumn user control for
DataGrids for Windows application. It inherits from
DataGridTextBoxColumn, since this is already an available control in the .NET Framework. To use this class, simply add it to the project, construct an instance, and use its properties.
Images of the button and button-pressed are attached with the class as well. You can add images to the solution, and choose "embedded resource" as build action in their properties. Then, in the class constructor, you must specify the location of the images in the assembly's resources:
try{char[] c={','};System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream(this.GetType().Assembly.ToString().Split(c,10)[0]+".button.bmp");_button = new Bitmap(strm);strm = this.GetType().Assembly.GetManifestResourceStream(this.GetType().Assembly.ToString().Split(c,10)[0]+".buttonpressed.bmp");_buttonPressed = new Bitmap(strm);}catch(Exception ex){MessageBox.Show(ex.Message);}
This class uses handles, thus the constructor includes column number in its parameter. You must specify the
DataGrid's column number in which the button control will be inserted:
RamiSaad.DataGridButtonColumn buttonColStyle =new RamiSaad.DataGridButtonColumn(i); //pass the column#
A button click event argument class is inserted in the same control's class. This is useful to manage the messages or actions when the button is clicked:
public delegate void DataGridCellButtonClickEventHandler(object sender,DataGridCellButtonClickEventArgs e);public class DataGridCellButtonClickEventArgs : EventArgs{private int _row;private int _col;public DataGridCellButtonClickEventArgs(int row, int col){_row = row;_col = col;}public int RowIndex {get{return _row;}}public int ColIndex {get{return _col;}}}
Note: You must define the button click event handler
DataGridCellButtonClickEventHandler. This will be called from the form itself which calls the
DataGrid.
Here is a sample on how to use this class from the
DataGrid's form:
Collapse
{buttonColStyle = new RamiSaad.DataGridButtonColumn(i); //pass the column#buttonColStyle.HeaderText = "Delete";buttonColStyle.MappingName = _dataSet.Tables["Book"].Columns[i].ColumnName;//hookup our cellbutton handler...buttonColStyle.CellButtonClicked +=new RamiSaad.DataGridCellButtonClickEventHandler(HandleCellButtonClick);tableStyle.GridColumnStyles.Add(buttonColStyle);//hook the mouse handlersdataGrid1.MouseDown += new MouseEventHandler(buttonColStyle.HandleMouseDown);dataGrid1.MouseUp += new MouseEventHandler(buttonColStyle.HandleMouseUp);}/////////// handler for a click on one of the gridcell buttonsprivate void HandleCellButtonClick(object sender, DataGridCellButtonClickEventArgs e){MessageBox.Show("row " + e.RowIndex.ToString() + " will be deleted.");}
Note: The handler for a button click is inserted in the
DataGrid's form itself. Also, you must hook the mouse handlers to the button column style.
That's all. The user controls' classes are easy to use, as explained above. Hopefully, more user controls will be added later. Please let me know of any feedback or comment on this article or code.
相关文章推荐
- 关于DataGrid模板列的文章
- 关于Neutron的好文章,果断收藏
- 关于WinDbg的一些文章收藏
- 收藏关于Linux IPC的一个系列的文章
- 【收藏】WebForm中DataGrid的20篇经典文章
- WebForm中关于DataGrid的20篇经典文章
- 一篇很不错的关于WPF DataGrid的文章,包含validation
- 收藏的好文章关于SQL
- 一篇关于如何写注释的文章,值得收藏
- 一篇很好的关于mysqld_safe脚本源码解读的文章,收藏了!!
- 收藏的一篇关于prototype的文章
- 好文章收藏--关于算法、推荐算法、数据挖掘
- 关于动态生成列的DATAGRID不能出发模板列的时间问题
- 张礼军blog中关于OAF文章收藏
- 关于悲观锁和乐观锁的区别(最直观理解) 2015-11-23 11:59 5105人阅读 评论(1) 收藏 举报 分类: 数据库(20) 版权声明:本文为博主原创文章,未经博主允许不得转载。
- 收藏关于Linux IPC的一个系列的文章
- 收藏关于AI的相关的文章
- 收藏一篇关于Asp.net Response.Filter的文章
- 这篇关于野指针的文章写得太有个性了!收藏了
- 收藏一个关于推送的文章