上善若水

水善利万物而不争
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# DataGridView 常用设置

Posted on 2022-09-01 09:14  董锡振  阅读(340)  评论(0编辑  收藏  举报

 

--》错误提示 :操作无效,原因是它导致对 SetCurrentCellAddressCore 函数的可重入调用。

解决方法:

 

 private void dataGridPen_CurrentCellDirtyStateChanged(object sender, EventArgs e)
 {
    if (dataGridPen.IsCurrentCellDirty)
    {
         dataGridPen.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
 }

 

--》点击单元格选中整行:

设置DataGridView的属性SelectionMode为FullRowSelect

--》设置不自动显示数据库中未绑定的列

this.dgvResult.AutoGenerateColumns = false;

--》设置默认的第一个空白列不显示

RowHeadersViible=false 

--》DataGridView  常用样式设置

 

 

 private void GridViewStyle(DataGridView dgv)
        {
            #region DataGridVeiw Style
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            dgv.AllowUserToAddRows = false;
            dgv.AllowUserToDeleteRows = false;
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;
            dgv.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            dgv.BackgroundColor = System.Drawing.Color.White;
            dgv.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            dgv.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
            dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
            dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;
            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dgv.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
            dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dgv.EnableHeadersVisualStyles = false;
            dgv.GridColor = System.Drawing.SystemColors.GradientInactiveCaption;
            //dgv.ReadOnly = true;
            dgv.RowHeadersVisible = false; //建议改为true;为了以后显示序号。
            dgv.RowTemplate.Height = 23;
            //dgv.RowTemplate.ReadOnly = true;
            #endregion
        }