right aligned button on custom DataGridViewButtonCell











up vote
1
down vote

favorite












I want to create a custom DataGridViewCell that looks like this example



enter image description here



I started creating this cell. At first I inherit from a DataGridViewButtonCell and override the important methods.



    private class DataGridViewAllocationCell : DataGridViewButtonCell
{
public void Initialize() // Pseudo Constructor with some arguments
{
contextMenu = new ContextMenuStrip();
// fill the contextMenu here
}

private ContextMenuStrip contextMenu;

private const string BUTTON_TEXT = "...";

private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }

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)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
Rectangle cellRectangle = GetContentBounds(rowIndex);
Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
cellRectangle.Offset(displayRectangle.Location);
base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
}

protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
{
Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);
}

protected override void OnContentClick(DataGridViewCellEventArgs e)
{
base.OnContentClick(e);
Rectangle contentRectangle = GetContentBounds(e.RowIndex);
Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
contextMenu.Show(DataGridView, location);
}
}


When creating a column with these cells I get this grid



enter image description here



The important part is the second column. The button control is filling the rest of the cell.



Is there a way making the button as big as its text (default width) and align it on the right side?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I want to create a custom DataGridViewCell that looks like this example



    enter image description here



    I started creating this cell. At first I inherit from a DataGridViewButtonCell and override the important methods.



        private class DataGridViewAllocationCell : DataGridViewButtonCell
    {
    public void Initialize() // Pseudo Constructor with some arguments
    {
    contextMenu = new ContextMenuStrip();
    // fill the contextMenu here
    }

    private ContextMenuStrip contextMenu;

    private const string BUTTON_TEXT = "...";

    private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
    private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }

    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)
    {
    base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
    Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
    Rectangle cellRectangle = GetContentBounds(rowIndex);
    Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
    cellRectangle.Offset(displayRectangle.Location);
    base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
    TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
    }

    protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
    {
    Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
    return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);
    }

    protected override void OnContentClick(DataGridViewCellEventArgs e)
    {
    base.OnContentClick(e);
    Rectangle contentRectangle = GetContentBounds(e.RowIndex);
    Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
    Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
    contextMenu.Show(DataGridView, location);
    }
    }


    When creating a column with these cells I get this grid



    enter image description here



    The important part is the second column. The button control is filling the rest of the cell.



    Is there a way making the button as big as its text (default width) and align it on the right side?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to create a custom DataGridViewCell that looks like this example



      enter image description here



      I started creating this cell. At first I inherit from a DataGridViewButtonCell and override the important methods.



          private class DataGridViewAllocationCell : DataGridViewButtonCell
      {
      public void Initialize() // Pseudo Constructor with some arguments
      {
      contextMenu = new ContextMenuStrip();
      // fill the contextMenu here
      }

      private ContextMenuStrip contextMenu;

      private const string BUTTON_TEXT = "...";

      private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
      private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }

      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)
      {
      base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
      Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
      Rectangle cellRectangle = GetContentBounds(rowIndex);
      Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
      cellRectangle.Offset(displayRectangle.Location);
      base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
      TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
      }

      protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
      {
      Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
      return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);
      }

      protected override void OnContentClick(DataGridViewCellEventArgs e)
      {
      base.OnContentClick(e);
      Rectangle contentRectangle = GetContentBounds(e.RowIndex);
      Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
      Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
      contextMenu.Show(DataGridView, location);
      }
      }


      When creating a column with these cells I get this grid



      enter image description here



      The important part is the second column. The button control is filling the rest of the cell.



      Is there a way making the button as big as its text (default width) and align it on the right side?










      share|improve this question













      I want to create a custom DataGridViewCell that looks like this example



      enter image description here



      I started creating this cell. At first I inherit from a DataGridViewButtonCell and override the important methods.



          private class DataGridViewAllocationCell : DataGridViewButtonCell
      {
      public void Initialize() // Pseudo Constructor with some arguments
      {
      contextMenu = new ContextMenuStrip();
      // fill the contextMenu here
      }

      private ContextMenuStrip contextMenu;

      private const string BUTTON_TEXT = "...";

      private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
      private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }

      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)
      {
      base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
      Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
      Rectangle cellRectangle = GetContentBounds(rowIndex);
      Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
      cellRectangle.Offset(displayRectangle.Location);
      base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
      TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
      }

      protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
      {
      Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
      return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);
      }

      protected override void OnContentClick(DataGridViewCellEventArgs e)
      {
      base.OnContentClick(e);
      Rectangle contentRectangle = GetContentBounds(e.RowIndex);
      Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
      Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
      contextMenu.Show(DataGridView, location);
      }
      }


      When creating a column with these cells I get this grid



      enter image description here



      The important part is the second column. The button control is filling the rest of the cell.



      Is there a way making the button as big as its text (default width) and align it on the right side?







      c# winforms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 12:39









      MHComputech

      6113




      6113
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          I would advise you go with the approach of overriding DataGridViewTextBoxCell and inserting a button when it's focused rather than overriding DataGridViewButtonCell and drawing the text part.



          Alas, what you did there is still valid and if you want the button to align differently you just have to specify different content bounds, i.e. instead of:



          return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);


          You may put something like this, a right-aligned button 22 pixels wide:



          return new Rectangle(rectangle.Width - 22, rectangle.Top, 22, rectangle.Height);


          And your button will behave like a typical "..." button you'd expect. Anyways, therein lies the key to your problem.






          share|improve this answer





















          • thanks a lot for your answer. Would you mind providing a full answer for the DataGridViewTextBoxCell solution? That would be really awesome. Currently I just know the DataGridViewButtonCell solution and this is really new to me
            – MHComputech
            Nov 19 at 14:01










          • I just replaced the 22 with rectangle.Height because it returns me the value 21 I think this fits better
            – MHComputech
            Nov 20 at 7:38











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374863%2fright-aligned-button-on-custom-datagridviewbuttoncell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          I would advise you go with the approach of overriding DataGridViewTextBoxCell and inserting a button when it's focused rather than overriding DataGridViewButtonCell and drawing the text part.



          Alas, what you did there is still valid and if you want the button to align differently you just have to specify different content bounds, i.e. instead of:



          return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);


          You may put something like this, a right-aligned button 22 pixels wide:



          return new Rectangle(rectangle.Width - 22, rectangle.Top, 22, rectangle.Height);


          And your button will behave like a typical "..." button you'd expect. Anyways, therein lies the key to your problem.






          share|improve this answer





















          • thanks a lot for your answer. Would you mind providing a full answer for the DataGridViewTextBoxCell solution? That would be really awesome. Currently I just know the DataGridViewButtonCell solution and this is really new to me
            – MHComputech
            Nov 19 at 14:01










          • I just replaced the 22 with rectangle.Height because it returns me the value 21 I think this fits better
            – MHComputech
            Nov 20 at 7:38















          up vote
          1
          down vote













          I would advise you go with the approach of overriding DataGridViewTextBoxCell and inserting a button when it's focused rather than overriding DataGridViewButtonCell and drawing the text part.



          Alas, what you did there is still valid and if you want the button to align differently you just have to specify different content bounds, i.e. instead of:



          return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);


          You may put something like this, a right-aligned button 22 pixels wide:



          return new Rectangle(rectangle.Width - 22, rectangle.Top, 22, rectangle.Height);


          And your button will behave like a typical "..." button you'd expect. Anyways, therein lies the key to your problem.






          share|improve this answer





















          • thanks a lot for your answer. Would you mind providing a full answer for the DataGridViewTextBoxCell solution? That would be really awesome. Currently I just know the DataGridViewButtonCell solution and this is really new to me
            – MHComputech
            Nov 19 at 14:01










          • I just replaced the 22 with rectangle.Height because it returns me the value 21 I think this fits better
            – MHComputech
            Nov 20 at 7:38













          up vote
          1
          down vote










          up vote
          1
          down vote









          I would advise you go with the approach of overriding DataGridViewTextBoxCell and inserting a button when it's focused rather than overriding DataGridViewButtonCell and drawing the text part.



          Alas, what you did there is still valid and if you want the button to align differently you just have to specify different content bounds, i.e. instead of:



          return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);


          You may put something like this, a right-aligned button 22 pixels wide:



          return new Rectangle(rectangle.Width - 22, rectangle.Top, 22, rectangle.Height);


          And your button will behave like a typical "..." button you'd expect. Anyways, therein lies the key to your problem.






          share|improve this answer












          I would advise you go with the approach of overriding DataGridViewTextBoxCell and inserting a button when it's focused rather than overriding DataGridViewButtonCell and drawing the text part.



          Alas, what you did there is still valid and if you want the button to align differently you just have to specify different content bounds, i.e. instead of:



          return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);


          You may put something like this, a right-aligned button 22 pixels wide:



          return new Rectangle(rectangle.Width - 22, rectangle.Top, 22, rectangle.Height);


          And your button will behave like a typical "..." button you'd expect. Anyways, therein lies the key to your problem.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 13:46









          bokibeg

          1,393715




          1,393715












          • thanks a lot for your answer. Would you mind providing a full answer for the DataGridViewTextBoxCell solution? That would be really awesome. Currently I just know the DataGridViewButtonCell solution and this is really new to me
            – MHComputech
            Nov 19 at 14:01










          • I just replaced the 22 with rectangle.Height because it returns me the value 21 I think this fits better
            – MHComputech
            Nov 20 at 7:38


















          • thanks a lot for your answer. Would you mind providing a full answer for the DataGridViewTextBoxCell solution? That would be really awesome. Currently I just know the DataGridViewButtonCell solution and this is really new to me
            – MHComputech
            Nov 19 at 14:01










          • I just replaced the 22 with rectangle.Height because it returns me the value 21 I think this fits better
            – MHComputech
            Nov 20 at 7:38
















          thanks a lot for your answer. Would you mind providing a full answer for the DataGridViewTextBoxCell solution? That would be really awesome. Currently I just know the DataGridViewButtonCell solution and this is really new to me
          – MHComputech
          Nov 19 at 14:01




          thanks a lot for your answer. Would you mind providing a full answer for the DataGridViewTextBoxCell solution? That would be really awesome. Currently I just know the DataGridViewButtonCell solution and this is really new to me
          – MHComputech
          Nov 19 at 14:01












          I just replaced the 22 with rectangle.Height because it returns me the value 21 I think this fits better
          – MHComputech
          Nov 20 at 7:38




          I just replaced the 22 with rectangle.Height because it returns me the value 21 I think this fits better
          – MHComputech
          Nov 20 at 7:38


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374863%2fright-aligned-button-on-custom-datagridviewbuttoncell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]