1、 如何解決單擊記錄整行選中的問題
View->OptionsBehavior->EditorShowMode 設(shè)置為:Click
2劣挫、 如何新增一條記錄
(1)、gridView.AddNewRow()
(2)、實現(xiàn) gridView_InitNewRow 事件
3突那、如何解決 GridControl 記錄能獲取而沒有顯示出來的問題
gridView.populateColumns();
4侨颈、如何讓行只能選擇而不能編輯(或編輯某一單元格)
(1)余赢、View->OptionsBehavior->EditorShowMode 設(shè)置為:Click
(2)、View->OptionsBehavior->Editable 設(shè)置為:false
5哈垢、如何禁用 GridControl 中單擊列彈出右鍵菜單
設(shè)置 Run Design->OptionsMenu->EnableColumnMenu 設(shè)置為:false
6妻柒、如何隱藏 GridControl 的 GroupPanel 表頭
設(shè)置 Run Design->OptionsView->ShowGroupPanel 設(shè)置為:false
7、如何禁用 GridControl 中列頭的過濾器 過濾器如下圖所示:? ?
設(shè)置 Run Design->OptionsCustomization->AllowFilter 設(shè)置為:false
8耘分、如何在查詢得到 0 條記錄時顯示自定義的字符提示/顯示 如圖所示:
方法如下:
//When no Records Are Being Displayed
private void gridView1_CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e)
{
//方法一(此方法為GridView設(shè)置了數(shù)據(jù)源綁定時举塔,可用)
ColumnView columnView = sender as ColumnView;
BindingSource bindingSource = this.gridView1.DataSource as BindingSource;
if(bindingSource.Count == 0)
{
string str = "沒有查詢到你所想要的數(shù)據(jù)!";
Font f = new Font("宋體", 10, FontStyle.Bold);
Rectangle r = new Rectangle(e.Bounds.Top + 5, e.Bounds.Left + 5, e.Bounds.Right - 5, e.Bounds.Height - 5);
e.Graphics.DrawString(str, f, Brushes.Black, r); }
//方法二(此方法為GridView沒有設(shè)置數(shù)據(jù)源綁定時,使用求泰,一般使用此種方 法)
if (this._flag)
{ if (this.gridView1.RowCount == 0)
{ string str = "沒有查詢到你所想要的數(shù)據(jù)!"; Font f = new Font("宋體", 10, FontStyle.Bold);
Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5, e.Bounds.Height - 5);
e.Graphics.DrawString(str, f, Brushes.Black, r); } } }
9央渣、如何顯示水平滾動條?或
設(shè)置 this.gridView.OptionsView.ColumnAutoWidth = false;
.....列表寬度自適應(yīng)內(nèi)容
gridview1.BestFitColumns();
10渴频、如何定位到第一條數(shù)據(jù)/記錄芽丹?
設(shè)置 this.gridView.MoveFirst()
11、如何定位到下一條數(shù)據(jù)/記錄卜朗?
設(shè)置 this.gridView.MoveNext()
12拔第、如何定位到最后一條數(shù)據(jù)/記錄?
設(shè)置 this.gridView.MoveLast()
13场钉、設(shè)置成一次選擇一行蚊俺,并且不能被編輯
this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
this.gridView1.OptionsBehavior.Editable = false;
this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
14、如何顯示行號逛万?
? private void gvPayList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
? ? ? ? {
? ? ? ? ? ? e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
? ? ? ? ? ? if (e.Info.IsRowIndicator)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (e.RowHandle >= 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? e.Info.DisplayText = (e.RowHandle + 1).ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (e.RowHandle < 0 && e.RowHandle > -1000)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
? ? ? ? ? ? ? ? ? ? e.Info.DisplayText = "G" + e.RowHandle.ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
15泳猬、如何讓各列頭禁止移動?
設(shè)置 gridView1.OptionsCustomization.AllowColumnMoving = false;
16、如何讓各列頭禁止排序暂殖?
設(shè)置 gridView1.OptionsCustomization.AllowSort = false;
17价匠、如何禁止各列頭改變列寬?
設(shè)置 gridView1.OptionsCustomization.AllowColumnResizing = false;
18.拖動滾動條時固定某一列
設(shè)置Columns呛每,選擇要固定的列踩窖。設(shè)置Fixed屬性,可以選擇:固定在左邊晨横、固定在右邊洋腮、不固定。
19.獲取選定行,指定列單元格的內(nèi)容
? ? ? return gridView1.GetRowCellValue(pRows[0], ColumName).ToString ();
20.分組顯示
OptionsView>OptionsBehavior>AutoExpandAllGroups = True
選擇要分組的列手形,將GroupIndex屬性設(shè)置為0
21.格式化數(shù)據(jù)
? ? ? private void gvList_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
? ? ? ? {
? ? ? ? ? ? if (this.gvList.FocusedColumn.FieldName == "passQty")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string passQty = e.Value.ToString().Trim();
? ? ? ? ? ? ? ? int receiveQty = orderDetailList[this.gvList.FocusedRowHandle].qty;
? ? ? ? ? ? ? ? if (!JXType.IsIntBigThanZero(passQty))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? e.Valid = false;
? ? ? ? ? ? ? ? ? ? e.ErrorText = "合格數(shù)量必須為大于等于0小于等于接貨數(shù)量的整數(shù)啥供!";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (int.Parse(passQty) > receiveQty)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? e.Valid = false;
? ? ? ? ? ? ? ? ? ? ? ? e.ErrorText = "合格數(shù)量必須為大于0小于等于接貨數(shù)量的整數(shù)!";
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
}
22.合并表頭
? ///初始化表格
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraEditors.Repository;
? ? ? ? private void InitGrid()
? ? ? ? {
? ? ? ? ? ? // advBandedGridView1是表格上的默認(rèn)視圖库糠,注意這里聲明的是:BandedGridView
? ? ? ? ? ? BandedGridView view = advBandedGridView1 as BandedGridView;
? ? ? ? ? ? view.BeginUpdate(); //開始視圖的編輯伙狐,防止觸發(fā)其他事件
? ? ? ? ? ? view.BeginDataUpdate(); //開始數(shù)據(jù)的編輯
? ? ? ? ? ? view.Bands.Clear();
? ? ? ? ? ? view.OptionsView.ShowColumnHeaders = false;? ? ? ? ? ? ? ? ? ? ? ? //因為有Band列了,所以把ColumnHeader隱藏
? ? ? ? ? ? //添加列標(biāo)題
? ? ? //添加列標(biāo)題
? ? ? ? ? ? GridBand bandID = view.Bands.AddBand("ID");
? ? ? ? ? ? bandID.Visible = false; //隱藏ID列
? ? ? ? ? ? GridBand bandName = view.Bands.AddBand("姓名");
? ? ? ? ? ? GridBand bandSex = view.Bands.AddBand("性別");
? ? ? ? ? ? GridBand bandBirth = view.Bands.AddBand("出生日期");
? ? ? ? ? ? GridBand bandScore = view.Bands.AddBand("分?jǐn)?shù)");
? ? ? ? ? ? GridBand bandMath = bandScore.Children.AddBand("數(shù)學(xué)");
? ? ? ? ? ? GridBand bandChinese = bandScore.Children.AddBand("語文");
? ? ? ? ? ? GridBand bandEnglish = bandScore.Children.AddBand("英語");
? ? ? ? ? ? GridBand bandSubTotal = bandScore.Children.AddBand("小計");
? ? ? ? ? ? GridBand bandRemark = view.Bands.AddBand("備注");
? ? ? ? ? ? bandFile.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;//這是合并表頭居中顯示
? ? ? ? ? ? view.EndDataUpdate();//結(jié)束數(shù)據(jù)的編輯
? ? ? ? ? ? view.EndUpdate();? //結(jié)束視圖的編輯
? ? ? ? }
具體可看
dev gridcontrol 合并表頭
23.? //動態(tài)添加列
? ? ? ? ? ? DevExpress.XtraGrid.Columns.GridColumn Col1 = new DevExpress.XtraGrid.Columns.GridColumn();
? ? ? ? ? ? Col1.FieldName = "name";
? ? ? ? ? ? Col1.Caption = "名字";
? ? ? ? ? ? Col1.Visible = false;
? ? ? ? ? ? Col1.VisibleIndex = gvCountry.Columns.Count;
? ? ? ? ? ? gvCountry.Columns.Add(Col1);
24瞬欧。設(shè)置自動增加的行號
? private void gridview_CustomDrawRowIndicator(object sender,? ? ? ? ? ? ? ? ? ? ? ? ? ? DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
? ? {
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
? ? ? ? ? ? if (e.Info.IsRowIndicator)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (e.RowHandle >= 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? e.Info.DisplayText = (e.RowHandle + 1).ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (e.RowHandle < 0 && e.RowHandle > -1000)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
? ? ? ? ? ? ? ? ? ? e.Info.DisplayText = "G" + e.RowHandle.ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
}
25.特效:gridcontrol中有5種view 型式贷屎,普通的是gridview,然后分別為cardview艘虎、BandedView唉侄、Advanced BandedView、LayoutView野建;共5種属划。
? 1)、view組中把OptionView下的viewmode 設(shè)置成“Carousel”就達到這種“旋轉(zhuǎn)木馬”式的gridcontrol view 特效了
? 2)候生、layoutView1.OptionsCarouselMode.PitchAngle 這個屬性決定“旋轉(zhuǎn)木馬”的pitch angle 螺距角; 螺旋角; 螺旋升角; 俯仰角; 傾角; 節(jié)錐半角
? 3)同眯、Roll Angle 屬性決定著 傾側(cè)角度
? 4)、指定數(shù)據(jù)源陶舞,顯示數(shù)據(jù):
? //顯示數(shù)據(jù)
? ? ? ? private void showData(List<Employee > list)
? ? ? ? {
? ? ? ? ? ? DataTable dt = new DataTable("OneEmployee");
? ? ? ? ? ? dt.Columns.Add("Caption", System.Type.GetType("System.String"));
? ? ? ? ? ? dt.Columns.Add("Department", System.Type.GetType("System.String"));
? ? ? ? ? ? dt.Columns.Add("PhotoName", System.Type.GetType("System.Byte[]"));
? ? ? ? ? ? for (int i = 0; i < list.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DataRow dr = dt.NewRow();
? ? ? ? ? ? ? ? dr["Caption"] = list[i].Name;
? ? ? ? ? ? ? ? dr["Department"] = list[i].Department;
? ? ? ? ? ? ? ? string imagePath = @"D:\C#\photos\" + list[i].PhotoPath;
? ? ? ? ? ? ? ? dr["PhotoName"] = getImageByte(imagePath);
? ? ? ? ? ? ? ? dt.Rows.Add(dr);
? ? ? ? ? ? }
? ? ? ? ? ? gridControl1.DataSource = dt;
? ? ? ? }
? ? ? ? //返回圖片的字節(jié)流byte[]
? ? ? ? private byte[] getImageByte(string imagePath)
? ? ? ? {
? ? ? ? ? ? FileStream files = new FileStream(imagePath, FileMode.Open);
? ? ? ? ? ? byte[] imgByte = new byte [files.Length ];
? ? ? ? ? ? files.Read(imgByte, 0, imgByte.Length);
? ? ? ? ? ? files.Close();
? ? ? ? ? ? return imgByte;
? ? ? ? }
26.檢查數(shù)據(jù)的有效性
? 在gridview的ValidateRow事件中加入檢查代碼:
? #region 檢查數(shù)據(jù)
? private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
? {
? GridView view = sender as GridView;
? view.ClearColumnErrors();
? if (view.GetRowCellValue(e.RowHandle, "ReceiveDate") == DBNull.Value)
? {
? e.Valid = false;
? view.SetColumnError(view.Columns["ReceiveDate"], "必須指定日期");
? }
? }
27.設(shè)某一列文字和標(biāo)題局中顯示? ? ? ? ? ? ? ? ?
? gridView1.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
? gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
28.列表過濾條件多選
列名.OptionsFilter.FilterPopupMode= DevExpress.XtraGrid.Columns.FilterPopupMode.CheckedList
29.隔行換色的方法
this.gridView1.Appearance.OddRow.BackColor = Color.White;? // 設(shè)置奇數(shù)行顏色 // 默認(rèn)也是白色 可以省略
this.gridView1.OptionsView.EnableAppearanceOddRow = true;? // 使能 // 和和上面綁定 同時使用有效
this.gridView1.Appearance.EvenRow.BackColor = Color.WhiteSmoke; // 設(shè)置偶數(shù)行顏色
this.gridView1.OptionsView.EnableAppearanceEvenRow = true;? // 使能 // 和和上面綁定 同時使用有效
1.隱藏最上面的GroupPanel
gridView1.OptionsView.ShowGroupPanel=false;
2.得到當(dāng)前選定記錄某字段的值
sValue=Table.Rows[gridView1.FocusedRowHandle][FieldName].ToString();
3.數(shù)據(jù)只讀
gridView1.OptionsBehavior.Editable=false;
4.不顯示MasterDetailView
gridView1.OptionsDetail.EnableMasterViewMode=false;
5.修改最上面的GroupPanel內(nèi)容
gridView1.GroupPanelText="柳生飄絮";
6.設(shè)置數(shù)據(jù)源:
gridControl1.DataSource = dt;
綁定每一列的FiledName--屬性
對于Oracle數(shù)據(jù)庫,所有的在設(shè)計器里寫的程序中的字段名必須大寫,否則可能綁定不上字段,sqlserver沒有這個限制.
7.讀寫拷貝權(quán)限設(shè)置
只讀不可拷貝:
ColumnViewOptionsBehavior.Editable = False
只讀可拷貝:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = True
可編輯:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = False
8.模板列的設(shè)置:
到Columns中嗽测,在他的屬性中找到ColumnEdit.
以LookUpEdit為例:
首先從Designer左邊菜單In-Place Editor Repository中添加LookUpEdit.取名為Re1.然后.在他的Columns屬性中添加3列.Caption依次為:編號,姓名,性別.FieldName依次為:FID,FNAME,FSEX.然后將Re1的NullText設(shè)置成空.
AutoSearchColumnIndex屬性設(shè)置為2.ImmediatePopup屬性設(shè)置為True.
SearchMode設(shè)置為OnlyInPopup.
然后將這個模板列附加到我們上面提到的列1(也就是將列1的ColumnEdit屬性設(shè)成Re1)
最后我們還要在代碼里面給Re1綁定數(shù)據(jù)源和顯示項.
Re1.DataSource = DALUse.Query("select fid,fname,fsex from dual").Tables[0];
Re1.DisplayMember = "FSEX";
Re1.ValueMember = "FNAME";
9.設(shè)某一列文字和標(biāo)題局中顯示
gridView1.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
10.去掉某一列上面的自動篩選功能(Filter)
gridView1.Columns[0].OptionsFilter.AllowAutoFilter = false;
gridView1.Columns[0].OptionsFilter.AllowFilter = false;
gridView1.Columns[0].OptionsFilter.ImmediateUpdateAutoFilter = false;
11.設(shè)置凍結(jié)列(左凍結(jié))
gridView1.Columns[0].Fixed= DevExpress.XtraGrid.Columns.FixedStyle.Left;
12.得到單元格數(shù)據(jù)(0行0列)
string ss=gridView1.GetRowCellDisplayText(0, gridView1.Columns[0]);
string ss = gridView1.GetRowCellValue(0, gridView1.Columns[0]);
13.設(shè)置單元格數(shù)據(jù)(將0行0列的單元格賦值123)
gridView1.SetRowCellValue(0, gridView1.Columns[0], "123");
13.手動添加dev的列
DevExpress.XtraGrid.Columns.GridColumn Col1=new DevExpress.XtraGrid.Columns.GridColumn ();
Col1.FieldName="FID";
Col1.Visible=true;
Col1.VisibleIndex=gridView1.Columns.Count;
gridView1.Columns.Add(Col1);
14.設(shè)置自動增加的行號,需要先添加給gridview添加事件CustomDrawRowIndicator
private void gridview_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
15.刪除: (修改了dgvdel里的datagridviewdel方法)
public static void datagridviewdel_Dev(DevExpress.XtraGrid.Views.Grid.GridView Mydgv)
{
if (MessageBox.Show("你確定要刪除選中的記錄嗎绪励?", "刪除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
{
int iSelectRowCount = Mydgv.SelectedRowsCount;
if (iSelectRowCount > 0)
{
Mydgv.DeleteSelectedRows();
}
}
}
16. 新增: (對于新增,其本身的AddNewRow方法就可以做到)
private void btn_add_Click(object sender, EventArgs e)
{
gridView1.AddNewRow();
}
具體如果對于新加行還有什么特別的設(shè)置,可以在它gridView1_InitNewRow事件中填寫:
private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
{
ColumnView View = sender as ColumnView;
View.SetRowCellValue(e.RowHandle, View.Columns[0], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[0])); //復(fù)制最后一行的數(shù)據(jù)到新行
View.SetRowCellValue(e.RowHandle, View.Columns[1], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[1])); //復(fù)制最后一行的數(shù)據(jù)到新行
}
17. 保存 (第三方控件提供的RefreshData和RefreshDataSource方法對于保存數(shù)據(jù)都不好使,最后還是使用了Dgvsave的datagridviewsave方法,用這個方法就可以)
18.特效:gridcontrol中有5種view 型式肿孵,普通的是gridview,然后分別為cardview疏魏、BandedView停做、Advanced BandedView、LayoutView大莫;共5種蛉腌。
1)、view組中把OptionView下的viewmode 設(shè)置成“Carousel”就達到這種“旋轉(zhuǎn)木馬”式的gridcontrol view 特效了
2)、layoutView1.OptionsCarouselMode.PitchAngle 這個屬性決定“旋轉(zhuǎn)木馬”的pitch angle 螺距角; 螺旋角; 螺旋升角; 俯仰角; 傾角; 節(jié)錐半角
3)烙丛、Roll Angle 屬性決定著 傾側(cè)角度
4)舅巷、指定數(shù)據(jù)源,顯示數(shù)據(jù):
//顯示數(shù)據(jù)
private void showData(List<Employee > list)
{
DataTable dt = new DataTable("OneEmployee");
dt.Columns.Add("Caption", System.Type.GetType("System.String"));
dt.Columns.Add("Department", System.Type.GetType("System.String"));
dt.Columns.Add("PhotoName", System.Type.GetType("System.Byte[]"));
for (int i = 0; i < list.Count; i++)
{
DataRow dr = dt.NewRow();
dr["Caption"] = list[i].Name;
dr["Department"] = list[i].Department;
string imagePath = @"D:\C#\photos\" + list[i].PhotoPath;
dr["PhotoName"] = getImageByte(imagePath);
dt.Rows.Add(dr);
}
gridControl1.DataSource = dt;
}
//返回圖片的字節(jié)流byte[]
private byte[] getImageByte(string imagePath)
{
FileStream files = new FileStream(imagePath, FileMode.Open);
byte[] imgByte = new byte [files.Length ];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
return imgByte;
}
19.檢查數(shù)據(jù)的有效性
在gridview的ValidateRow事件中加入檢查代碼:
#region 檢查數(shù)據(jù)
private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
{
GridView view = sender as GridView;
view.ClearColumnErrors();
if (view.GetRowCellValue(e.RowHandle, "ReceiveDate") == DBNull.Value)
{
e.Valid = false;
view.SetColumnError(view.Columns["ReceiveDate"], "必須指定日期");
}
}
#endregion
調(diào)用gridview.UpdateCurrentRow()方法執(zhí)行檢查
最常用的DevExpress Winform 4個代碼片段:
一 河咽、GridControl的刪除操作
private void rILinkEditInfoDel_Click(object sender, EventArgs e)
{
if (XtraMessageBox.Show("請確定是否刪除當(dāng)前記錄钠右?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
DataRow row = gvInfos.GetDataRow(gvInfos.FocusedRowHandle);
delByCode(row["Code"].ToString());
XtraMessageBox.Show("操作成功!");
}
}
二忘蟹、綁定非數(shù)據(jù)表中列
Hashtable ht = new Hashtable();
private void gridView6_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
object needAlert = View.GetRowCellValue(e.RowHandle, View.Columns["needAlert"]);
if (needAlert != null & needAlert != DBNull.Value && needAlert.ToString().Trim() != "0" & View.GetRowCellValue(e.RowHandle, View.Columns["Value"]) != DBNull.Value)
{
decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["Value"]));
object MinValue = View.GetRowCellValue(e.RowHandle, View.Columns["MinValue"]);
object MaxVlaue = View.GetRowCellValue(e.RowHandle, View.Columns["MaxValue"]);
if (MinValue != DBNull.Value & MinValue != null & MaxVlaue.ToString() != "" & MaxVlaue != DBNull.Value && MaxVlaue != null & MaxVlaue.ToString() != "")
{
decimal gridColumn2 = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["MinValue"]));
decimal gridColumn1 = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["MaxValue"]));
if (gridColumn2 > AverValue || AverValue > gridColumn1)
{
if (!ht.ContainsKey("pic"))
ht.Add("pic", GetImage(1));
e.Value = ht["pic"];
}
}
}
}
}
/// <summary>
/// 由資源文件獲取圖片
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
byte[] GetImage(int key)
{
Image img = DevExpress.Utils.Controls.ImageHelper.CreateImageFromResources(string.Format("RiverSys.Resources.{0}.gif", key.ToString()), typeof(RiverInfos).Assembly);
return DevExpress.XtraEditors.Controls.ByteImageConverter.ToByteArray(img, ImageFormat.Gif);
}
/// <summary>
/// 動態(tài)根據(jù)條件設(shè)置行樣式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView6_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
object needAlert = View.GetRowCellValue(e.RowHandle, View.Columns["needAlert"]);
if (needAlert != null & needAlert != DBNull.Value && needAlert.ToString().Trim() != "0" & View.GetRowCellValue(e.RowHandle, View.Columns["Value"]) != DBNull.Value)
{
decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["Value"]));
object MinValue = View.GetRowCellValue(e.RowHandle, View.Columns["MinValue"]);
object MaxVlaue = View.GetRowCellValue(e.RowHandle, View.Columns["MaxValue"]);
if (MinValue != DBNull.Value & MinValue != null & MaxVlaue.ToString() != "" & MaxVlaue != DBNull.Value && MaxVlaue != null & MaxVlaue.ToString() != "")
{
decimal gridColumn2 = Convert.ToDecimal(MinValue);
decimal gridColumn1 = Convert.ToDecimal(MaxVlaue);
if (gridColumn2 > AverValue || AverValue > gridColumn1)
{
e.Appearance.ForeColor = Color.Red;
e.Appearance.BackColor = Color.LightGray;
}
}
}
}
}
三飒房、GridControl 中顏色選擇控件
private void gvMapColor_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
GridView view = sender as GridView;
DataView dv = view.DataSource as DataView;
if (e.IsGetData)
{
string strVal = dv[e.ListSourceRowIndex]["Color"].ToString();
if (strVal != "")
{
//e.Value = DevExpress.Utils.StyleLayout.ColorFromString(strVal);
e.Value = Common.HexToColor(strVal);
}
}
else
{
//Color colorVal = DevExpress.Utils.StyleLayout.ColorFromString(e.Value.ToString());
Color colorVal = (Color)e.Value;
dv[e.ListSourceRowIndex]["Color"] = Common.RGB_HEX(colorVal.ToArgb());
}
}
四、關(guān)于 GridControl 驗證示例
/**//// <summary>
/// 初始化GridView媚值,綁定數(shù)據(jù)
/// </summary>
/// <param name="parentId"></param>
private void GridViewBindData(string parentId)
{
this.gridView1.Columns.Clear();
this.FDs= areaSetupActionHelper.getDsRegionByParentId(parentId);
this.gridCArea.DataSource =this.FDs.Tables[0].DefaultView;
this.gridView1.Columns["id"].VisibleIndex = -1;
this.gridView1.Columns["childCounts"].VisibleIndex = -1;
this.gridView1.Columns["reg_id"].Caption = "區(qū)劃編號";
this.gridView1.Columns["reg_name"].Caption = "區(qū)劃名稱";
this.gridView1.Columns["parent_id"].Caption = "父區(qū)劃編號";
this.gridView1.Columns["reg_desc"].Caption = "區(qū)劃描述";
this.gridView1.Columns["parent_id"].ImageIndex =1;
this.gridView1.Columns["reg_desc"].ImageIndex = 0;
RepositoryItemTextEdit textEditReg_Id = new RepositoryItemTextEdit();
textEditReg_Id.Mask.EditMask =parentId+"\\d{2,3}";
textEditReg_Id.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Regular;
this.gridView1.Columns["reg_id"].ColumnEdit = textEditReg_Id;
this.gridView1.Columns["reg_desc"].ColumnEdit = new RepositoryItemMemoExEdit();
TreeListNode node = this.treelArea.FocusedNode.ParentNode;
string fid = node==null?"0":node.GetValue("RegID").ToString().Trim();
DataSet ds = areaSetupActionHelper.getDsRegionByParentId(fid);
RepositoryItemLookUpEdit lookUEParent_Id = new RepositoryItemLookUpEdit();
lookUEParent_Id.Columns.Add(new LookUpColumnInfo("reg_id", 40, "區(qū)劃編號"));
lookUEParent_Id.Columns.Add(new LookUpColumnInfo("reg_name", 40, "區(qū)劃名稱"));
lookUEParent_Id.DataSource = ds.Tables[0].DefaultView;
lookUEParent_Id.ValueMember = "reg_id";
lookUEParent_Id.DisplayMember = "reg_id";
this.gridView1.Columns["parent_id"].ColumnEdit = lookUEParent_Id;
}
/**//// <summary>
/// gridView單元格驗證的相關(guān)處理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
{
if (e.Valid == false&this.gridView1.FocusedColumn.FieldName == "reg_id")
{
e.ErrorText = "區(qū)劃編號不合法狠毯!\n應(yīng)為父區(qū)劃編號加2~3位數(shù)據(jù)組成!";
}
if (this.gridView1.FocusedColumn.FieldName == "reg_name")
{
Regex reg=new Regex(@"[\u4e00-\u9fa5]{1,20}");
Match m=reg.Match(e.Value.ToString().Trim());
if (m.Length != e.Value.ToString().Trim().Length)
{
e.Valid = false;
e.ErrorText = "區(qū)劃名稱應(yīng)為漢字\n長度為1至20";
}
}
}
private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e)
{
if (MyDialog.Alert(" 您所填寫的內(nèi)容不符合規(guī)則\n 要放棄您剛才對此項所做的更改嗎褥芒?", "您所編輯的內(nèi)容不符合規(guī)則", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
e.ExceptionMode = ExceptionMode.Ignore;
}
}
/**//// <summary>
/// gridView行驗證的相關(guān)處理程序
/// </summary>
private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
{
string regid = this.gridView1.GetRowCellValue(e.RowHandle, "reg_id").ToString().Trim();
string regName = this.gridView1.GetRowCellValue(e.RowHandle, "reg_name").ToString().Trim();
if ( regid.Length < 1)
{
e.Valid = false;
this.gridView1.SetColumnError(this.gridView1.Columns["reg_id"], "請?zhí)顚憛^(qū)劃編號!", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
// e.ErrorText = "區(qū)劃名稱不能為空!";
}
if (regName.Length < 1)
{
e.Valid = false;
this.gridView1.SetColumnError(this.gridView1.Columns["reg_name"], "區(qū)劃名稱不能為空!", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
}
}
private void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
{
if (e.RowHandle >= 0)
{
if (this.gridView1.GetRowCellValue(e.RowHandle, this.gridView1.Columns["reg_id"]).ToString().Trim() == "" || this.gridView1.GetRowCellValue(e.RowHandle, this.gridView1.Columns["reg_name"]).ToString().Trim() == "")
{
if (MyDialog.Alert(" 您所填寫的內(nèi)容不符合規(guī)則\n 要放棄您剛才對此項所做的更改嗎嚼松?", "您所編輯的內(nèi)容不符合規(guī)則", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
e.ExceptionMode = ExceptionMode.Ignore;
}
else
{
e.ExceptionMode = ExceptionMode.NoAction;
}
}
}
else
{
e.ExceptionMode = ExceptionMode.Ignore;
}
}
表單綁定數(shù)據(jù)集刷新GridView數(shù)據(jù):
gv_zdfw.RefreshData();
gv_zdfw.RefreshRowCell(viewIndex.FocusedRowHandle, gv_zdfw.Columns["MDMZDFW_ZDBH"]);
gv_zdfw.RefreshRow(viewIndex.FocusedRowHandle);
GridView數(shù)據(jù)刷新表單綁定數(shù)據(jù)集:
gv_zdfw.CloseEditor();
分類: DEV
標(biāo)簽: DEV