DevExpress使用方法GridControl總結

1.隱藏最上面的GroupPanelhtml

  gridView1.OptionsView.ShowGroupPanel=false; web

2.獲得當前選定記錄某字段的值數據庫

  sValue=Table.Rows[gridView1.FocusedRowHandle][FieldName].ToString();express

3.數據只讀less

  gridView1.OptionsBehavior.Editable=false;函數

4.不顯示MasterDetailViewpost

gridView1.OptionsDetail.EnableMasterViewMode=false;字體

5.修改最上面的GroupPanel內容this

gridView1.GroupPanelText="柳生飄絮";spa

6.數據綁定:

FieldName --數據庫的字段名稱

7.讀寫拷貝權限設置

ColumnView.Editable
    This property returns a value of the ColumnViewOptionsBehavior.Editable option
    不可寫
ColumnViewOptionsBehavior.Editable
    Gets or sets whether end users are allowed to invoke cell editors
    可讀可寫

OptionsColumn.AllowEdit
    Gets or sets whether end users are allowed to invoke editors for the column's cells.
    可讀可寫
    只有ColumnViewOptionsBehavior.Editable = True設置OptionsColumn.AllowEdit纔有意義

OptionsColumn.ReadOnly
    Gets or sets whether end-users are prevented from editing the column's cell values. 
    可讀可寫

If the ReadOnly property is set to true, the cell values of columns cannot be modified by end-users. In this case cell editors can be invoked but end users can only select and copy an editor's content. This option is not in effect if the column's AllowEdit or the view's ColumnView.Editable option is disabled.

Cell values can still be modified in code using the ColumnView.SetRowCellValue method regardless of the ReadOnly property's setting.

只讀不可拷貝:
    ColumnViewOptionsBehavior.Editable = False

只讀可拷貝:
    ColumnViewOptionsBehavior.Editable = True
    OptionsColumn.AllowEdit = True
    OptionsColumn.ReadOnly = True

可編輯:
    ColumnViewOptionsBehavior.Editable = True
    OptionsColumn.AllowEdit = True
    OptionsColumn.ReadOnly = False

出處:https://blog.csdn.net/liushengpiaoxu/article/details/3420996

=============================================================

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中列頭的過濾器

過濾器以下圖所示:

DevExpress GridControl使用方法總結

設置 Run Design->OptionsCustomization->AllowFilter 設置爲:false

8、如何在查詢獲得0條記錄時顯示自定義的字符提示/顯示

如圖所示:

DevExpress GridControl使用方法總結

方法以下:

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

10、如何定位到第一條數據/記錄?

設置 this.gridView.MoveFirst()

11、如何定位到下一條數據/記錄?

設置 this.gridView.MoveNext()

12、如何定位到最後一條數據/記錄?

設置 this.gridView.MoveLast()

十3、設置成一次選擇一行,而且不能被編輯

this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus; this.gridView1.OptionsBehavior.Editable = false; this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;

十4、如何顯示行號?

this.gridView1.IndicatorWidth = 40; //顯示行的序號 private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e) {      if (e.Info.IsRowIndicator && e.RowHandle>=0)      {           e.Info.DisplayText = (e.RowHandle + 1).ToString();      } }

十5、如何讓各列頭禁止移動?

設置gridView1.OptionsCustomization.AllowColumnMoving = false;

十6、如何讓各列頭禁止排序?

設置gridView1.OptionsCustomization.AllowSort = false;

十7、如何禁止各列頭改變列寬?

設置gridView1.OptionsCustomization.AllowColumnResizing = false;

 

出處:https://www.cnblogs.com/starksoft/p/4936517.html

=======================================================================================

devexpress表格控件gridcontrol設置隔行變色、焦點行顏色、設置(改變)顯示值、固定列不移動(附源碼)

介紹一些經常使用的gridcontrol設置。

一、設置隔行變色。首先設置顯示隔行變色,步驟:OptionsView-->EnableAppearanceEvenRow-->true和OptionsView-->EnableAppearanceOddRow-->true;而後設置奇數行和偶數行樣式顏色等:Appearance-->EvenRow和Appearance-->OddRow。設計完成後,設計器出現隔行變色效果,如圖:

二、設置奇偶行樣式時,會看到其餘行樣式。Appearance-->FoucsedRow就是焦點行顏色,設置後可突出顯示焦點行樣式,如圖所示:

三、設置顯示值,有個兩種方法。第一種能夠在如圖所示地方設置,列顯示出來就會加上單位元;用的最多的通常是設置日期樣式yyyy年MM月dd日:

第二種可以使用代碼改變,代碼以下:

        /// <summary>
        /// 改變顯示值
        /// </summary>
        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "A3")
            {
                if (e.Value.ToString() == "1")
                    e.DisplayText = "男";
                else
                    e.DisplayText = "女";
            }
        }

四、有時表格列太多,須要拖動查看,可是又但願某些列能始終固定不移動。這個時候就能夠設置該列爲固定列了,屬性如圖所示:

五、運行效果圖:

 附源碼:http://files.cnblogs.com/files/starksoft/demo006.rar

附加內容:根據條件改變行的樣式(字體顏色、背景顏色、漸變色)

複製代碼
private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            int hand = e.RowHandle;//行號
            if(hand<0)
            {
                return;
            }
            DataRow dr = gridView1.GetDataRow(hand);
            if (dr == null)
                return;

            //int selectedHandle = gridView1.GetSelectedRows()[hand];
            if (gridView1.GetRowCellValue(hand, "CODE").ToString().Contains("5"))
            {
                e.Appearance.ForeColor = Color.Red;//字體顏色
                e.Appearance.BackColor=Color.Linen;//行背景顏色
                //e.Appearance.BackColor2 = Color.Blue;//漸變顏色
            }
        }
