【WinForm】Dev GridControl 使用汇总

https://www.cnblogs.com/Mars-0603/p/14658923.html#_label0_1

【WinForm】Dev GridControl 使用汇总

        </h1>
        <div class="clear"></div>
        <div class="postBody">
            <div id="cnblogs_post_body" class="blogpost-body blogpost-body-html"><a rel="nofollow noopener" name="_labelTop"></a><div id="navCategory" style="color:#152e97;"><p style="font-size:18px;"><b>目录</b></p><ul><li><a rel="nofollow noopener" href="#_label0">一、外观属性汇总</a><ul><li><a rel="nofollow noopener" href="#_label0_0">1、DevExPress GridControl 禁用列头筛选(过滤)</a></li><li><a rel="nofollow noopener" href="#_label0_1">2、改变Dev控件GridControl表头颜色(或改变绑定的Skin样式)</a></li><li><a rel="nofollow noopener" href="#_label0_2">3、改变GridControl单元格颜色</a></li><li><a rel="nofollow noopener" href="#_label0_3">4、gridcontrol设置隔行变色、焦点行颜色</a></li><li><a rel="nofollow noopener" href="#_label0_4">5、设置GridControl的焦点行</a></li><li><a rel="nofollow noopener" href="#_label0_5">6、GridControl禁用列排序</a></li><li><a rel="nofollow noopener" href="#_label0_6">8、禁用GridControl中单击列弹出右键菜单</a></li><li><a rel="nofollow noopener" href="#_label0_7">9、隐藏GridControl的GroupPanel表头</a></li><li><a rel="nofollow noopener" href="#_label0_8">10、显示水平滚动条</a></li><li><a rel="nofollow noopener" href="#_label0_9">11、定位到指定记录</a></li><li><a rel="nofollow noopener" href="#_label0_10">12、设置成一次选择一行,并且不能被编辑</a></li><li><a rel="nofollow noopener" href="#_label0_11">13、读写拷贝权限设置</a></li><li><a rel="nofollow noopener" href="#_label0_12">14、让各列头禁止移动</a></li><li><a rel="nofollow noopener" href="#_label0_13">15、禁止各列头改变列宽</a></li><li><a rel="nofollow noopener" href="#_label0_14">16、列表宽度自适应内容</a></li><li><a rel="nofollow noopener" href="#_label0_15">17、修改最上面的GroupPanel内容</a></li><li><a rel="nofollow noopener" href="#_label0_16">18、设某一列文字和标题居中显示</a></li><li><a rel="nofollow noopener" href="#_label0_17">19、设置冻结列(左冻结)</a></li><li><a rel="nofollow noopener" href="#_label0_18">20、设置单元格不可编辑</a></li><li><a rel="nofollow noopener" href="#_label0_19">21、常规要设置的属性记录</a></li><li><a rel="nofollow noopener" href="#_label0_20">&nbsp;21、动态根据条件设置行样式</a></li></ul></li><li><a rel="nofollow noopener" href="#_label1">二、GridControl绑定、更新数据源</a></li><li><a rel="nofollow noopener" href="#_label2">三、绑定的数据源有值但不显示的问题</a></li><li><a rel="nofollow noopener" href="#_label3">四、GridControl中使用GridFormatRule改变行/单元格样式</a></li><li><a rel="nofollow noopener" href="#_label4">五、GridControl单元格绑定图片</a></li><li><a rel="nofollow noopener" href="#_label5">六、GridControl双击行事件</a></li><li><a rel="nofollow noopener" href="#_label6">七、从表格获取数据</a><ul><li><a rel="nofollow noopener" href="#_label6_0">1、得到当前选定记录某字段的值</a></li><li><a rel="nofollow noopener" href="#_label6_1">2、得到单元格数据(0行0列)</a></li><li><a rel="nofollow noopener" href="#_label6_2">3、得到GridControl的列标题的值</a></li></ul></li><li><a rel="nofollow noopener" href="#_label7">八、自动增加序号</a></li><li><a rel="nofollow noopener" href="#_label8">九、GridControl选中行事件</a></li><li><a rel="nofollow noopener" href="#_label9">十、上移、下移</a></li><li><a rel="nofollow noopener" href="#_label10">&nbsp;十一、添加勾选框</a></li><li><a rel="nofollow noopener" href="#_label11">十二、单独修改某一单元格的值</a></li><li><a rel="nofollow noopener" href="#_label12">十三、添加右键菜单</a></li><li><a rel="nofollow noopener" href="#_label13">十四、删除行</a></li></ul></div><p>&nbsp;</p><hr style="height:1px;border:none;border-top:1px dashed #0066CC;">

