2012-09-21 13 views
8

में बटन कॉलम को अक्षम करना मेरे पास 4 कॉलम के साथ डेटा ग्रिडव्यू है, पहले 2 कॉलम कम्बोबॉक्स कॉलम हैं, तीसरा कॉलम टेक्स्टबॉक्स कॉलम है और चौथा कॉलम बटन कॉलम है। फॉर्म लोड में मुझे डेटाग्रिड के पूरे बटन कॉलम को अक्षम करना होगा और इसके बाद मुझे पहले तीन कॉलम चुनना चाहिए और इसे सहेजने के बाद डेटाबेस में इन पहले तीन कॉलम को सहेजना चाहिए, विशेष पंक्ति में बटन कॉलम सक्षम होना चाहिए। पहले तीन कॉलम को बटन पर क्लिक करके डेटाबेस में सहेजा जाना चाहिए। कृपया मेरी मदद im कई दिनों से इस समस्या के साथ मारा यहाँ कोड है जो मैं इस्तेमाल कियाडेटाग्रिडव्यू

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 का विस्तार करना होगा। This article on MSDN विवरण यह कैसे करें।

लेख कोड का एक बहुत कुछ है, और मैं तुम्हें विशेष ध्यान दें करने के लिए प्रोत्साहित करते हैं, लेकिन सभी तुम सच में क्या करने की जरूरत को कॉपी करें और अपनी परियोजना में पेस्ट लेख में पाया निम्नलिखित वर्गों है:
- DataGridViewDisableButtonColumn
- DataGridViewDisableButtonCell

एक बार ऐसा करने के बाद आप अपने डेटाग्रिड व्यू में DataGridViewDisableButtonColumn एस जोड़ सकेंगे। प्रत्येक सेल के बटन नियंत्रण की Enabled संपत्ति सेट करने के लिए अपने कस्टम कॉलम में उजागर की गई सार्वजनिक 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

एमएसडीएन आलेख में 'DataGridViewDisableButtonCell.Paint() 'विधि repaint (कोई डबल बफरिंग) पर झिलमिलाहट। [इस आलेख] (https://msdn.microsoft.com/en-us/library/ka0yazs1 (v = vs.110) .aspx) का उपयोग करके उनके कार्यान्वयन को संशोधित करने के लिए एक गाइड के रूप में। BufferedGraphics ऑब्जेक्ट बनाएं (उदा। 'MyBuffer'),' myBuffer.Graphics 'के साथ' ग्राफिक्स 'के उपयोग को प्रतिस्थापित करें, और फिर अंत में' myBuffer.Render() 'को कॉल करें। चेतावनी: 'TextRenderer.DrawText()' पर कॉल को टेक्स्ट टेक्स्ट को सही स्थिति में रखने के लिए इन टेक्स्ट प्रारूप झंडे को निर्दिष्ट करना होगा: 'PreserveGraphicsTranslateTransform | क्षैतिज केंद्र | वर्टिकल सेंटर ' –

+0

इस लिंक का समाधान भी आपके सेल स्टाइल में पैडिंग को पूरा नहीं करता है। आप पैडिंग जोड़कर "सेल स्टाइल" में मौजूद किसी भी पैडिंग को शामिल करने के लिए पेंट विधि को बदल सकते हैं। बटन समायोजन के लिए होरिज़ोंटल वैल्यू एडजस्टमेंट। विथथ, और पैडिंग। बटन एडर्टमेंट के लिए वर्टिकल वैल्यू। हाइट और सेल स्टाइल की पैडिंग जोड़ना। बटन को समायोजन समायोजन.एक्स और सेल स्टाइल की पैडिंग। बटन पर समायोजन समायोजन। वाई। –

3

आप इसे आप सक्षम जाँच करने के लिए है कि DataGridView बटन और सूचना के लिए एक वर्ग का उपयोग करता है इस MSDN लेख MSDN article:Disable button in dataGridView का उपयोग कर सकते है जब भी आप इसे संभालना चाहते हैं तो बटन की स्थिति

5

यह जे के जवाब के लिए एक पूरक है: आप स्तंभ में सभी बटन के Enabled संपत्ति सेट करना चाहते हैं जब से तुम एक सहायक विधि है कि आपके DataGridView में सभी पंक्तियों के माध्यम से लूप और Enabled संपत्ति सेट लिख सकते हैं।

अनुरोध करके, यह वह कोड है जिसे मैं एक बटन सेल बनाने के लिए उपयोग करता था जिसे अक्षम किया जा सकता था। इसमें डबल-बफरिंग शामिल है ताकि जब उपयोगकर्ता स्क्रॉल करता है तो बटन झिलमिलाहट नहीं करते हैं।

/// <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

संबंधित मुद्दे