複製代碼

 

 

 

出處:https://www.cnblogs.com/starksoft/p/4936207.html

 

GridControl_gridView 單元格內容換行

經過正常的添加列,或者下面的方式添加列:

DevExpress.XtraGrid.Columns.GridColumn gcContent = gridView1.Columns.AddField("Content");

或者

gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
new DevExpress.XtraGrid.Columns.GridColumn { Visible=true,Caption="通知公告", FieldName="Content"},
new DevExpress.XtraGrid.Columns.GridColumn { Visible=true,Caption="時間", FieldName="CreateTime", MaxWidth=100, MinWidth=90},
new DevExpress.XtraGrid.Columns.GridColumn { Visible=true,Caption="發佈人", FieldName="Creator", MaxWidth=120, MinWidth=90}
});

再經過設置列的ColumnEidt屬性,使其換行。

RepositoryItemMemoEdit repoMemo = new RepositoryItemMemoEdit();
repoMemo.WordWrap = true;
repoMemo.AutoHeight = true;
this.gridControl1.RepositoryItems.Add(repoMemo);
this.gridControl1.gridView1.Columns[0].ColumnEdit=repoMemo
this.gridControl1.gridView1.OptionsView.RowAutoHeight = true;

 我本身寫了個變體,其實效果是同樣的

gridControl1.RepositoryItems.Add(new RepositoryItemMemoEdit { Name= "Content", WordWrap = true,AutoHeight=true });
gridView1.Columns[0].ColumnEdit = gridControl1.RepositoryItems["Content"];
gridView1.OptionsView.RowAutoHeight = true;

參考出處:https://blog.csdn.net/ljunqiang/article/details/40346171

 

=======================================================================

 

表頭字體顏色
for (int i = 0; i < this.gridView1.Columns.Count; i++)
{
this.gridView1.Columns[i].AppearanceHeader.ForeColor = Color.Red;
}

表頭背景顏色
for (int i = 0; i < this.gridView1.Columns.Count; i++)
{
this.gridView1.Columns[i].AppearanceHeader.BackColor = Color.Red;
this.gridView1.Columns[i].AppearanceHeader.BackColor2 = Color.Red;
}
在form1構造方法中加入 gridView1.CustomDrawColumnHeader+= OnCustomDrawColumnHeader; 添加方法代碼以下
private void OnCustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
//背景顏色沒有設置且爲空,則默認
if (e.Column == null || (e.Column.AppearanceHeader.BackColor == Color.Empty && !e.Column.AppearanceHeader.Options.UseBackColor))
return;
Rectangle rect = e.Bounds;
rect.Inflate(-1, -1);
// 填充表頭背景顏色
e.Graphics.FillRectangle(new SolidBrush(e.Column.AppearanceHeader.BackColor), rect);
// 填充表頭字體顏色
e.Appearance.DrawString(e.Cache, e.Info.Caption, e.Info.CaptionRect);
e.Handled = true;
}

字體顏色
form1構造函數中加入:this.gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(this.gridView1_RowCellStyle); 添加方法代碼以下
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
e.Appearance.ForeColor = Color.Red;
}
//獲取焦點(只有獲取焦點後纔會改變顏色)
this.gridControl1.Focus();

選中行顏色
this.gridView1.Appearance.FocusedRow.BackColor = Color.Red;
this.gridView1.Appearance.FocusedRow.BackColor2 = Color.Red;

表體顏色
一、首先開啓gridControl 奇偶行外觀
this.gridView1.OptionsView.EnableAppearanceEvenRow = true;
this.gridView1.OptionsView.EnableAppearanceOddRow = true;
二、 再改變表體顏色
this.gridView1.Appearance.OddRow.BackColor = Color.Red;
this.gridView1.Appearance.OddRow.BackColor2 = Color.Red;
this.gridView1.Appearance.EvenRow.BackColor = Color.Red;
this.gridView1.Appearance.EvenRow.BackColor2 = Color.Red;

表格線顏色
this.gridView1.Appearance.HorzLine.BackColor= Color.Red;
this.gridView1.Appearance.HorzLine.BackColor2 = Color.Red;
this.gridView1.Appearance.VertLine.BackColor = Color.Red;
this.gridView1.Appearance.VertLine.BackColor2 = Color.Red;

選中單元格顏色
this.gridView1.Appearance.FocusedCell.BackColor = Color.Red;
this.gridView1.Appearance.FocusedCell.BackColor2 = Color.Red;

空白區域顏色
gridView1.Appearance.Empty.BackColor = Color.Red;

 

出處:https://blog.csdn.net/weixin_42609389/article/details/82109148

 ========================================================

設置gridControl的gridView的每行的字體和大小

表頭:gridView1.Appearance.HeaderPanel.Font = new Font("Tahoma", 14);

表體:gridView1.Appearance.Row.Font = new Font("Tahoma", 14);

 

單元格都靠左或者右邊顯示

run 進去後選擇須要左或右顯示的列。找到列屬性中的AppearanceCell下的TextOptions下的HAlignment 選擇 Far 靠左 center 中間 defalut 靠右

 

DevExpress GridControl 選擇整行被選單元格不變色的設置

設置GridControl 裏面的 gridview 屬性, 找到OptionSelection 將EnableAppearanceFocusedCell 屬性設置False 就能夠了

 

 

 

================================================================================================

使用Devpress LayoutControl控件後,運行軟件時,右擊控件的時候有菜單customize layout 彈出來,我想問下怎麼把它屏蔽了?

答:layoutControl1.AllowCustomization = false;

出處:https://bbs.csdn.net/topics/390692246

================================================================================================

相關文章
相關標籤/搜索