Disabling button column in datagridview

I have a fourview gridview. The first 2 columns are column columns, the third column is a text field column, and the fourth column is a button column. In loading the form, I need to disable the entire column of datagrid buttons, after which I have to select the first three columns and save these first three columns in the database, after saving this column the buttons in the specific row should be turned on. The first three columns must be saved in the databese by clicking the button. Please help me to get this problem due to many days here is the code I used

private void SATAddTemplate_Load(object sender, EventArgs e) { foreach (DataGridViewRow row in datagrdADDTEMP.Rows) { DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3]; btn.ReadOnly = true; } } private void btnSaveSettings_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in datagrdADDTEMP.Rows) { DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3]; btn.ReadOnly = false; } } 
+8
c # datagridview
source share
3 answers

The problem of setting the Enabled property of the buttons that appear in the DataGridViewButtonColumn will help you here.

You need to extend the DataGridViewButtonColumn to create your own DataGridView column with disconnect buttons. This MSDN article details how to do this.

There is a lot of code in the article, and I recommend you look carefully, but all you really need to do is copy and paste the following classes found in the article into your project:
- DataGridViewDisableButtonColumn
- DataGridViewDisableButtonCell

Once you do this, you can add a DataGridViewDisableButtonColumn to your DataGridView. Use the public Enabled property set in your custom column to set the Enabled property for each Button control. Since you want to set the Enabled property for all buttons in a column, you can write a helper method that will go through all the rows in your DataGridView and sets the Enabled property:

 private void SetDGVButtonColumnEnable(bool enabled) { foreach (DataGridViewRow row in dataGridView1.Rows) { // Set Enabled property of the fourth column in the DGV. ((DataGridViewDisableButtonCell)row.Cells[3]).Enabled = enabled; } dataGridView1.Refresh(); } 
+15
source share

This is a complement to Jay's answer.

Upon request, here is the code that I used to create the button cell, which can be disabled. It includes double buffering, so that the buttons do not flicker as the user scrolls.

 /// <summary> /// Adapted from https://msdn.microsoft.com/en-us/library/ms171619.aspx. Double-buffering was added to remove flicker on re-paints. /// </summary> public class DataGridViewDisableButtonCell : DataGridViewButtonCell { private bool enabledValue; public bool Enabled { get { return enabledValue; } set { if (enabledValue == value) return; enabledValue = value; // force the cell to be re-painted if (DataGridView != null) DataGridView.InvalidateCell(this); } } // Override the Clone method so that the Enabled property is copied. public override object Clone() { var cell = (DataGridViewDisableButtonCell) base.Clone(); cell.Enabled = Enabled; return cell; } // By default, enable the button cell. public DataGridViewDisableButtonCell() { enabledValue = true; } 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 (!enabledValue) { var currentContext = BufferedGraphicsManager.Current; using (var myBuffer = currentContext.Allocate(graphics, cellBounds)) { // Draw the cell background, if specified. if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) { using (var cellBackground = new SolidBrush(cellStyle.BackColor)) { myBuffer.Graphics.FillRectangle(cellBackground, cellBounds); } } // Draw the cell borders, if specified. if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border) { PaintBorder(myBuffer.Graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } // Calculate the area in which to draw the button. var buttonArea = cellBounds; var 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(myBuffer.Graphics, buttonArea, PushButtonState.Disabled); // Draw the disabled button text. var formattedValueString = FormattedValue as string; if (formattedValueString != null) { TextRenderer.DrawText(myBuffer.Graphics, formattedValueString, DataGridView.Font, buttonArea, SystemColors.GrayText, TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter); } myBuffer.Render(); } } 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); } } } 
+5
source share

You can use this MSDN article. MSDN article: Disable a button in a dataGridView , it uses the class for the datagridview button and notices that you need to check the permission of the button status when you are ready to process it

+3
source share

All Articles