Blog信息 |
blog名称:注册会计师(注会)练习软件 日志总数:398 评论数量:116 留言数量:27 访问次数:3270053 建立时间:2005年6月6日 |

| |
[devexpress相关控件][转贴]xtragrid的使用 软件技术
吕向阳 发表于 2006/9/14 13:05:00 |
提交当前行的修改using DevExpress.XtraGrid;using DevExpress.XtraGrid.Views.Base;using System.Data.Common;//...public void UpdateDatasource(GridControl grid) { //Save the latest changes to the bound DataTable ColumnView view = (ColumnView)grid.FocusedView; view.CloseEditor(); if(!view.UpdateCurrentRow()) return; //Update the database's Suppliers table to which oleDBDataAdapter1 is connected DoUpdate(oleDbDataAdapter1, dataSet11.Tables["Suppliers"]); //Update the database's Products table to which the oleDbDataAdapter2 is connected DoUpdate(oleDbDataAdapter2, dataSet11.Tables["Products"]);} public void DoUpdate(DbDataAdapter dataAdapter, System.Data.DataTable dataTable) { try { dataAdapter.Update(dataTable); } catch(Exception ex) { MessageBox.Show(ex.Message); }}
从非绑定数据源得到数据private void Form1_Load(object sender, System.EventArgs e) { // ... // Create an unbound column. GridColumn unbColumn = gridView1.Columns.Add("Total"); unbColumn.VisibleIndex = gridView1.Columns.Count; unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal; // Disable editing. unbColumn.OptionsColumn.AllowEdit = false; // Specify format settings. unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric; unbColumn.DisplayFormat.FormatString = "c"; // Customize the appearance settings. unbColumn.AppearanceCell.BackColor = Color.LemonChiffon;} // Returns the total amount for a specific row.decimal getTotalValue(ColumnView view, int rowHandle) { decimal unitPrice = Convert.ToDecimal(view.GetRowCellValue(rowHandle, "UnitPrice")); decimal quantity = Convert.ToDecimal(view.GetRowCellValue(rowHandle, "Quantity")); decimal discount = Convert.ToDecimal(view.GetRowCellValue(rowHandle, "Discount")); return unitPrice * quantity * (1 - discount);} // Provides data for the Total column.private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if(e.Column.FieldName == "Total" && e.IsGetData) e.Value = getTotalValue(sender as ColumnView, e.RowHandle);}
运行时绑定到实现Ilist接口的数据源public class Record { int id, age; string name; public Record(int id, string name, int age) { this.id = id; this.name = name; this.age = age; } public int ID { get { return id; } } public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } }}
ArrayList listDataSource = new ArrayList();listDataSource.Add(new Record(1, "Jane", 19));listDataSource.Add(new Record(2, "Joe", 30));listDataSource.Add(new Record(3, "Bill", 15));listDataSource.Add(new Record(4, "Michael", 42));gridControl1.DataSource = listDataSource;gridControl1.MainView.PopulateColumns();
自定义列:[C#]DevExpress.XtraGrid.Views.Base.ColumnView view = gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView;DialogResult answer = MessageBox.Show("Do you want to create columns for all fields?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);if (answer == DialogResult.Yes) view.PopulateColumns();else { string[] fieldNames = new string[] {"ProductID", "ProductName", "QuantityPerUnit", "UnitPrice"}; DevExpress.XtraGrid.Columns.GridColumn column; view.Columns.Clear(); for (int i = 0; i < fieldNames.Length; i++) { column = view.Columns.Add(fieldNames[i]); column.VisibleIndex = i; }}GridColumn主要属性Property Description GridColumn.Name 设计时定义的列名 GridColumn.FieldName 绑定到的数据源中的列名 GridColumn.AbsoluteIndex 在grid中的绝对位置索引 GridColumn.ColumnHandle 指定关联数据源列名的标识,它不一定是唯一的,因为一个数据源列可以关联到一个Grid中的多个列.
手动创建Band// obtaining the main view and clearing its bands collectionBandedGridView view = gridControl1.MainView as BandedGridView;view.Bands.Clear();// creating the bands layoutGridBand bandGeneral = view.Bands.Add("General Info");GridBand bandTechnical = view.Bands.Add("Technical Info");GridBand bandEngine = bandTechnical.Children.Add("Engine Info");GridBand bandTransmission = bandTechnical.Children.Add("Transmission Info");// assigning columns to bandscolTrademark.OwnerBand = bandGeneral;colModel.OwnerBand = bandGeneral;colLiter.OwnerBand = bandEngine;colCylinders.OwnerBand = bandEngine;colSpeedCount.OwnerBand = bandTransmission;colTransmission.OwnerBand = bandTransmission;
如何定位和查找指定列显示值的行(注意是列的实显示值,而不是关联数据源列值)using DevExpress.XtraGrid.Views.Base;using DevExpress.XtraGrid.Columns;// ... string searchText = "Japan";// obtaining the focused viewColumnView view = (ColumnView)gridControl1.FocusedView;// obtaining the column bound to the Country fieldGridColumn column = view.Columns["Country"];if(column != null) {// locating the row//如果用数据源中的列值,请用ColumnView.LocateByValue int rhFound = view.LocateByDisplayText(view.FocusedRowHandle + 1, column, searchText); // focusing the cell if(rhFound != GridControl.InvalidRowHandle) { view.FocusedRowHandle = rhFound; view.FocusedColumn = column; }}
另一个查找示例
DevExpress.XtraGrid.Views.Base.ColumnView view = gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView;view.BeginUpdate();try { int rowHandle = 0; DevExpress.XtraGrid.Columns.GridColumn col = view.Columns["Category"]; while(true) { // locating the next row rowHandle = view.LocateByValue(rowHandle, col, "SPORTS"); // exiting the loop if no row is found if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle) break; // perform specific operations on the row found here // ... rowHandle++; }} finally { view.EndUpdate(); }
将特定编辑框绑定到列默认的cell编辑框是不可以改变的,即使是在运行时,因为它们是动态创建和注销的。要想定制,就在设计时修改ColumnEdit吧。using DevExpress.XtraEditors.Repository; //Create a repository item for a combo box editor RepositoryItemComboBox riCombo = new RepositoryItemComboBox();riCombo.Items.AddRange(new string[] {"London", "Berlin", "Paris"});//Add the item to the internal repositorygridControl1.RepositoryItems.Add(riCombo);//Now you can define the repository item as an in-place column editorcolCity.ColumnEdit = riCombo;
另一个运行时绑定列编辑框示例private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { if (e.Column.FieldName == "FieldName") return; DevExpress.XtraGrid.Views.Grid.GridView gv = sender as DevExpress.XtraGrid.Views.Grid.GridView; string fieldName = gv.GetRowCellValue(e.RowHandle, gv.Columns["FieldName"]).ToString(); switch (fieldName) { case "Population": e.RepositoryItem = repositoryItemSpinEdit1; break; case "Country": e.RepositoryItem = repositoryItemComboBox1; break; case "Capital": e.RepositoryItem = repositoryItemCheckEdit1; break; } }
检验录入数据是否有效
using DevExpress.XtraGrid.Views.Grid;using DevExpress.XtraGrid.Columns; public bool isValidDate(int day, int month, int year) { return (day > 0) && (month > 0) && (month <= 12) && (year > 1980) && (year < 2100) && (day <= DateTime.DaysInMonth(year, month));} private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) { GridView view = sender as GridView; GridColumn colDay = view.Columns["Day"]; GridColumn colMonth = view.Columns["Month"]; GridColumn colYear = view.Columns["Year"]; int day = (int)view.GetRowCellValue(e.RowHandle, colDay); int month = (int)view.GetRowCellValue(e.RowHandle, colMonth); int year = (int)view.GetRowCellValue(e.RowHandle, colYear); e.Valid = isValidDate(day, month, year); if(!e.Valid) { view.SetColumnError(colDay, "Check the day"); view.SetColumnError(colMonth, "Check the month"); view.SetColumnError(colYear, "Check the year"); }}
MouseMove捕捉
private void Grid_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { ShowHitInfo(this.gridView1.CalcHitInfo(new Point(e.X, e.Y))); }
private void ShowHitInfo(DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi) { DevExpress.XtraGrid.Views.Base.ColumnView cgv = (DevExpress.XtraGrid.Views.Base.ColumnView)Grid.MainView;
string columnName = hi.Column == null ? "No column" : hi.Column.Caption; switch(columnName) { case "账号": txtUserName.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column); break; case "密码": txtPassword.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column); break; case "真实姓名": txtRealName.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column); break; case "电子邮件": txtEmail.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column); break; case "角色": cbRole.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column); break; default: txtUserName.Text = "Null"; txtPassword.Text = "Null"; txtRealName.Text = "Null"; txtEmail.Text = "Null"; cbRole.Text = "Null"; break; } |
|
|