接着说,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的更多相关文章

  1. DEV 财务货币格式单元格

    在用友金蝶等财务软件中,经常需要输入货币类型的数据, 那么这种输入框要如何制作呢? 扩展DataGridView 的功能  出自在天空飞翔博客 http://www.cnblogs.com/micha ...

  2. Winform 中DataGridView、dev Gridview控件添加行标题

    有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) /// <summary> / ...

  3. Winform 中DataGridView控件添加行标题

    有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) /// <summary> / ...

  4. DEV XtraGrid绑定非绑定列(转)

      在Gridview创建一列 .将该列的UnboundType属性设置为bound(默认值)以外的数据类型 为该列设置一个窗体内全局唯一的FieldName,注意这个FieldName甚至不能出现在 ...

  5. Dev用于界面按选中列进行分组统计数据源(实用技巧)

    如果有用U8的可以明白这个功能就是模仿他的统计功能.我不过是把他造成通用的与适应于DEV的. (效率为6000条数据分组统计时间为3秒左右分组列过多5秒.1000条以下0.几秒,500条下0.00几秒 ...

  6. DEV GridControl小结

    1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...

  7. DEV控件:gridControl常用属性设置(转载)

    特别长,先撸下来再说 1.隐藏最上面的GroupPanel  gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值  sValue=T ...

  8. DEV控件:gridControl常用属性设置

    1.隐藏最上面的GroupPanel  gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值  sValue=Table.Rows[g ...

  9. DEV控件Grid显示行号

    DEV控件Grid的显示行号需要通过一个事件来设置,具体设置代码为: private void gridView1_CustomDrawRowIndicator(object sender, DevE ...

随机推荐

  1. php源码分析之php_info输出中css样式是怎么来的

    我们经常使用echo phpinfo();查看php的配置信息,但是大家知道里面的css样式是怎么来的吗? 我们查看源码(php源码/ext/standard/css.c) PHPAPI void p ...

  2. linux export将PATH环境变量误删了的解决办法

    今天新增环境变量的时候不小心把冒号错打成了分号 export PATH=/usr/local/php5/bin;$PATH; 导致PATH变量为/usr/local/php/bin 解决办法:[ubu ...

  3. tps,qps

    http://blog.itpub.net/22664653/viewspace-767265/

  4. windows cmd 命令大全

    原文: http://www.cnblogs.com/greatverve/archive/2011/12/09/windows-cmd.html 命令简介 cmd是command的缩写.即命令行 . ...

  5. android脚步---跟随手指动的小球

    这次的学习主要是在包里面除自动生成的mainactivity.java之外,再新建一个class, DrawView,在mainactivity里面调用DrawView. 在DrawView里面主要是 ...

  6. Google IP

    http://173.194.130.4/http://64.233.167.165/

  7. ARM汇编指令集

    一.跳转指令.跳转指令用于实现程序流程的跳转,在ARM程序中有以下两种方法可以实现程序流程的跳转. Ⅰ.使用专门的跳转指令.Ⅱ.直接向程序计数器PC写入跳转地址值. 通过向程序计数器PC写入跳转地址值 ...

  8. led.c驱动框架2nd

    led.c: #include <linux/module.h> #include <linux/init.h> #include <linux/fs.h> ; v ...

  9. Fastjson简单使用方法

    一.简单数据的序列化 pubic class UserInfo implements Serializable{ private String name; private int age; publi ...

  10. FatMouse and Cheese 动态化搜索

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...