C#控件-显示边框的DataGridViewColumn-创新互联
默认我们的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; } 应该还有更多好的办法,希望大家能批评改正
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:C#控件-显示边框的DataGridViewColumn-创新互联
标题链接:http://cdiso.cn/article/djoieo.html