2012-09-21 68 views
8

我有一個數據網格視圖,其中有4列,前2列是組合框列,第三列是文本框列,第四列是按鈕列。表單加載我必須禁用整個按鈕列的數據網格在此之後,我應該選擇前三列並將這三列保存到數據庫中,然後保存該行中的按鈕列應啓用。前三列應通過單擊按鈕保存在數據庫中。 請幫我我可是從很多天 擊中了這個問題,這裏是我用禁用datagridview中的按鈕列

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; 
      } 
    } 

回答

15

這裏有一些關於設置出現在DataGridViewButtonColumn中的按鈕的Enabled屬性的問題的幫助。

您需要擴展DataGridViewButtonColumn才能使用禁用按鈕創建您自己的DataGridView列。 This article on MSDN詳細信息如何做到這一點。

文章有大量的代碼,我鼓勵你仔細看,但你真正需要做的就是複製並粘貼到您的項目在文章中找到的類:
- DataGridViewDisableButtonColumn
- DataGridViewDisableButtonCell

一旦你這樣做,你將能夠將DataGridViewDisableButtonColumn s添加到您的DataGridView。使用公開的自定義列中公開的Enabled屬性來設置每個單元格的Button控件的Enabled屬性。既然你要設置的所有按鈕的Enabled屬性列,你可以寫,通過在DataGridView中的所有行循環,並設置Enabled屬性的輔助方法:

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(); 
} 
+7

爲什麼在默認情況下不能禁用按鈕? – Coops

+0

MSDN文章中的'DataGridViewDisableButtonCell.Paint()'方法在重繪時閃爍(無雙緩衝)。作爲指導,使用[本文](https://msdn.microsoft.com/zh-cn/library/ka0yazs1(v = vs.110).aspx)修正其實現。創建一個BufferedGraphics對象(例如'myBuffer'),用'myBuffer.Graphics'替換'graphics'的用法,然後在最後調用'myBuffer.Render()'。注意:對TextRenderer.DrawText()的調用必須指定這些文本格式標誌來正確定位按鈕文本:PreserveGraphicsTranslateTransform | Horizo​​ntalCenter | VerticalCenter' –

+0

此鏈接中的解決方案也不適合您的CellStyle中的填充。 您可以通過將「Padding.Horizo​​ntal」值添加到buttonAdjustment.Width,並將「Padding.Vertical」值添加到buttonAdjustment.Height並將cellStyle的「Padding.Left」添加到buttonAdjustment.X中,並將「繪圖方法」更改爲合併「cellStyle」 cellStyle的Padding.Top到buttonAdjustment.Y。 –

5

這是一個補充,周杰倫的答案。

通過請求,這裏是我用來創建一個可以被禁用的按鈕單元的代碼。它包含雙緩衝,以便用戶滾動時按鈕不閃爍。

/// <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); 
     } 
    } 
} 
+0

感謝您發佈此代碼,這是最好的答案! – Misiu

+0

我正在使用這個,但有沒有什麼辦法停止閱讀點擊的時候,禁用按鈕,而不檢查它的數據網格? – miguelmpn