1. 程式人生 > >GridControl常見用法【轉】

GridControl常見用法【轉】

解決 綁定 tst add strong 指定 value option xtra

剛接觸DevExpress第三方控件,把GridControl的常見用法整理一下,以供參考:

說明:

gcTest GridControl

gvText GridView

//隱藏最上面的GroupPanel 即去掉"Drag a Column Header Here To Group by that Column"
gvText.OptionsView.ShowGroupPanel = false;

//修改最上面的GroupPanel
gvText.GroupPanelText = "修改後的內容";

//單元格不可編輯
gvText.OptionsBehavior.Editable = false;

//某列標題居中 0是列索引
gvText.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

//某列內容居中 0是列索引
gvText.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

//凍結列
gvText.Columns[0].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

//自動改變行高適應內容
gvText.OptionsView.RowAutoHeight = true;

//顯示自動篩選行
gvText.OptionsView.ShowAutoFilterRow = false;

//不顯示字表信息
gvText.OptionsView.ShowDetailButtons = false;


//顯示水平滾動條,自動列寬
gvText.OptionsView.ColumnAutoWidth=false;

//自動調整列寬
gvText.BestFitColumns();

//禁用GridControl中單擊列彈出右鍵菜單
gvText.OptionsMenu.EnableColumnMenu = false;

//禁用GridControl中列頭的過濾器
gvText.OptionsCustomization.AllowFilter = false;

//禁止各列頭移動
gvText.OptionsCustomization.AllowColumnMoving = false;

//禁止各列頭排序
gvText.OptionsCustomization.AllowSort = false;

//禁止各列頭改變列寬
gvText.OptionsCustomization.AllowColumnResizing = false;

//根據綁定的數據源自動產生列,有時可以解決GridControl記錄能獲取而沒有顯示出來的問題
gvText.PopulateColumns();

//奇偶行變色
gvText.OptionsView.EnableAppearanceEvenRow = true;
gvText.OptionsView.EnableAppearanceOddRow = true;
gvText.Appearance.EvenRow.BackColor = Color.Gray;
gvText.Appearance.OddRow.BackColor = Color.GreenYellow;


//特殊列設置 日期
string strDate="yyyy-MM-dd HH:mm";
RepositoryItemDateEdit ride = new RepositoryItemDateEdit();
ride.DisplayFormat.FormatString = strDate;
ride.EditFormat.FormatString = strDate;
ride.EditMask = strDate;
gvText.Columns["日期"].ColumnEdit = ride;

//列格式設置 decimal 價格
public const string MoneyFormatStr = "##,###,###,###,##0.00";
RepositoryItemCalcEdit rice = new RepositoryItemCalcEdit();
rice.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
rice.DisplayFormat.FormatString = MoneyFormatStr;
rice.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
rice.EditFormat.FormatString = MoneyFormatStr;
rice.EditMask = MoneyFormatStr;
gv.Columns["價格"].ColumnEdit = rice;

//給單元格賦值
gvText.SetRowCellValue(3, gvText.Columns["列名或列索引"],"要賦的值");

//添加行
gvText.AddNewRow();

//添加列
DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
col.Caption = "列標題";
col.FieldName = "列字段值";
col.Visible = true;
col.VisibleIndex = gvText.Columns.Count;
gvText.Columns.Add(col);
/// <summary>
/// 獲取選定行指定列單元格的值
/// </summary>
/// <param name="str">指定列的列名</param>
/// <returns>單元格的值</returns>
public string GetCellValue(string str) {
int[] pRows = this.gvText.GetSelectedRows();
if (pRows.GetLength(0) > 0){
return gvText.GetRowCellValue(pRows[0], str).ToString();
}
else {
return null;
}
}

GridControl常見用法【轉】