C# .NET - how to hide button for Particular Cell of DatagridView + Windows Appli

Asked By shraddha bhosale on 02-Sep-10 02:53 AM

Hi All,
    
      I am using DataGridView control of windows application. i have added one buttonColumn in a grid.
Now, i want to Hide button for particular row from the buttonColumn.

      Please send me solution with sample code.

Thanks.
Sagar P replied to shraddha bhosale on 02-Sep-10 03:01 AM
There are two options to make the DataGridViewButtonCell invisible.

One way is that we can custom paint the cell. Since the button inside a DataGridViewButtonCell is not an actual button, it is painted like a button, we can paint particular cell to make it looks like a common cell by handling the CellPainting event. Please check the following link for details.

  • http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting.aspx

The other way is very tricky. We can change the DataGridViewButtonCell to DataGridViewTextBoxCell to hide the Button. Here is a sample for your information.

Code Snippet

    public partial class Form17 : Form

    {

      public Form17()

      {

        InitializeComponent();

      }

 

      private void Form17_Load(object sender, EventArgs e)

      {

        DataTable dt = new DataTable();

        dt.Columns.Add("id");

        dt.Columns.Add("item");

        for (int i = 0; i < 50; i++)

        {

          dt.Rows.Add(i, "item" + i);

        }

        dt.AcceptChanges();

 

        DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();

        btnColumn.DataPropertyName = "id";

        btnColumn.Width = 100;

        btnColumn.ReadOnly = true;

        this.dataGridView1.Columns.Add(btnColumn);

        this.dataGridView1.DataSource = dt;

 

        foreach (DataGridViewRow dr in this.dataGridView1.Rows)

        {

          if (dr.Cells[0].Value != null)

          {

            int i;

            if ((int.TryParse(dr.Cells[0].Value.ToString(),out i)))

            {

              if (i % 2 == 0)

              {

                DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();

                dr.Cells[0] = txtcell;

              }

            }

          }

        }

      }

    }

Reena Jain replied to shraddha bhosale on 02-Sep-10 03:01 AM

Please try the following code:

 

    public partial class Form1 : Form

    {

      public Form1()

      {

        InitializeComponent();

      }

 

      private void Form1_Load(object sender, EventArgs e)

      {

        DataGridViewDisableButtonColumn disbtn = new DataGridViewDisableButtonColumn();

 

        this.dataGridView1.Columns.Add(disbtn);

 

        this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());

        this.dataGridView1.Rows.Add(5);

 

        ((DataGridViewDisableButtonCell)this.dataGridView1[0, 0]).Hide = true;

 

      }

    }

 

    public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn

    {

 

      public DataGridViewDisableButtonColumn()

      {

 

        this.CellTemplate = new DataGridViewDisableButtonCell();

 

      }

 

    }

 

 

 

    public class DataGridViewDisableButtonCell : DataGridViewButtonCell

    {

 

      private bool hideValue;

 

      public bool Hide

      {

 

        get

        {

 

          return hideValue;

 

        }

 

        set

        {

 

          hideValue = value;

 

        }

 

      }

 

 

 

      // Override the Clone method so that the Hide property is copied.

 

      public override object Clone()

      {

 

        DataGridViewDisableButtonCell cell =

 

          (DataGridViewDisableButtonCell)base.Clone();

 

        cell.Hide = this.Hide;

 

        return cell;

 

      }

 

 

 

      // By default, display the button cell.

 

      public DataGridViewDisableButtonCell()

      {

 

        this.hideValue = false;

 

      }

 

 

 

      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 hide, so paint the border, 

 

        // background, and hide button for the cell.

 

        if (this.hideValue)

        {

 

          // Draw the cell background, if specified.

 

          if ((paintParts & DataGridViewPaintParts.Background) ==

 

            DataGridViewPaintParts.Background)

          {

 

            SolidBrush cellBackground =

 

              new SolidBrush(cellStyle.BackColor);

 

            graphics.FillRectangle(cellBackground, cellBounds);

 

            cellBackground.Dispose();

 

          }

 

 

 

          // Draw the cell borders, if specified.

 

          if ((paintParts & DataGridViewPaintParts.Border) ==

 

            DataGridViewPaintParts.Border)

          {

 

            PaintBorder(graphics, clipBounds, cellBounds, cellStyle,

 

              advancedBorderStyle);

 

          }

 

 

          // if we don't want to draw the button at all, comment out the following lines.

 

 

          // Calculate the area in which to draw the button.

 

          //Rectangle buttonArea = cellBounds;

 

          //Rectangle buttonAdjustment =

 

          //    this.BorderWidths(advancedBorderStyle);

 

          //buttonArea.X += buttonAdjustment.X;

 

          //buttonArea.Y += buttonAdjustment.Y;

 

          //buttonArea.Height -= buttonAdjustment.Height;

 

          //buttonArea.Width -= buttonAdjustment.Width;

 

 

 

          // Draw the disabled button.         

 

          //ButtonRenderer.DrawButton(graphics, buttonArea,

 

          //    PushButtonState.Disabled);

 

 

 

          // Draw the disabled button text.

 

          //if (this.FormattedValue is String)

          //{

 

          //    TextRenderer.DrawText(graphics,

 

          //      (string)this.FormattedValue,

 

          //      this.DataGridView.Font,

 

          //      buttonArea, SystemColors.GrayText);

 

          //}

 

        }

 

        else

        {

 

          // The button cell isn't hide, so let the base class

 

          // handle the painting.

 

          base.Paint(graphics, clipBounds, cellBounds, rowIndex,

 

            elementState, value, formattedValue, errorText,

 

            cellStyle, advancedBorderStyle, paintParts);

 

        }

 

      }

 

    }

 

If you have any other questions, please feel free to ask.

 

Best regards,

Ling


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Reena Jain replied to shraddha bhosale on 02-Sep-10 03:03 AM
Code Snippet

    public partial class Form17 : Form

    {

      public Form17()

      {

        InitializeComponent();

      }

 

      private void Form17_Load(object sender, EventArgs e)

      {

        DataTable dt = new DataTable();

        dt.Columns.Add("id");

        dt.Columns.Add("item");

        for (int i = 0; i < 50; i++)

        {

          dt.Rows.Add(i, "item" + i);

        }

        dt.AcceptChanges();

 

        DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();

        btnColumn.DataPropertyName = "id";

        btnColumn.Width = 100;

        btnColumn.ReadOnly = true;

        this.dataGridView1.Columns.Add(btnColumn);

        this.dataGridView1.DataSource = dt;

 

        foreach (DataGridViewRow dr in this.dataGridView1.Rows)

        {

          if (dr.Cells[0].Value != null)

          {

            int i;

            if ((int.TryParse(dr.Cells[0].Value.ToString(),out i)))

            {

              if (i % 2 == 0)

              {

                DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();

                dr.Cells[0] = txtcell;

              }

            }

          }

        }

      }

    }



try this also
shraddha bhosale replied to Sagar P on 02-Sep-10 03:17 AM
Sujt, u rocks!
your example working fine for me..
it's exactly what i need..

thanks