一、外观属性汇总

1、DevExPress GridControl 禁用列头筛选(过滤)

GridControl属性搜索AllowFilter,设置为false

2、改变Dev控件GridControl表头颜色(或改变绑定的Skin样式)

方法一:

改变GridControl自定义column  Header背景颜色属性

在Form窗体上拖放了一个GridControl,尝试修改列头 column header 背景颜色. 但是修改AppearanceHeaderPanel 属性后,并没有效果出现, 我使用的是 Outlook 默认皮肤 。

后来发现原因是GridControl的Header使用了Devexpress皮肤中的图片作为背景。如果你想手动更改ColumnHeader的背景色,可以这样做:

1. 设置 GridControl的LookAndFeel.UseDefaultLookAndFeel 属性为 False

2. 设置 GridControl 的LookAndFeel.Style 属性为 Flat( UltraFlat 或 Style3D)

3. 设置 GridColumn.AppearanceHeader.BackColor 为你需要的颜色。

 

方法二:

使用SkinEditor创建自定义的皮肤,这个不难,因为是继承的Devexpress皮肤模板,在皮肤中移除列头的图片背景或者更换需要的图片背景。

http://www.360doc.com/content/11/0425/19/6832513_112260508.shtml

如果有其他地方皮肤需要自定义可以选择第二种方式。

 

方法三:

实现 GridView.CustomDrawColumnHeader 事件

3、改变GridControl单元格颜色

https://bbs.csdn.net/topics/350220798

4、gridcontrol设置隔行变色、焦点行颜色

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

5、设置GridControl的焦点行

gridView2.FocusedRowHandle = dtselected.Rows.Count - 1;

6、GridControl禁用列排序

AllowSort属性设为False
 

7、让行只能选择而不能编辑(或编辑某一单元格)

(1)View->OptionsBehavior->EditorShowMode 设置为:Click
(2)View->OptionsBehavior->Editable 设置为:false
 

8、禁用GridControl中单击列弹出右键菜单

设置Run Design->OptionsMenu->EnableColumnMenu 设置为:false
 

9、隐藏GridControl的GroupPanel表头

设置Run Design->OptionsView->ShowGroupPanel 设置为:false

10、显示水平滚动条

设置this.gridView.OptionsView.ColumnAutoWidth = false;

11、定位到指定记录

定位到第一条数据/记录: 设置 this.gridView.MoveFirst()
定位到下一条数据/记录: 设置 this.gridView.MoveNext()
定位到最后一条数据/记录:设置 this.gridView.MoveLast()

12、设置成一次选择一行,并且不能被编辑

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

13、读写拷贝权限设置

只读不可拷贝:
ColumnViewOptionsBehavior.Editable = False
只读可拷贝:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = True
可编辑:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = False

14、让各列头禁止移动

设置gridView1.OptionsCustomization.AllowColumnMoving = false;

15、禁止各列头改变列宽

设置gridView1.OptionsCustomization.AllowColumnResizing = false;

16、列表宽度自适应内容

gridview1.BestFitColumns();

17、修改最上面的GroupPanel内容

gridView1.GroupPanelText="xxx";

18、设某一列文字和标题居中显示

gridView1.Columns[0].AppearanceHeader.TextOptions.HAlignment =DevExpress.Utils.HorzAlignment.Center;
gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment =DevExpress.Utils.HorzAlignment.Center;

19、设置冻结列(左冻结)

gridView1.Columns[0].Fixed= DevExpress.XtraGrid.Columns.FixedStyle.Left;

 

