Laden...

Spalte als DataGridViewButtonColumn soll in Abhängigkeit keinen Button darstellen, nur Text

Erstellt von diana vor 6 Jahren Letzter Beitrag vor 6 Jahren 1.209 Views
D
diana Themenstarter:in
586 Beiträge seit 2004
vor 6 Jahren
Spalte als DataGridViewButtonColumn soll in Abhängigkeit keinen Button darstellen, nur Text

... ist so etwas möglich und hat jemend eine Idee? Vielen Dank schon mal im Voraus 😉

:::

D
diana Themenstarter:in
586 Beiträge seit 2004
vor 6 Jahren
dgrv.Rows[ds.Tables[0].Rows.Count - 1].Cells["col"] = new System.Windows.Forms.DataGridViewTextBoxCell();

Funktioniert!

:::

2.298 Beiträge seit 2010
vor 6 Jahren

Hallo,

auch wenn du schon eine Lösung hast, wäre es eventuell eine Option von der ButtonCell und ButtonColumn abzuleiten.

Ich habe da gerade mal etwas rumprobiert. Folgendes kam dabei raus (noch nicht optimal):


    public class DataGridViewButtonCellEx : DataGridViewButtonCell
    {
        private bool _hideButton;

        public bool HideButton
        {
            get { return _hideButton; }
            set { 
                _hideButton = value;                
            }
        }

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            if (!this._hideButton)
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            else
            {
                graphics.FillRectangle(Brushes.White, cellBounds);
                using (Brush b = new SolidBrush(Color.Black))
                {
                    Font f = this.Style.Font != null ? this.Style.Font : this.DataGridView.DefaultCellStyle.Font;
                    SizeF size = graphics.MeasureString(formattedValue.ToString(), f);

                    graphics.DrawString(formattedValue.ToString(), f, b, cellBounds.X + (cellBounds.Width - size.Width)/2, cellBounds.Y + (cellBounds.Height - size.Height)/2);
                   
                }
            }   
        } 
    }

    public class DataGridViewButtonColumnEx : DataGridViewButtonColumn
    {        
        public override DataGridViewCell CellTemplate
        {
            get
            {
                if (!(base.CellTemplate is DataGridViewButtonCellEx))
                    base.CellTemplate = new DataGridViewButtonCellEx();

                return base.CellTemplate;
            }
            set
            {
                if (value is DataGridViewButtonCellEx)
                    base.CellTemplate = value;
                else
                    throw new ArgumentException("The cell template has the wrong style!");
            }
        }
    }

Wird für die Zelle HideButton auf true gesetzt, wird nur der Text gezeichnet.

Das Ergebnis sieht dann wie folgt aus:

Wissen ist nicht alles. Man muss es auch anwenden können.

PS Fritz!Box API - TR-064 Schnittstelle | PS EventLogManager |

2.207 Beiträge seit 2011
vor 6 Jahren

Hallo diana,

bitte benutze die richtigen Code-Tags. Du bist lang genug dabei.

Gruss

Coffeebean