默認(rèn)我們的DataGridView cell是沒(méi)有邊框的,無(wú)倫是正常狀態(tài)還是編輯狀態(tài),因?yàn)橄到y(tǒng)把TextBox的BorderStyle值改成了None,所以這里首先要把BorderStyle值改回來(lái),那至于是FixedSingle還是Fixed3D,就得看你自己需要了,我這里改成了FixedSingle,方便繪制
主要是繼承 DataGridViewTextBoxCell 類(lèi),需要在這里改點(diǎn)東西代碼如下:
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; } } } 應(yīng)用: 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; } 應(yīng)該還有更多好的辦法,希望大家能批評(píng)改正
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
文章標(biāo)題:C#控件-顯示邊框的DataGridViewColumn-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://www.ekvhdxd.cn/article40/djoieo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站營(yíng)銷(xiāo)、做網(wǎng)站、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容