Define operators on DataGridViewCell












0















I need to define some custom operators on DataGridView cells, and try to follow the OOP perspective.



I derived a new class from the standard DataGridViewTextBoxCell (call it DataGridViewTextBoxCell_1 for the sake of this question) as follows



class DataGridViewTextBoxCell_1:DataGridViewTextBoxCell
{
public static bool operator >=(DataGridViewTextBoxCell_1 a, DataGridViewTextBoxCell_1 b)
{ // do something }
}


Thus >= is the new operator defined on text box cells.
Now, when I create a datagridview control named Datagrid_1 and add new cells with cell template set us DataGridViewTextBoxCell_1, namely



 this.Datagrid_1.Columns[this.Datagrid_1.Columns.Count - 1].CellTemplate = new DataGridViewTextBoxCell_1();


I cannot use the above defined operator >= on cells of DataGrid_1, although their template supports the operatons ( I'm getting an error that Operator >= cannot be applied to operands of type 'DataGridViewCell' ).



I assume that I can use casting, but for extensive use that might be inefficient.



On the other hand, I'm not sure how to extend the DataGridViewCell class ( to which the error refers to) and make the DataGridView Control to use that new extended Cell class, as the relation between DataGridViewCell and DataGridView classes is not entirely clear.



I'd appreciate any suggestions on this matter.










share|improve this question























  • It is unclear what you are trying to achieve. However, I am guessing that since the operator “>=” method is returning a bool … that would indicate that the “grid cell” is of type bool. Which translates to a simple DataGridViewCheckBoxCell. It is unclear why there is a need for a “custom” “Cell type”?

    – JohnG
    Nov 27 '18 at 2:28











  • In addition, it appears the code is attempting to “compare” two cells in the grid and place the result of that comparison in another cell. There are many ways to achieve this without having to create “custom” cells for the grid. I recommend looking at a DataTable which would achieve what you describe easily without having to create any custom cells.

    – JohnG
    Nov 27 '18 at 2:28











  • @JohnG, the operator >= indeed returns a bool, however there is no reason for the grid cell to be a bool (it can very well contain datetime, or a numeric data). As for your second comment on putting the comparison result in the other cell, the code doesn't want to do that, and I'm not sure which part makes you think that. All I'm looking for is to have a way to compare values of different cells in a neat way ( >= being a single example) . I can feed the cell values to functions of course, but doing it in OOP fashion will make the overall code cleaner.

    – Hayk
    Nov 27 '18 at 4:55













  • Creating a method to compare two cells in a grid is trivial. As you state the comparison could return any value, however since the “operator” method IS returning “some” value, I assumed the result would be stored somewhere. It is unclear how the “custom” cell is used. In other words, the posted code “extends” from the DataGridViewTextBoxCell … but why? What is this “extended” cell going to contain?

    – JohnG
    Nov 27 '18 at 5:47











  • @JohnG, it is clearly stated in the 1st line of my comment above that the operator >= returns bool, not any value. My question is not about creating a method to compare 2 cells in the grid. Suppose I loop over the rows of the grid, and apply the comparison operators on the cells (or whatever the operator at the moment is). If the cells are a and b I can have a method F doing the job of the operator >= with the call F(a,b), however, if the grid supports >= directly, I can just use a>=b which is syntactically nicer ( there can be many more examples). This is the point of extension.

    – Hayk
    Nov 27 '18 at 5:58
















0















I need to define some custom operators on DataGridView cells, and try to follow the OOP perspective.



I derived a new class from the standard DataGridViewTextBoxCell (call it DataGridViewTextBoxCell_1 for the sake of this question) as follows



class DataGridViewTextBoxCell_1:DataGridViewTextBoxCell
{
public static bool operator >=(DataGridViewTextBoxCell_1 a, DataGridViewTextBoxCell_1 b)
{ // do something }
}


Thus >= is the new operator defined on text box cells.
Now, when I create a datagridview control named Datagrid_1 and add new cells with cell template set us DataGridViewTextBoxCell_1, namely



 this.Datagrid_1.Columns[this.Datagrid_1.Columns.Count - 1].CellTemplate = new DataGridViewTextBoxCell_1();


I cannot use the above defined operator >= on cells of DataGrid_1, although their template supports the operatons ( I'm getting an error that Operator >= cannot be applied to operands of type 'DataGridViewCell' ).



I assume that I can use casting, but for extensive use that might be inefficient.



On the other hand, I'm not sure how to extend the DataGridViewCell class ( to which the error refers to) and make the DataGridView Control to use that new extended Cell class, as the relation between DataGridViewCell and DataGridView classes is not entirely clear.



I'd appreciate any suggestions on this matter.










share|improve this question























  • It is unclear what you are trying to achieve. However, I am guessing that since the operator “>=” method is returning a bool … that would indicate that the “grid cell” is of type bool. Which translates to a simple DataGridViewCheckBoxCell. It is unclear why there is a need for a “custom” “Cell type”?

    – JohnG
    Nov 27 '18 at 2:28











  • In addition, it appears the code is attempting to “compare” two cells in the grid and place the result of that comparison in another cell. There are many ways to achieve this without having to create “custom” cells for the grid. I recommend looking at a DataTable which would achieve what you describe easily without having to create any custom cells.

    – JohnG
    Nov 27 '18 at 2:28











  • @JohnG, the operator >= indeed returns a bool, however there is no reason for the grid cell to be a bool (it can very well contain datetime, or a numeric data). As for your second comment on putting the comparison result in the other cell, the code doesn't want to do that, and I'm not sure which part makes you think that. All I'm looking for is to have a way to compare values of different cells in a neat way ( >= being a single example) . I can feed the cell values to functions of course, but doing it in OOP fashion will make the overall code cleaner.

    – Hayk
    Nov 27 '18 at 4:55













  • Creating a method to compare two cells in a grid is trivial. As you state the comparison could return any value, however since the “operator” method IS returning “some” value, I assumed the result would be stored somewhere. It is unclear how the “custom” cell is used. In other words, the posted code “extends” from the DataGridViewTextBoxCell … but why? What is this “extended” cell going to contain?

    – JohnG
    Nov 27 '18 at 5:47











  • @JohnG, it is clearly stated in the 1st line of my comment above that the operator >= returns bool, not any value. My question is not about creating a method to compare 2 cells in the grid. Suppose I loop over the rows of the grid, and apply the comparison operators on the cells (or whatever the operator at the moment is). If the cells are a and b I can have a method F doing the job of the operator >= with the call F(a,b), however, if the grid supports >= directly, I can just use a>=b which is syntactically nicer ( there can be many more examples). This is the point of extension.

    – Hayk
    Nov 27 '18 at 5:58














0












0








0








I need to define some custom operators on DataGridView cells, and try to follow the OOP perspective.



I derived a new class from the standard DataGridViewTextBoxCell (call it DataGridViewTextBoxCell_1 for the sake of this question) as follows



class DataGridViewTextBoxCell_1:DataGridViewTextBoxCell
{
public static bool operator >=(DataGridViewTextBoxCell_1 a, DataGridViewTextBoxCell_1 b)
{ // do something }
}


Thus >= is the new operator defined on text box cells.
Now, when I create a datagridview control named Datagrid_1 and add new cells with cell template set us DataGridViewTextBoxCell_1, namely



 this.Datagrid_1.Columns[this.Datagrid_1.Columns.Count - 1].CellTemplate = new DataGridViewTextBoxCell_1();


I cannot use the above defined operator >= on cells of DataGrid_1, although their template supports the operatons ( I'm getting an error that Operator >= cannot be applied to operands of type 'DataGridViewCell' ).



I assume that I can use casting, but for extensive use that might be inefficient.



On the other hand, I'm not sure how to extend the DataGridViewCell class ( to which the error refers to) and make the DataGridView Control to use that new extended Cell class, as the relation between DataGridViewCell and DataGridView classes is not entirely clear.



I'd appreciate any suggestions on this matter.










share|improve this question














I need to define some custom operators on DataGridView cells, and try to follow the OOP perspective.



I derived a new class from the standard DataGridViewTextBoxCell (call it DataGridViewTextBoxCell_1 for the sake of this question) as follows



class DataGridViewTextBoxCell_1:DataGridViewTextBoxCell
{
public static bool operator >=(DataGridViewTextBoxCell_1 a, DataGridViewTextBoxCell_1 b)
{ // do something }
}


Thus >= is the new operator defined on text box cells.
Now, when I create a datagridview control named Datagrid_1 and add new cells with cell template set us DataGridViewTextBoxCell_1, namely



 this.Datagrid_1.Columns[this.Datagrid_1.Columns.Count - 1].CellTemplate = new DataGridViewTextBoxCell_1();


I cannot use the above defined operator >= on cells of DataGrid_1, although their template supports the operatons ( I'm getting an error that Operator >= cannot be applied to operands of type 'DataGridViewCell' ).



I assume that I can use casting, but for extensive use that might be inefficient.



On the other hand, I'm not sure how to extend the DataGridViewCell class ( to which the error refers to) and make the DataGridView Control to use that new extended Cell class, as the relation between DataGridViewCell and DataGridView classes is not entirely clear.



I'd appreciate any suggestions on this matter.







c# oop datagridview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 19:59









HaykHayk

19319




19319













  • It is unclear what you are trying to achieve. However, I am guessing that since the operator “>=” method is returning a bool … that would indicate that the “grid cell” is of type bool. Which translates to a simple DataGridViewCheckBoxCell. It is unclear why there is a need for a “custom” “Cell type”?

    – JohnG
    Nov 27 '18 at 2:28











  • In addition, it appears the code is attempting to “compare” two cells in the grid and place the result of that comparison in another cell. There are many ways to achieve this without having to create “custom” cells for the grid. I recommend looking at a DataTable which would achieve what you describe easily without having to create any custom cells.

    – JohnG
    Nov 27 '18 at 2:28











  • @JohnG, the operator >= indeed returns a bool, however there is no reason for the grid cell to be a bool (it can very well contain datetime, or a numeric data). As for your second comment on putting the comparison result in the other cell, the code doesn't want to do that, and I'm not sure which part makes you think that. All I'm looking for is to have a way to compare values of different cells in a neat way ( >= being a single example) . I can feed the cell values to functions of course, but doing it in OOP fashion will make the overall code cleaner.

    – Hayk
    Nov 27 '18 at 4:55













  • Creating a method to compare two cells in a grid is trivial. As you state the comparison could return any value, however since the “operator” method IS returning “some” value, I assumed the result would be stored somewhere. It is unclear how the “custom” cell is used. In other words, the posted code “extends” from the DataGridViewTextBoxCell … but why? What is this “extended” cell going to contain?

    – JohnG
    Nov 27 '18 at 5:47











  • @JohnG, it is clearly stated in the 1st line of my comment above that the operator >= returns bool, not any value. My question is not about creating a method to compare 2 cells in the grid. Suppose I loop over the rows of the grid, and apply the comparison operators on the cells (or whatever the operator at the moment is). If the cells are a and b I can have a method F doing the job of the operator >= with the call F(a,b), however, if the grid supports >= directly, I can just use a>=b which is syntactically nicer ( there can be many more examples). This is the point of extension.

    – Hayk
    Nov 27 '18 at 5:58



















  • It is unclear what you are trying to achieve. However, I am guessing that since the operator “>=” method is returning a bool … that would indicate that the “grid cell” is of type bool. Which translates to a simple DataGridViewCheckBoxCell. It is unclear why there is a need for a “custom” “Cell type”?

    – JohnG
    Nov 27 '18 at 2:28











  • In addition, it appears the code is attempting to “compare” two cells in the grid and place the result of that comparison in another cell. There are many ways to achieve this without having to create “custom” cells for the grid. I recommend looking at a DataTable which would achieve what you describe easily without having to create any custom cells.

    – JohnG
    Nov 27 '18 at 2:28











  • @JohnG, the operator >= indeed returns a bool, however there is no reason for the grid cell to be a bool (it can very well contain datetime, or a numeric data). As for your second comment on putting the comparison result in the other cell, the code doesn't want to do that, and I'm not sure which part makes you think that. All I'm looking for is to have a way to compare values of different cells in a neat way ( >= being a single example) . I can feed the cell values to functions of course, but doing it in OOP fashion will make the overall code cleaner.

    – Hayk
    Nov 27 '18 at 4:55













  • Creating a method to compare two cells in a grid is trivial. As you state the comparison could return any value, however since the “operator” method IS returning “some” value, I assumed the result would be stored somewhere. It is unclear how the “custom” cell is used. In other words, the posted code “extends” from the DataGridViewTextBoxCell … but why? What is this “extended” cell going to contain?

    – JohnG
    Nov 27 '18 at 5:47











  • @JohnG, it is clearly stated in the 1st line of my comment above that the operator >= returns bool, not any value. My question is not about creating a method to compare 2 cells in the grid. Suppose I loop over the rows of the grid, and apply the comparison operators on the cells (or whatever the operator at the moment is). If the cells are a and b I can have a method F doing the job of the operator >= with the call F(a,b), however, if the grid supports >= directly, I can just use a>=b which is syntactically nicer ( there can be many more examples). This is the point of extension.

    – Hayk
    Nov 27 '18 at 5:58

















It is unclear what you are trying to achieve. However, I am guessing that since the operator “>=” method is returning a bool … that would indicate that the “grid cell” is of type bool. Which translates to a simple DataGridViewCheckBoxCell. It is unclear why there is a need for a “custom” “Cell type”?

– JohnG
Nov 27 '18 at 2:28





It is unclear what you are trying to achieve. However, I am guessing that since the operator “>=” method is returning a bool … that would indicate that the “grid cell” is of type bool. Which translates to a simple DataGridViewCheckBoxCell. It is unclear why there is a need for a “custom” “Cell type”?

– JohnG
Nov 27 '18 at 2:28













In addition, it appears the code is attempting to “compare” two cells in the grid and place the result of that comparison in another cell. There are many ways to achieve this without having to create “custom” cells for the grid. I recommend looking at a DataTable which would achieve what you describe easily without having to create any custom cells.

– JohnG
Nov 27 '18 at 2:28





In addition, it appears the code is attempting to “compare” two cells in the grid and place the result of that comparison in another cell. There are many ways to achieve this without having to create “custom” cells for the grid. I recommend looking at a DataTable which would achieve what you describe easily without having to create any custom cells.

– JohnG
Nov 27 '18 at 2:28













@JohnG, the operator >= indeed returns a bool, however there is no reason for the grid cell to be a bool (it can very well contain datetime, or a numeric data). As for your second comment on putting the comparison result in the other cell, the code doesn't want to do that, and I'm not sure which part makes you think that. All I'm looking for is to have a way to compare values of different cells in a neat way ( >= being a single example) . I can feed the cell values to functions of course, but doing it in OOP fashion will make the overall code cleaner.

– Hayk
Nov 27 '18 at 4:55







@JohnG, the operator >= indeed returns a bool, however there is no reason for the grid cell to be a bool (it can very well contain datetime, or a numeric data). As for your second comment on putting the comparison result in the other cell, the code doesn't want to do that, and I'm not sure which part makes you think that. All I'm looking for is to have a way to compare values of different cells in a neat way ( >= being a single example) . I can feed the cell values to functions of course, but doing it in OOP fashion will make the overall code cleaner.

– Hayk
Nov 27 '18 at 4:55















Creating a method to compare two cells in a grid is trivial. As you state the comparison could return any value, however since the “operator” method IS returning “some” value, I assumed the result would be stored somewhere. It is unclear how the “custom” cell is used. In other words, the posted code “extends” from the DataGridViewTextBoxCell … but why? What is this “extended” cell going to contain?

– JohnG
Nov 27 '18 at 5:47





Creating a method to compare two cells in a grid is trivial. As you state the comparison could return any value, however since the “operator” method IS returning “some” value, I assumed the result would be stored somewhere. It is unclear how the “custom” cell is used. In other words, the posted code “extends” from the DataGridViewTextBoxCell … but why? What is this “extended” cell going to contain?

– JohnG
Nov 27 '18 at 5:47













@JohnG, it is clearly stated in the 1st line of my comment above that the operator >= returns bool, not any value. My question is not about creating a method to compare 2 cells in the grid. Suppose I loop over the rows of the grid, and apply the comparison operators on the cells (or whatever the operator at the moment is). If the cells are a and b I can have a method F doing the job of the operator >= with the call F(a,b), however, if the grid supports >= directly, I can just use a>=b which is syntactically nicer ( there can be many more examples). This is the point of extension.

– Hayk
Nov 27 '18 at 5:58





@JohnG, it is clearly stated in the 1st line of my comment above that the operator >= returns bool, not any value. My question is not about creating a method to compare 2 cells in the grid. Suppose I loop over the rows of the grid, and apply the comparison operators on the cells (or whatever the operator at the moment is). If the cells are a and b I can have a method F doing the job of the operator >= with the call F(a,b), however, if the grid supports >= directly, I can just use a>=b which is syntactically nicer ( there can be many more examples). This is the point of extension.

– Hayk
Nov 27 '18 at 5:58












0






active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488154%2fdefine-operators-on-datagridviewcell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488154%2fdefine-operators-on-datagridviewcell%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)