DEV GridControl控件的使用总结

去掉GridControl控件上面的
Drag a column header here to group by that column
Options/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(); }

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值