20、设置单元格不可编辑

复制代码
复制代码
  private void LinkGridView_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (LinkGridView.FocusedColumn.FieldName == "name1" || LinkGridView.FocusedColumn.FieldName == "name2")
            {
                int row = this.LinkGridView.FocusedRowHandle;
                string ss = LinkGridView.GetRowCellDisplayText(row, LinkGridView.Columns["Xx"]);
                string ss2 = LinkGridView.GetRowCellDisplayText(row, LinkGridView.Columns["xX"]);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ss.Contains(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">xx</span><span style="color: rgba(128, 0, 0, 1)">"</span>) || ss2.Contains(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))
            {
                e.Cancel </span>= <span style="color: rgba(0, 0, 255, 1)">true</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该行不可编辑</span>

}
}
}

复制代码
复制代码

 

21、常规要设置的属性记录

RunDesiger后

 属性设置代码:
复制代码
复制代码
 private void BindDataSource(DataTable dt)
     {

         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">绑定DataTable</span>
gridControl1.DataSource = dt;
         gridView1.OptionsCustomization.AllowColumnMoving </span>= <span style="color: rgba(0, 0, 255, 1)">false</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如何让各列头禁止移动</span>
gridView1.OptionsCustomization.AllowFilter = false;//不允许过滤 gridView1.OptionsCustomization.AllowSort = false;//不允许排序 gridView1.OptionsCustomization.AllowGroup = false;//不允许分组 gridView1.OptionsView.ShowGroupPanel = false;//不显示组面板 gridView1.OptionsSelection.MultiSelect = true;//可以多选 gridView1.OptionsSelection.MultiSelectMode =
             DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">RowSelect; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">选择模式MultiSelectMode = CheckBoxRowSelect,                                                          </span>
gridView1.OptionsView.ColumnAutoWidth = true;//列宽自动 gridView1.OptionsCustomization.AllowColumnResizing = false;//不允许改变列宽 gridView1.OptionsBehavior.Editable = true;//允许编辑 gridView1.OptionsView.EnableAppearanceEvenRow = true;//设置奇、偶行交替颜色 gridView1.OptionsView.EnableAppearanceOddRow = true;//设置奇、偶行交替颜色 gridView1.Appearance.EvenRow.BackColor=Color.DeepSkyBlue;
         gridView1.Appearance.OddRow.BackColor </span>=<span style="color: rgba(0, 0, 0, 1)"> Color.CornflowerBlue;

         </span><span style="color: rgba(0, 0, 255, 1)">this</span>.gridView1.IndicatorWidth = <span style="color: rgba(128, 0, 128, 1)">30</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置显示行号的列宽</span>
MessageBox.Show((gridView1.GetDataRow(0)[0]).ToString ());//获取第二行第二列数据,下标从零开始
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">.AllowFilter = False

         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">绑定DataSet

         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">gridControl1.DataSource = ds;

         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">gridControl1.DataMember = "表名";</span>
}
复制代码
复制代码

 

 21、动态根据条件设置行样式

