right aligned button on custom DataGridViewButtonCell
up vote
1
down vote
favorite
I want to create a custom DataGridViewCell
that looks like this example
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
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
add a comment |
up vote
1
down vote
favorite
I want to create a custom DataGridViewCell
that looks like this example
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
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to create a custom DataGridViewCell
that looks like this example
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
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
I want to create a custom DataGridViewCell
that looks like this example
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
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
c# winforms
asked Nov 19 at 12:39
MHComputech
6113
6113
add a comment |
add a comment |
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.
thanks a lot for your answer. Would you mind providing a full answer for theDataGridViewTextBoxCell
solution? That would be really awesome. Currently I just know theDataGridViewButtonCell
solution and this is really new to me
– MHComputech
Nov 19 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 at 7:38
add a comment |
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.
thanks a lot for your answer. Would you mind providing a full answer for theDataGridViewTextBoxCell
solution? That would be really awesome. Currently I just know theDataGridViewButtonCell
solution and this is really new to me
– MHComputech
Nov 19 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 at 7:38
add a comment |
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.
thanks a lot for your answer. Would you mind providing a full answer for theDataGridViewTextBoxCell
solution? That would be really awesome. Currently I just know theDataGridViewButtonCell
solution and this is really new to me
– MHComputech
Nov 19 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 at 7:38
add a comment |
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.
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.
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 theDataGridViewTextBoxCell
solution? That would be really awesome. Currently I just know theDataGridViewButtonCell
solution and this is really new to me
– MHComputech
Nov 19 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 at 7:38
add a comment |
thanks a lot for your answer. Would you mind providing a full answer for theDataGridViewTextBoxCell
solution? That would be really awesome. Currently I just know theDataGridViewButtonCell
solution and this is really new to me
– MHComputech
Nov 19 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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