您好,登錄后才能下訂單哦!
默認我們的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; } 應該還有更多好的辦法,希望大家能批評改正
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。