1、 如何解決單擊記錄整行選中的問題
View->OptionsBehavior->EditorShowMode 設置爲:Click
2、 如何新增一條記錄
(1)、gridView.AddNewRow()
(2)、實現 gridView_InitNewRow 事件
3、如何解決 GridControl 記錄能獲取而沒有顯示出來的問題
gridView.populateColumns();
4、如何讓行只能選擇而不能編輯(或編輯某一單元格)
(1)、View->OptionsBehavior->EditorShowMode 設置爲:Click
(2)、View->OptionsBehavior->Editable 設置爲:false
5、如何禁用 GridControl 中單擊列彈出右鍵菜單
設置 Run Design->OptionsMenu->EnableColumnMenu 設置爲:false
6、如何隱藏 GridControl 的 GroupPanel 表頭
設置 Run Design->OptionsView->ShowGroupPanel 設置爲:false
7、如何禁用 GridControl 中列頭的過濾器 過濾器如下圖所示:
設置 Run Design->OptionsCustomization->AllowFilter 設置爲:false
8、如何在查詢得到 0 條記錄時顯示自定義的字符提示/顯示 如圖所示:
方法如下:
//When no Records Are Being Displayed
private void gridView1_CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e)
{
//方法一(此方法爲GridView設置了數據源綁定時,可用)
ColumnView columnView = sender as ColumnView;
BindingSource bindingSource = this.gridView1.DataSource as BindingSource;
if(bindingSource.Count == 0)
{
string str = "沒有查詢到你所想要的數據!";
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沒有設置數據源綁定時,使用,一般使用此種方 法)
if (this._flag)
{ if (this.gridView1.RowCount == 0)
{ string str = "沒有查詢到你所想要的數據!"; 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、如何顯示水平滾動條?或
設置 this.gridView.OptionsView.ColumnAutoWidth = false;
.....列表寬度自適應內容
gridview1.BestFitColumns();
10、如何定位到第一條數據/記錄?
設置 this.gridView.MoveFirst()
11、如何定位到下一條數據/記錄?
設置 this.gridView.MoveNext()
12、如何定位到最後一條數據/記錄?
設置 this.gridView.MoveLast()
13、設置成一次選擇一行,並且不能被編輯
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、如何讓各列頭禁止移動?
設置 gridView1.OptionsCustomization.AllowColumnMoving = false;
16、如何讓各列頭禁止排序?
設置 gridView1.OptionsCustomization.AllowSort = false;
17、如何禁止各列頭改變列寬?
設置 gridView1.OptionsCustomization.AllowColumnResizing = false;
18.拖動滾動條時固定某一列
設置Columns,選擇要固定的列。設置Fixed屬性,可以選擇:固定在左邊、固定在右邊、不固定。
|
19.獲取選定行,指定列單元格的內容
return gridView1.GetRowCellValue(pRows[0], ColumName).ToString ();
20.分組顯示
OptionsView>OptionsBehavior>AutoExpandAllGroups = True
選擇要分組的列,將GroupIndex屬性設置爲0
21.格式化數據
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 = "合格數量必須爲大於等於0小於等於接貨數量的整數!";
}
else
{
if (int.Parse(passQty) > receiveQty)
{
e.Valid = false;
e.ErrorText = "合格數量必須爲大於0小於等於接貨數量的整數!";
}
}
}
}
22.合併表頭
///初始化表格
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraEditors.Repository;
private void InitGrid()
{
// advBandedGridView1是表格上的默認視圖,注意這裏聲明的是:BandedGridView
BandedGridView view = advBandedGridView1 as BandedGridView;
view.BeginUpdate(); //開始視圖的編輯,防止觸發其他事件
view.BeginDataUpdate(); //開始數據的編輯
view.Bands.Clear();
view.OptionsView.ShowColumnHeaders = false; //因爲有Band列了,所以把ColumnHeader隱藏
//添加列標題
//添加列標題
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("分數");
GridBand bandMath = bandScore.Children.AddBand("數學");
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();//結束數據的編輯
view.EndUpdate(); //結束視圖的編輯
}
23. //動態添加列
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。設置自動增加的行號
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 設置成“Carousel”就達到這種“旋轉木馬”式的gridcontrol view 特效了
2)、layoutView1.OptionsCarouselMode.PitchAngle 這個屬性決定“旋轉木馬”的pitch angle 螺距角; 螺旋角; 螺旋升角; 俯仰角; 傾角; 節錐半角
3)、Roll Angle 屬性決定着 傾側角度
4)、指定數據源,顯示數據:
//顯示數據
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;
}
//返回圖片的字節流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.檢查數據的有效性
在gridview的ValidateRow事件中加入檢查代碼:
#region 檢查數據
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.設某一列文字和標題局中顯示
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; // 設置奇數行顏色 // 默認也是白色 可以省略
this.gridView1.OptionsView.EnableAppearanceOddRow = true; // 使能 // 和和上面綁定 同時使用有效
this.gridView1.Appearance.EvenRow.BackColor = Color.WhiteSmoke; // 設置偶數行顏色
this.gridView1.OptionsView.EnableAppearanceEvenRow = true; // 使能 // 和和上面綁定 同時使用有效
30.合併單元格
設置Gridview的AllowCellMerge屬性爲ture,然後設置要合併列的AllowCellMerge爲True,這樣某列相同行內容就會合並了
31.如果要改變單元格樣式,如字體顏色,背景什麼,像html頁面樣式。。。。
將ColumnEdit改成repositoryItemRichTextEdit然後ColumnEdit=>DocumentFormat改成html,這樣單元格就可以顯示跟html一樣的樣式,很方便
32.動態綁定列的下拉框值
private DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox repositoryItemImageComboBox_Test = new DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox();
private void gvList_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
string fieldName = e.Column.FieldName;
DataRow selectRow = gvList.GetDataRow(e.RowHandle);
if (fieldName == "test")
{
if (Convert.ToInt32(selectRow["aaa"]) == 0)
{
repositoryItemImageComboBox_Test.Items.AddRange(new DevExpress.XtraEditors.Controls.ImageComboBoxItem[] {
new DevExpress.XtraEditors.Controls.ImageComboBoxItem("A", "A", -1),
new DevExpress.XtraEditors.Controls.ImageComboBoxItem("B", "B", -1),
new DevExpress.XtraEditors.Controls.ImageComboBoxItem("C", "C", -1),
new DevExpress.XtraEditors.Controls.ImageComboBoxItem("D", "D", -1)});
e.RepositoryItem = repositoryItemImageComboBox_Test;
}
else
{
repositoryItemImageComboBox_Test.Items.AddRange(new DevExpress.XtraEditors.Controls.ImageComboBoxItem[] {
new DevExpress.XtraEditors.Controls.ImageComboBoxItem("A", "A", -1),
new DevExpress.XtraEditors.Controls.ImageComboBoxItem("B", "B", -1),
new DevExpress.XtraEditors.Controls.ImageComboBoxItem("C", "C", -1),
e.RepositoryItem = repositoryItemImageComboBox_Test;
}
}
}
轉自:http://blog.csdn.net/happy09li/article/details/7186829
40.當單元格編輯是下來框的時候,不允許手動輸入