GirdView1的常用属性.docx
《GirdView1的常用属性.docx》由会员分享,可在线阅读,更多相关《GirdView1的常用属性.docx(26页珍藏版)》请在冰豆网上搜索。
![GirdView1的常用属性.docx](https://file1.bdocx.com/fileroot1/2023-3/2/1f47af50-0470-435f-a50f-1d8b7e4a74b4/1f47af50-0470-435f-a50f-1d8b7e4a74b41.gif)
GirdView1的常用属性
C#中gridView常用属性和技巧介绍-凌风95
时间 2014-08-1500:
37:
00 博客园-所有随笔区原文
1.隐藏最上面的GroupPanel
gridView1.OptionsView.ShowGroupPanel=false;
2.得到当前选定记录某字段的值
sValue=Table.Rows[gridView1.FocusedRowHandle][FieldName].ToString();
3.数据只读
gridView1.OptionsBehavior.Editable=false;
4.不显示MasterDetailView
gridView1.OptionsDetail.EnableMasterViewMode=false;
5.修改最上面的GroupPanel内容
gridView1.GroupPanelText="柳生飘絮";
6.设置数据源:
gridControl1.DataSource=dt;
绑定每一列的FiledName--属性
对于Oracle数据库,所有的在设计器里写的程序中的字段名必须大写,否则可能绑定不上字段,sqlserver没有这个限制.
7.读写拷贝权限设置
只读不可拷贝:
ColumnViewOptionsBehavior.Editable=False
只读可拷贝:
ColumnViewOptionsBehavior.Editable=True
OptionsColumn.AllowEdit=True
OptionsColumn.ReadOnly=True
可编辑:
ColumnViewOptionsBehavior.Editable=True
OptionsColumn.AllowEdit=True
OptionsColumn.ReadOnly=False
8.模板列的设置:
到Columns中,在他的属性中找到ColumnEdit.
以LookUpEdit为例:
首先从Designer左边菜单In-PlaceEditorRepository中添加LookUpEdit.取名为Re1.然后.在他的Columns属性中添加3列.Caption依次为:
编号,姓名,性别.FieldName依次为:
FID,FNAME,FSEX.然后将Re1的NullText设置成空.
AutoSearchColumnIndex属性设置为2.ImmediatePopup属性设置为True.
SearchMode设置为OnlyInPopup.
然后将这个模板列附加到我们上面提到的列1(也就是将列1的ColumnEdit属性设成Re1)
最后我们还要在代码里面给Re1绑定数据源和显示项.
Re1.DataSource=DALUse.Query("selectfid,fname,fsexfromdual").Tables[0];
Re1.DisplayMember="FSEX";
Re1.ValueMember="FNAME";
9.设某一列文字和标题局中显示
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.设置冻结列(左冻结)
gridView1.Columns[0].Fixed=DevExpress.XtraGrid.Columns.FixedStyle.Left;
12.得到单元格数据(0行0列)
stringss=gridView1.GetRowCellDisplayText(0,gridView1.Columns[0]);
stringss=gridView1.GetRowCellValue(0,gridView1.Columns[0]);
13.设置单元格数据(将0行0列的单元格赋值123)
gridView1.SetRowCellValue(0,gridView1.Columns[0],"123");
13.手动添加dev的列
DevExpress.XtraGrid.Columns.GridColumnCol1=newDevExpress.XtraGrid.Columns.GridColumn();
Col1.FieldName="FID";
Col1.Visible=true;
Col1.VisibleIndex=gridView1.Columns.Count;
gridView1.Columns.Add(Col1);
14.设置自动增加的行号,需要先添加给gridview添加事件CustomDrawRowIndicator
privatevoidgridview_CustomDrawRowIndicator(objectsender,DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgse)
{
if(e.Info.IsRowIndicator&&e.RowHandle>=0)
e.Info.DisplayText=(e.RowHandle+1).ToString();
}
15.删除:
(修改了dgvdel里的datagridviewdel方法)
publicstaticvoiddatagridviewdel_Dev(DevExpress.XtraGrid.Views.Grid.GridViewMydgv)
{
if(MessageBox.Show("你确定要删除选中的记录吗?
","删除提示",MessageBoxButtons.YesNo,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2,0,false)==DialogResult.Yes)
{
intiSelectRowCount=Mydgv.SelectedRowsCount;
if(iSelectRowCount>0)
{
Mydgv.DeleteSelectedRows();
}
}
}
16.新增:
(对于新增,其本身的AddNewRow方法就可以做到)
privatevoidbtn_add_Click(objectsender,EventArgse)
{
gridView1.AddNewRow();
}
具体如果对于新加行还有什么特别的设置,可以在它gridView1_InitNewRow事件中填写:
privatevoidgridView1_InitNewRow(objectsender,DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgse)
{
ColumnViewView=senderasColumnView;
View.SetRowCellValue(e.RowHandle,View.Columns[0],gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount-2),gridView1.Columns[0]));//复制最后一行的数据到新行
View.SetRowCellValue(e.RowHandle,View.Columns[1],gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount-2),gridView1.Columns[1]));//复制最后一行的数据到新行
}
17.保存(第三方控件提供的RefreshData和RefreshDataSource方法对于保存数据都不好使,最后还是使用了Dgvsave的datagridviewsave方法,用这个方法就可以)
18.特效:
gridcontrol中有5种view型式,普通的是gridview,然后分别为cardview、BandedView、AdvancedBandedView、LayoutView;共5种。
1)、view组中把OptionView下的viewmode设置成“Carousel”就达到这种“旋转木马”式的gridcontrolview特效了
2)、layoutView1.OptionsCarouselMode.PitchAngle这个属性决定“旋转木马”的pitchangle螺距角;螺旋角;螺旋升角;俯仰角;倾角;节锥半角
3)、RollAngle属性决定着倾侧角度
4)、指定数据源,显示数据:
//显示数据
privatevoidshowData(Listlist)
{
DataTabledt=newDataTable("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(inti=0;i{
DataRowdr=dt.NewRow();
dr["Caption"]=list[i].Name;
dr["Department"]=list[i].Department;
stringimagePath=@"D:
\C#\photos"+list[i].PhotoPath;
dr["PhotoName"]=getImageByte(imagePath);
dt.Rows.Add(dr);
}
gridControl1.DataSource=dt;
}
//返回图片的字节流byte[]
privatebyte[]getImageByte(stringimagePath)
{
FileStreamfiles=newFileStream(imagePath,FileMode.Open);
byte[]imgByte=newbyte[files.Length];
files.Read(imgByte,0,imgByte.Length);
files.Close();
returnimgByte;
}
19.检查数据的有效性
在gridview的ValidateRow事件中加入检查代码:
#region检查数据
privatevoidgridView1_ValidateRow(objectsender,ValidateRowEventArgse)
{
GridViewview=senderasGridView;
view.ClearColumnErrors();
if(view.GetRowCellValue(e.RowHandle,"ReceiveDate")==DBNull.Value)
{
e.Valid=false;
view.SetColumnError(view.Columns["ReceiveDate"],"必须指定日期");
}
}
#endregion
调用gridview.UpdateCurrentRow()方法执行检查
最常用的DevExpressWinform4个代码片段:
一、GridControl的删除操作
privatevoidrILinkEditInfoDel_Click(objectsender,EventArgse)
{
if(XtraMessageBox.Show("请确定是否删除当前记录?
","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
{
DataRowrow=gvInfos.GetDataRow(gvInfos.FocusedRowHandle);
delByCode(row["Code"].ToString());
XtraMessageBox.Show("操作成功!
");
}
}
二、绑定非数据表中列
Hashtableht=newHashtable();
privatevoidgridView6_CustomUnboundColumnData(objectsender,DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgse)
{
GridViewView=senderasGridView;
if(e.RowHandle>=0)
{
objectneedAlert=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)
{
decimalAverValue=Convert.ToDecimal(View.GetRowCellValue(e.RowHandle,View.Columns["Value"]));
objectMinValue=View.GetRowCellValue(e.RowHandle,View.Columns["MinValue"]);
objectMaxVlaue=View.GetRowCellValue(e.RowHandle,View.Columns["MaxValue"]);
if(MinValue!
=DBNull.Value&MinValue!
=null&MaxVlaue.ToString()!
=""&MaxVlaue!
=DBNull.Value&&MaxVlaue!
=null&MaxVlaue.ToString()!
="")
{
decimalgridColumn2=Convert.ToDecimal(View.GetRowCellValue(e.RowHandle,View.Columns["MinValue"]));
decimalgridColumn1=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"];
}
}
}
}
}
///
///由资源文件获取图片
///
///
///
byte[]GetImage(intkey)
{
Imageimg=DevExpress.Utils.Controls.ImageHelper.CreateImageFromResources(string.Format("RiverSys.Resources.{0}.gif",key.ToString()),typeof(RiverInfos).Assembly);
returnDevExpress.XtraEditors.Controls.ByteImageConverter.ToByteArray(img,ImageFormat.Gif);
}
///
///动态根据条件设置行样式
///
///
///
privatevoidgridView6_RowStyle(objectsender,DevExpress.XtraGrid.Views.Grid.RowStyleEventArgse)
{
GridViewView=senderasGridView;
if(e.RowHandle>=0)
{
objectneedAlert=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)
{
decimalAverValue=Convert.ToDecimal(View.GetRowCellValue(e.RowHandle,View.Columns["Value"]));
objectMinValue=View.GetRowCellValue(e.RowHandle,View.Columns["MinValue"]);
objectMaxVlaue=View.GetRowCellValue(e.RowHandle,View.Columns["MaxValue"]);
if(MinValue!
=DBNull.Value&MinValue!
=null&MaxVlaue.ToString()!
=""&MaxVlaue!
=DBNull.Value&&MaxVlaue!
=null&MaxVlaue.ToString()!
="")
{
decimalgridColumn2=Convert.ToDecimal(MinValue);
decimalgridColumn1=Convert.ToDecimal(MaxVlaue);
if(gridColumn2>AverValue||AverValue>gridColumn1)
{
e.Appearance.ForeColor=Color.Red;
e.Appearance.BackColor=Color.LightGray;
}
}
}
}
}
三、GridControl中颜色选择控件
privatevoidgvMapColor_CustomUnboundColumnData(objectsender,DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgse)
{
GridViewview=senderasGridView;
DataViewdv=view.DataSourceasDataView;
if(e.IsGetData)
{
stringstrVal=dv[e.ListSourceRowIndex]["Color"].ToString();
if(strVal!
="")
{
//e.Value=DevExpress.Utils.StyleLayout.ColorFromString(strVal);
e.Value=Common.HexToColor(strVal);
}
}
else
{
//ColorcolorVal=DevExpress.Utils.StyleLayout.ColorFromString(e.Value.ToString());
ColorcolorVal=(Color)e.Value;
dv[e.ListSourceRowIndex]["Color"]=Common.RGB_HEX(colorVal.ToArgb());
}
}
四、关于GridControl验证示例
///
///初始化GridView,绑定数据
///
///
privatevoidG