3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
C# DataGridViewButtonColumn 支持 disable [自製元件] (GOOGLE: datagridviewbuttoncell enabled)  資料來源: https://www.twblogs.net/a/5c3203a8bd9eee35b3a4f5e7 GITHUB: https://github.com/jash-git/CS_DataGridViewButtonColumn_disable 實際圖: 程式碼: CustomDGVButtonCell.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Windows.Forms; using System.Drawing; using System.Windows.Forms.VisualStyles; namespace WindowsFormsApplication1 { //C# DataGridViewButtonColumn 支持disable //https://www.twblogs.net/a/5c3203a8bd9eee35b3a4f5e7 public class CustomDGVButtonCell : DataGridViewButtonCell { public bool Enabled { get; set; } // By default, enable the button cell. public CustomDGVButtonCell() { Enabled = true; } // Override the Clone method so that the Enabled property is copied. public override object Clone() { CustomDGVButtonCell cell = (CustomDGVButtonCell)base.Clone(); cell.Enabled = Enabled; return cell; } public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) { base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); } protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // The button cell is disabled, so paint the border, // background, and disabled button for the cell. if (!this.Enabled) { // Draw the cell background, if specified. if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) { SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor); graphics.FillRectangle(cellBackground, cellBounds); cellBackground.Dispose(); } // Draw the cell borders, if specified. if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border) { PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } // Calculate the area in which to draw the button. Rectangle buttonArea = cellBounds; Rectangle buttonAdjustment = BorderWidths(advancedBorderStyle); buttonArea.X += buttonAdjustment.X; buttonArea.Y += buttonAdjustment.Y; buttonArea.Height -= buttonAdjustment.Height; buttonArea.Width -= buttonAdjustment.Width; // Draw the disabled button. ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled); // Draw the disabled button text. if (FormattedValue is string) { TextRenderer.DrawText(graphics, (string)FormattedValue, DataGridView.Font, buttonArea, SystemColors.GrayText); } } else { // The button cell is enabled, so let the base class // handle the painting. base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } } } public class CustomDGVButtonColumn : DataGridViewButtonColumn { public CustomDGVButtonColumn() { CustomDGVButtonCell obj = new CustomDGVButtonCell(); this.CellTemplate = obj; CellTemplate = new CustomDGVButtonCell(); } } } Form1.Designer.cs namespace WindowsFormsApplication1 { partial class Form1 { /// /// 設計工具所需的變數。 /// private System.ComponentModel.IContainer components = null; /// /// 清除任何使用中的資源。 /// /// 如果應該處置 Managed 資源則為 true,否則為 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form 設計工具產生的程式碼 /// /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改 /// 這個方法的內容。 /// private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.button1 = new System.Windows.Forms.Button(); this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column2 = new CustomDGVButtonColumn(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2}); this.dataGridView1.Location = new System.Drawing.Point(12, 12); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 24; this.dataGridView1.Size = new System.Drawing.Size(630, 150); this.dataGridView1.TabIndex = 0; this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick); // // button1 // this.button1.Location = new System.Drawing.Point(12, 168); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(137, 44); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Column1 // this.Column1.HeaderText = "Column1"; this.Column1.Name = "Column1"; // // Column2 // this.Column2.HeaderText = "Column2"; this.Column2.Name = "Column2"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(706, 391); this.Controls.Add(this.button1); this.Controls.Add(this.dataGridView1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.DataGridViewTextBoxColumn Column1; private CustomDGVButtonColumn Column2; private System.Windows.Forms.Button button1; } } Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { //--- //設定&清空 dataGridView1.RowHeadersVisible = false;//DataGridView 最前面指示選取列所在位置的箭頭欄位 dataGridView1.AllowUserToAddRows = false;//是否允許使用者新增資料 dataGridView1.AllowUserToDeleteRows = false;//是否允許使用者刪除資料 dataGridView1.AllowUserToOrderColumns = false;//是否允許使用者調整欄位位置 //所有表格欄位寬度全部變成可調 dgvSub0000_01.AllowUserToResizeColumns = false;//是否允許使用者改變欄寬 dataGridView1.AllowUserToResizeRows = false;//是否允許使用者改變行高 for (int i = 0; i < dataGridView1.Columns.Count; i++) { dataGridView1.Columns[i].ReadOnly = true;//單一欄位禁止編輯 } dataGridView1.AllowUserToAddRows = false;//刪除空白列 dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;//整列選取 do { for (int i = 0; i < dataGridView1.Rows.Count; i++) { DataGridViewRow r1 = this.dataGridView1.Rows[i];//取得DataGridView整列資料 this.dataGridView1.Rows.Remove(r1);//DataGridView刪除整列 } } while (dataGridView1.Rows.Count > 0); dataGridView1.Rows.Add("000", "123"); dataGridView1.Rows.Add("456", "789"); ((CustomDGVButtonCell)dataGridView1.Rows[1].Cells[1]).Enabled = false; } catch { } } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { CustomDGVButtonCell cell = (CustomDGVButtonCell)dataGridView1.Rows[e.RowIndex].Cells[1]; if (e.ColumnIndex == 1 && cell.Enabled) { try { MessageBox.Show(cell.Value.ToString()); } catch { } } } } } } 隱藏關鍵字:DataGridView Button Column 按鈕

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