right aligned button on custom DataGridViewButtonCell
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 |
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 |
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 '18 at 12:39
MHComputechMHComputech
26413
26413
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 '18 at 7:38
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 '18 at 7:38
add a comment |
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 '18 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 '18 at 7:38
add a comment |
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 '18 at 13:46
bokibegbokibeg
1,403815
1,403815
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 '18 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 '18 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 '18 at 14:01
I just replaced the22
withrectangle.Height
because it returns me the value21
I think this fits better
– MHComputech
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 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.
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