复制代码
复制代码
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"]));
              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() != "")
              {
                 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绑定、更新数据源

1、GridControl绑定数据源

https://blog.csdn.net/weixin_34391854/article/details/93839053

 

2、GridControl数据修改后实时更新数据源

https://www.cnblogs.com/Jingkunliu/p/4125744.html

 

 

三、绑定的数据源有值但不显示的问题

解决方法:
1、检查数据源是否有值;
2、用

      gridView1.RefreshData();

      gc.RefreshDataSource() 更新数据源

3、绑定的顺序很重要。尝试在更新数据源之前加上gc.DataSource = dt;

4、如果是绑定List,那么list里的类字段定义时必须按照get;set;形式来写

 

四、GridControl中使用GridFormatRule改变行/单元格样式

1、用法示例

设置方法(官方):

(1)https://docs.devexpress.com/WindowsForms/DevExpress.XtraEditors.FormatConditionRuleExpression

(2)https://docs.devexpress.com/WindowsForms/114615/controls-and-libraries/data-grid/getting-started/walkthroughs/appearance-and-conditional-formatting/tutorial-custom-styles-for-rows-and-cells

 

直接代码调用:https://blog.csdn.net/duanzi_peng/article/details/78791513

 

2、当判断依据为字符时,要加引号

 Expression = "[结果] == '不合格'"

 

五、GridControl单元格绑定图片

https://www.cnblogs.com/wuhuacong/p/9145765.html

 

 

六、GridControl双击行事件

https://www.cnblogs.com/xiaofengfeng/archive/2011/09/15/2177625.html

 

七、从表格获取数据

1、得到当前选定记录某字段的值

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

2、得到单元格数据(0行0列)

string ss=gridView1.GetRowCellDisplayText(0,gridView1.Columns[0]);
string ss = gridView1.GetRowCellValue(0, gridView1.Columns[0]);

3、得到GridControl的列标题的值

DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[0];第一个列标题

 

 

 

八、自动增加序号

GridView添加事件:

 

 

同时设定宽度:

gv_Msg.IndicatorWidth = 30;

 

九、GridControl选中行事件

选中事件有两个:
focusedRowChanged()
selectionChanged()
当选择模式为单行时,须使用focusedRowChanged()

 

十、上移、下移

  DataTable dtPostgreSql = new DataTable();
  gridControl1.DataSource = dtPostgreSql;
复制代码
复制代码
if (gridView1.SelectedRowsCount < 1)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            if (gridView1.SelectedRowsCount == 1)
            {
                int selectIndex = this.gridView1.GetDataSourceRowIndex(this.gridView1.FocusedRowHandle);
                if (selectIndex - 1 >= 0)
                {
                    object[] ot = dtPostgreSql.Rows[selectIndex].ItemArray;
                    DataRow dr = dtPostgreSql.Rows[selectIndex];
                    dtPostgreSql.Rows.Remove(dr);
                    DataRow drs = dtPostgreSql.NewRow();
                    drs.ItemArray = ot;
                    dtPostgreSql.Rows.InsertAt(drs, selectIndex - 1);
                    gridView1.FocusedRowHandle = selectIndex - 1;
                    dtPostgreSql.AcceptChanges();
                }
                else
                    MessageBox.Show("已到第一行不能继续上移");
            }
            if (gridView1.SelectedRowsCount > 1)
            {
                int[] index = new int[gridView1.SelectedRowsCount];
                int[] rows = gridView1.GetSelectedRows();
                int j=0;
                if (rows[0] - 1 >= 0)
                {
                    for (int i = rows[0]; i < rows[rows.Length - 1] + 1; i++)
                    {
                        object[] ot = dtPostgreSql.Rows[i].ItemArray;
                        DataRow dr = dtPostgreSql.Rows[i];
                        dtPostgreSql.Rows.Remove(dr);
                        DataRow drs = dtPostgreSql.NewRow();
                        drs.ItemArray = ot;
                        dtPostgreSql.Rows.InsertAt(drs, i - 1);
                        index[j] = i - 1;
                        j++;
                    }
                    gridView1.SelectRows(index[0], index[index.Length - 1]);
                    dtPostgreSql.AcceptChanges();
                }else
                    MessageBox.Show("已到第一行不能继续上移");
        }</span></pre>
复制代码
复制代码

下移:

复制代码
复制代码
     if (gridView1.SelectedRowsCount < 1)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            if (gridView1.SelectedRowsCount >= 1)
            {
                int[] index = new int[gridView1.SelectedRowsCount];
                int[] rows = gridView1.GetSelectedRows();
                int j = 0;
                if (rows[rows.Length - 1] <= gridView1.RowCount-2)
                {
                    for (int i = rows[rows.Length - 1]; i > rows[0]- 1; i--)
                    {
                        object[] ot = dtPostgreSql.Rows[i].ItemArray;
                        DataRow dr = dtPostgreSql.Rows[i];
                        dtPostgreSql.Rows.Remove(dr);
                        DataRow drs = dtPostgreSql.NewRow();
                        drs.ItemArray = ot;
                        dtPostgreSql.Rows.InsertAt(drs, i +1);
                        index[j] = i +1;
                        j++;
                    }
                    gridView1.SelectRows(index[0], index[index.Length - 1]);
                    dtPostgreSql.AcceptChanges();
                }
                else
                    MessageBox.Show("已到最后一条记录不能继续下移");
        }</span></pre>
复制代码
复制代码

 

参考:https://blog.csdn.net/fangyu723/article/details/104971670

 

 十一、添加勾选框

GridControl控件要设置为不可编辑状态,在“OptionsBehavior”下将“Editable”的值改为false,否则左键单击不管用。

 

进入“Run Designer”添加三列,在添加该列时将其属性[ColumnEdit]从下拉框中选择“CheckEdit”,然后打开该属性的下来箭头,设置其“ValueChecked”和“ValueUnChecked”的类型及值,这里我用了int类型的1和0来作为状态的指示。

【注:最好还是不要用默认的Bool型的true和false,这样容易出现总是无法显示打钩选中的情况,但是用int型的就不会出现此情况;我找到问题所在了:因为我用了ToString函数,这时会自动把true或者false改变为大写的“True”或“False”,因此判断时就不正确】
选中GridControl的GridView,然后添加其事件“RowClick”,添加事件代码如下:

复制代码
复制代码
private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string midValueStr = gridView1.GetDataRow(e.RowHandle)["select"].ToString();
            if (midValueStr == "0" || midValueStr == "")
            {
                gridView1.GetDataRow(e.RowHandle)["select"] = 1;
            }
            else
            {
                gridView1.GetDataRow(e.RowHandle)["select"] = 0;
            }
        }
