日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Dev的关于XtraGrid的使用2

發(fā)布時(shí)間:2025/6/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dev的关于XtraGrid的使用2 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

接著說(shuō),GirdControl如何定位和查找指定列顯示值的行(注意是列的實(shí)顯示值,而不是關(guān)聯(lián)數(shù)據(jù)源列值)

下面請(qǐng)看代碼:

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 //如果用數(shù)據(jù)源中的列值,請(qǐng)用ColumnView.LocateByValue int rhFound = view.LocateByDisplayText(view.FocusedRowHandle + 1, column, searchText); // focusing the cell if(rhFound != GridControl.InvalidRowHandle) { view.FocusedRowHandle = rhFound; view.FocusedColumn = column; } }

另一個(gè)查找示例

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(); }

將特定編輯框綁定到列 默認(rèn)的cell編輯框是不可以改變的,即使是在運(yùn)行時(shí),因?yàn)樗鼈兪莿?dòng)態(tài)創(chuàng)建和注銷的。 要想定制,就在設(shè)計(jì)時(shí)修改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;

另一個(gè)運(yùn)行時(shí)綁定列編輯框示例

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; } }

檢驗(yàn)錄入數(shù)據(jù)是否有效

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 "賬號(hào)": txtUserName.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column); break; case "密碼": txtPassword.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column); break; case "真實(shí)姓名": 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; }

?

主從表的設(shè)置

DataTable dt = pb.GetItemInfoList(Port).Copy(); //返回一個(gè)TABLEdt.TableName = "ItemInfo"; ds.Tables.Add(dt); DataTable dt2 = pb.GetBuildingInfoList(Port).Copy(); //返回一個(gè)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"];

獲取從表的當(dāng)前選擇行的某一列(如ID列) ???? 這個(gè)時(shí)候再使用獲取主表當(dāng)前選擇行的某一列的方法是不行的,因?yàn)樗玫降膕eletedrowscount=0。使用如下方法得到: ? 在MASTER表的展開(kāi)事件中得到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; } //取得從表的當(dāng)前行 int[] i = detailView.GetSelectedRows(); DataRowView dt = (DataRowView)detailView.GetRow(i[0]); //獲得當(dāng)前行某列的值可以使用 dt["列名"].ToString(); //獲得當(dāng)那個(gè)列的值。

定義焦點(diǎn)行的方法:

gridView_bcode.FocusedRowHandle = focuseRowInt; //通過(guò)設(shè)置GridView 的FocusedRowHandle屬性 //獲取焦點(diǎn)行任意單元格的數(shù)據(jù) ColumnView cv = (ColumnView)gridControl_Gongzi.FocusedView;//重新獲取此ID 否則無(wú)法從表頭連刪獲取不到id int focusedhandle = cv.FocusedRowHandle; object rowIdObj = gridView1.GetRowCellValue(focusedhandle, "id"); if (DBNull.Value != rowIdObj) { FocusedRow_id = Convert.ToInt32(rowIdObj); } //獲取焦點(diǎn)行任意單元格的數(shù)據(jù) ColumnView cv = (ColumnView)gridControl_Gongzi.FocusedView;//重新獲取此ID 否則無(wú)法從表頭連刪獲取不到id int focusedhandle = cv.FocusedRowHandle; object rowIdObj = gridView1.GetRowCellValue(focusedhandle, "id"); if (DBNull.Value != rowIdObj) { FocusedRow_id = Convert.ToInt32(rowIdObj); } view plaincopy to clipboardprint? //當(dāng)數(shù)據(jù)發(fā)生變化時(shí)執(zhí)行 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); } } //當(dāng)數(shù)據(jù)發(fā)生變化時(shí)執(zhí)行 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? //設(shè)置焦點(diǎn)行的焦點(diǎn)單元格的位置 ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView; view.FocusedColumn = view.Columns["bumen"]; //設(shè)置焦點(diǎn)行的焦點(diǎn)單元格的位置 ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView; view.FocusedColumn = view.Columns["bumen"]; view plaincopy to clipboardprint? //當(dāng)焦點(diǎn)行發(fā)生改變時(shí)執(zhí)行 獲取選中焦點(diǎn)行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)//做個(gè)判斷否則獲取不到id后報(bào)錯(cuò) { FocusedRow_id = Convert.ToInt32(rowIdObj); } } //當(dāng)焦點(diǎn)行發(fā)生改變時(shí)執(zhí)行 獲取選中焦點(diǎn)行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)//做個(gè)判斷否則獲取不到id后報(bào)錯(cuò) { FocusedRow_id = Convert.ToInt32(rowIdObj); } } view plaincopy to clipboardprint? //焦點(diǎn)行的FocusedHandle為: FocuseRow_Handle = -999998; //獲取焦點(diǎn)行的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"];//定位焦點(diǎn)網(wǎng)格的位置 FocuseRow_Handle = newview.FocusedRowHandle;//獲取新焦點(diǎn)行的FocuseRowHandle 并初始化全局變量FocuseRow_Handle供保存操作時(shí)判斷是upd ate還是insert }

?

轉(zhuǎn)載于:https://www.cnblogs.com/lhyqzx/p/5157364.html

總結(jié)

以上是生活随笔為你收集整理的Dev的关于XtraGrid的使用2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。