dev GridControl

来源:互联网 发布:网络建设工程师 编辑:程序博客网 时间:2024/04/30 22:21

DEV GridControl(摘自baobao博客)2008-03-26 09:06去掉GridControl控件上面的Drag a column header here to group by that columnOptions/OptionsView/ShowGroupPanel        True----->False提交当前行的修改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 collection 
BandedGridView view = gridControl1.MainView as BandedGridView; 
view.Bands.Clear(); 
// creating the bands layout 
GridBand 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 bands 
colTrademark.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 view 
ColumnView view = (ColumnView)gridControl1.FocusedView; 
// obtaining the column bound to the Country field 
GridColumn 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(); } 

原创粉丝点击