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