复制代码
复制代码

注:if条件中的空值判断,我的这一列是在数据库取出数据后新添加的,所以其值取出来是空的,而不是0.因此,该行未选中时可能有两种值即0和空。

出处:链接

 

十二、单独修改某一单元格的值

gv.SetRowCellValue(gv.FocusedRowHandle, gv.Columns[colNames[j]], rowValues.ToArray()[i][j].ToString());

 

十三、添加右键菜单

1.从工具箱中拖拽一个标准控件ContextMenuStrip到窗体中;

2.点击控件右上方的小三角,添加成员子菜单MenuItem,注意顶端出现的名称;

3.在gdc(不是gdv)控件的ContextMenuStrip属性中选择刚才创建的控件contextPopMenu;

4.添加gdv控件的PopupMenuShowing事件,处理点击右键后,弹出菜单前的处理机制;

5.在PopupMenuShowing事件中可根据当前选中行状态,判断是否弹出菜单,如

 if (!btAlterFlt.Enabled) //编辑状态解钩弹出菜单和控件的关系,实现右键无操作
       gdcFlts.ContextMenuStrip = null;
else
      gdcFlts.ContextMenuStrip = contextPopMenu;

MenuItemEnterBuffer.Text = $"加入缓冲区 ({buffer.Count})"; //改写菜单项名称

6.在contextPopMenu控件的ItemClicked事件进行处理。

右键点击某菜单项后,gdv的焦点也会切换到当前行,可在此事件中通过FocusRowHandel获取。

参考:链接

 

十四、删除行

intiSelectRowCount = dgv.SelectedRowsCount;
if(iSelectRowCount > 0)
{
    dgv.DeleteSelectedRows();
}

 

<div class="clear"></div>
<div id="post_next_prev">

<a href="https://www.cnblogs.com/Mars-0603/p/14658870.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/Mars-0603/p/14658870.html" data-featured-image="" title="发布于 2021-04-14 17:07">【Winform 】加载中Loading效果</a>
<br>
<a href="https://www.cnblogs.com/Mars-0603/p/14666871.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/Mars-0603/p/14666871.html" data-featured-image="" title="发布于 2021-04-16 14:45">【c#】数组使用汇总</a>
posted @   不溯流光  阅读(842)  评论(0编辑  收藏  举报
</div><!--end: topics 文章、评论容器-->
posted @   mkmkbj  阅读(842)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示