C#控件-显示边框的DataGridViewColumn
admin
2023-07-30 20:40:29
0

默认我们的DataGridView cell是没有边框的,无伦是正常状态还是编辑状态,因为系统把TextBox的BorderStyle值改成了None,所以这里首先要把BorderStyle值改回来,那至于是FixedSingle还是Fixed3D,就得看你自己需要了,我这里改成了FixedSingle,方便绘制

主要是继承 DataGridViewTextBoxCell 类,需要在这里改点东西代码如下:

  public class DataGridViewTextBoxEditCell : DataGridViewTextBoxCell
    {
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle)
        {            
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            TextBox textBox = this.DataGridView.EditingControl as TextBox;
            if (textBox != null)
            {
                textBox.BorderStyle = BorderStyle.FixedSingle;//改回边框
            }
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
             
            //绘制边框
            Rectangle cellBounds1 = new Rectangle(cellBounds.X + 2, cellBounds.Y + 1, cellBounds.Width - 4, cellBounds.Height - 3);
            graphics.DrawRectangle(new Pen(SystemColors.WindowFrame), cellBounds1);
        } 
    }
    
     public class DataGridViewTextBoxEditColumn : DataGridViewColumn
    {
        public DataGridViewTextBoxEditColumn()
            : base(new DataGridViewTextBoxEditCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a CalendarCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(DataGridViewTextBoxEditCell)))
                {
                    throw new InvalidCastException("Must be a CalendarCell");
                }
                base.CellTemplate = value;
            }
        }
    }
    
    应用:
    private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("name1");

            DataGridViewTextBoxEditColumn dataGridViewColumn = new DataGridViewTextBoxEditColumn();
            dataGridViewColumn.HeaderText = "columns";
            dataGridViewColumn.DataPropertyName = "name";
            this.dataGridView1.Columns.Add(dataGridViewColumn);

            DataGridViewTextBoxEditColumn dataGridViewColumn1 = new DataGridViewTextBoxEditColumn();
            dataGridViewColumn1.HeaderText = "columns";
            dataGridViewColumn1.DataPropertyName = "name1";
            this.dataGridView1.Columns.Add(dataGridViewColumn1);

            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
        }
        
        应该还有更多好的办法,希望大家能批评改正


相关内容

热门资讯

表面等离激元共振全息显微镜:折... 本文由论文作者团队撰稿 导读 近期,西北工业大学赵建林教授团队在发表了题为“Refractive ...
丘成桐:基础研究人才引进不能“... (来源:New Economist) 丘成桐出席国际弦理论大会资料图。本文来源:澎湃新闻,记者 蒋...
第八届创新发展论坛金i奖榜单揭... 近日,由中国科学院《互联网周刊》、DBC德本咨询、eNet研究院共同评选的“第八届创新发展论坛金i奖...
豆包又一功能下线:应用生成功能... 7月6日,豆包在宣布全面下线“智能体”功能之前,其“应用生成”功能已于5月31日率先停止服务。 目前...
哪款耳夹式耳机性价比高?学生党... 随着蓝牙技术持续迭代,开放式耳机凭不堵耳道、佩戴更自在的优势,成为运动通勤及重度听音人群的心头好。但...
特朗普再回应巴洛贡红牌事件,称... 美国总统特朗普6日在白宫对媒体证实,他确实就美国队球员巴洛贡红牌一事与国际足联主席因凡蒂诺通了电话,...
俄称打击乌军用设施,乌称打击俄... 6日,俄罗斯方面称打击了乌克兰多处军用设施,乌克兰方面称,对距离乌边境约2500公里的俄罗斯炼油厂进...
北约秘书长被曝想把峰会办成“签... “秘书长想把峰会办成一场‘签单大会’”。7月7日至8日在土耳其安卡拉举行的北约峰会又要被北约秘书长吕...
古巴再次全国大停电 古巴电力联盟6日在社交媒体发布公告说,古巴电力系统发生全面断电。公告说,原因正在调查中,将持续更新进...
广西防汛救援推进到哪一步?最大... 台风“美莎克”对我国造成持续影响。6日,国家防总针对广西启动了防汛二级应急响应,广西南宁将防汛三级应...