Dev的关于XtraGrid的使用2

接着说,GirdControl如何定位和查找指定列显示值的行(注意是列的实显示值,而不是关联数据源列值)

下面请看代码:

复制代码
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(); } 
复制代码

将特定编辑框绑定到列 默认的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 repository  gridControl1.RepositoryItems.Add(riCombo); //Now you can define the repository item as an in-place column editor colCity.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; } 
复制代码

 

主从表的设置

复制代码
DataTable dt = pb.GetItemInfoList(Port).Copy(); //返回一个TABLE dt.TableName = "ItemInfo"; ds.Tables.Add(dt); DataTable dt2 = pb.GetBuildingInfoList(Port).Copy(); //返回一个TABLE dt2.TableName = "BuildingInfo"; ds.Tables.Add(dt2); DataColumn keyColumn = ds.Tables["ItemInfo"].Columns["ITEMINFO_ID"]; DataColumn foreignKeyColumn = ds.Tables["BuildingInfo"].Columns["ITEMINFOID"]; ds.Relations.Add("itembuildinginfo", keyColumn, foreignKeyColumn); gridControl1.DataSource = ds.Tables["ItemInfo"]; 
复制代码

获取从表的当前选择行的某一列(如ID列)      这个时候再使用获取主表当前选择行的某一列的方法是不行的,因为所得到的seletedrowscount=0。使用如下方法得到:   在MASTER表的展开事件中得到detail有的view.然后就可以利用它了。例:

复制代码
//主表的masterrowexpanded事件 private void gridView1_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e) { detailView = gridView1.GetDetailView(e.RowHandle, e.RelationIndex) as DevExpress.XtraGrid.Views.Grid.GridView; } //取得从表的当前行 int[] i = detailView.GetSelectedRows(); DataRowView dt = (DataRowView)detailView.GetRow(i[0]); //获得当前行某列的值可以使用  dt["列名"].ToString(); //获得当那个列的值。 
复制代码

定义焦点行的方法:

复制代码
gridView_bcode.FocusedRowHandle = focuseRowInt; //通过设置GridView 的FocusedRowHandle属性 //获取焦点行任意单元格的数据 ColumnView cv = (ColumnView)gridControl_Gongzi.FocusedView;//重新获取此ID 否则无法从表头连删获取不到id int focusedhandle = cv.FocusedRowHandle; object rowIdObj = gridView1.GetRowCellValue(focusedhandle, "id"); if (DBNull.Value != rowIdObj) { FocusedRow_id = Convert.ToInt32(rowIdObj); } //获取焦点行任意单元格的数据 ColumnView cv = (ColumnView)gridControl_Gongzi.FocusedView;//重新获取此ID 否则无法从表头连删获取不到id int focusedhandle = cv.FocusedRowHandle; object rowIdObj = gridView1.GetRowCellValue(focusedhandle, "id"); if (DBNull.Value != rowIdObj) { FocusedRow_id = Convert.ToInt32(rowIdObj); } view plaincopy to clipboardprint? //当数据发生变化时执行 private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e) { int intRowHandle = e.RowHandle; FocusedRow_bumen = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "bumen")); FocusedRow_xingming = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "xingming")); //FocusedRow_jibengongzi = Convert.ToDecimal(gridView1.GetRowCellValue(intRowHandle, "jibengongzi")); object rowJibengongziObj = gridView1.GetRowCellValue(intRowHandle, "jibengongzi"); if (DBNull.Value != rowJibengongziObj) { FocusedRow_jibengongzi = Convert.ToDecimal(rowJibengongziObj); } } //当数据发生变化时执行 private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e) { int intRowHandle = e.RowHandle; FocusedRow_bumen = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "bumen")); FocusedRow_xingming = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "xingming")); //FocusedRow_jibengongzi = Convert.ToDecimal(gridView1.GetRowCellValue(intRowHandle, "jibengongzi")); object rowJibengongziObj = gridView1.GetRowCellValue(intRowHandle, "jibengongzi"); if (DBNull.Value != rowJibengongziObj) { FocusedRow_jibengongzi = Convert.ToDecimal(rowJibengongziObj); } } view plaincopy to clipboardprint? //设置焦点行的焦点单元格的位置 ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView; view.FocusedColumn = view.Columns["bumen"]; //设置焦点行的焦点单元格的位置 ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView; view.FocusedColumn = view.Columns["bumen"]; view plaincopy to clipboardprint? //当焦点行发生改变时执行 获取选中焦点行id private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { int intRowHandle = e.FocusedRowHandle; object rowIdObj = gridView1.GetRowCellValue(intRowHandle, "id"); if (DBNull.Value != rowIdObj)//做个判断否则获取不到id后报错  { FocusedRow_id = Convert.ToInt32(rowIdObj); } } //当焦点行发生改变时执行 获取选中焦点行id private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { int intRowHandle = e.FocusedRowHandle; object rowIdObj = gridView1.GetRowCellValue(intRowHandle, "id"); if (DBNull.Value != rowIdObj)//做个判断否则获取不到id后报错  { FocusedRow_id = Convert.ToInt32(rowIdObj); } } view plaincopy to clipboardprint? //焦点行的FocusedHandle为: FocuseRow_Handle = -999998; //获取焦点行的handle ColumnView newview = (ColumnView)gridControl_Gongzi.FocusedView; FocuseRow_Handle = newview.FocusedRowHandle; //回车添加新行  代码 private void gridView1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView; if(view.IsLastRow) { if (FocuseRow_Handle == 0) { gridView1.AddNewRow(); ColumnView newview = (ColumnView)gridControl_Gongzi.FocusedView; newview.FocusedColumn = newview.Columns["bumen"];//定位焦点网格的位置  FocuseRow_Handle = newview.FocusedRowHandle;//获取新焦点行的FocuseRowHandle 并初始化全局变量FocuseRow_Handle供保存操作时判断是upd ate还是insert  } 
复制代码

 

你可能感兴趣的:(Dev的关于XtraGrid的使用2)