How to assign values to some data grid view column when dgv is binded












0















I have a data grid view which is binded to a list. List is an object list



List<User> userList = new List<User>();

User user = new User(){
Name = "Diana",
Age = "37"
}

userList.Add(user);

myDataGridView.DataSource = null;

myDataGridView.DataSource = userList();
myDataGridView.Columns["Name"].DataPropertyName = "Name";
myDataGridView.Columns["Age"].DataPropertyName = "Age";


But i have one more column Which is "Permission"



I want to check if a user is age > 18 then permission columns value for related user's row will be a string "Valid"
There is no permission property in my user class and I don't want to add it. How can I do this?










share|improve this question























  • You could try using an expression to calculate the value of "Permission", theres several examples on SO including this: stackoverflow.com/questions/6097370/…

    – Wayne Phipps
    Nov 25 '18 at 18:07











  • or another here: stackoverflow.com/questions/1257674/…

    – Wayne Phipps
    Nov 25 '18 at 18:08
















0















I have a data grid view which is binded to a list. List is an object list



List<User> userList = new List<User>();

User user = new User(){
Name = "Diana",
Age = "37"
}

userList.Add(user);

myDataGridView.DataSource = null;

myDataGridView.DataSource = userList();
myDataGridView.Columns["Name"].DataPropertyName = "Name";
myDataGridView.Columns["Age"].DataPropertyName = "Age";


But i have one more column Which is "Permission"



I want to check if a user is age > 18 then permission columns value for related user's row will be a string "Valid"
There is no permission property in my user class and I don't want to add it. How can I do this?










share|improve this question























  • You could try using an expression to calculate the value of "Permission", theres several examples on SO including this: stackoverflow.com/questions/6097370/…

    – Wayne Phipps
    Nov 25 '18 at 18:07











  • or another here: stackoverflow.com/questions/1257674/…

    – Wayne Phipps
    Nov 25 '18 at 18:08














0












0








0








I have a data grid view which is binded to a list. List is an object list



List<User> userList = new List<User>();

User user = new User(){
Name = "Diana",
Age = "37"
}

userList.Add(user);

myDataGridView.DataSource = null;

myDataGridView.DataSource = userList();
myDataGridView.Columns["Name"].DataPropertyName = "Name";
myDataGridView.Columns["Age"].DataPropertyName = "Age";


But i have one more column Which is "Permission"



I want to check if a user is age > 18 then permission columns value for related user's row will be a string "Valid"
There is no permission property in my user class and I don't want to add it. How can I do this?










share|improve this question














I have a data grid view which is binded to a list. List is an object list



List<User> userList = new List<User>();

User user = new User(){
Name = "Diana",
Age = "37"
}

userList.Add(user);

myDataGridView.DataSource = null;

myDataGridView.DataSource = userList();
myDataGridView.Columns["Name"].DataPropertyName = "Name";
myDataGridView.Columns["Age"].DataPropertyName = "Age";


But i have one more column Which is "Permission"



I want to check if a user is age > 18 then permission columns value for related user's row will be a string "Valid"
There is no permission property in my user class and I don't want to add it. How can I do this?







c# datagridview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 17:46









mobumobu

41




41













  • You could try using an expression to calculate the value of "Permission", theres several examples on SO including this: stackoverflow.com/questions/6097370/…

    – Wayne Phipps
    Nov 25 '18 at 18:07











  • or another here: stackoverflow.com/questions/1257674/…

    – Wayne Phipps
    Nov 25 '18 at 18:08



















  • You could try using an expression to calculate the value of "Permission", theres several examples on SO including this: stackoverflow.com/questions/6097370/…

    – Wayne Phipps
    Nov 25 '18 at 18:07











  • or another here: stackoverflow.com/questions/1257674/…

    – Wayne Phipps
    Nov 25 '18 at 18:08

















You could try using an expression to calculate the value of "Permission", theres several examples on SO including this: stackoverflow.com/questions/6097370/…

– Wayne Phipps
Nov 25 '18 at 18:07





You could try using an expression to calculate the value of "Permission", theres several examples on SO including this: stackoverflow.com/questions/6097370/…

– Wayne Phipps
Nov 25 '18 at 18:07













or another here: stackoverflow.com/questions/1257674/…

– Wayne Phipps
Nov 25 '18 at 18:08





or another here: stackoverflow.com/questions/1257674/…

– Wayne Phipps
Nov 25 '18 at 18:08












1 Answer
1






active

oldest

votes


















0














What you are describing is doable, however there are some drawbacks to doing it the way you describe. First off, introducing a new column to the grid that “IS NOT” part of the data source will require the code to “manage” that column. Meaning that if the grid is sorted/filtered etc.… your code is going to have to go through the grid and manage the “age > 18” column. I am guessing you may be looking for something that will do this automatically.



I am curious as to why you need this column since the “age” column is already present and I would not think that the user would be challenged by “visually seeing” the age column and determining if the value is over 18. Possibly coloring the cell for values less than 18 instead of “adding” a new column. Just a thought.



In addition, I am not sure “why” you do not want to add the “permission” property to the class. It is a “computed” value from the existing data, therefore, there is no need to actually keep the value in the original data source. What happens if you add the following code to the User class? I am betting it will display the “permission” value as you describe.



public string Permission {
get {
if (Age > 18)
return "Valid";
else
return "Invalid";
}
}


This is one approach, however, using a List<T> as a DataSource for the grid has some drawbacks. Namely, the user will not be able to “add” new rows to the grid without more coding on your part. That is why I recommend you take a look at a BindingSource or BindingList<User> as an alternative to the List<User> … these Binding controls will make it easier to handle the adding of new rows, filtering, sorting, etc.






share|improve this answer

























    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%2f53470200%2fhow-to-assign-values-to-some-data-grid-view-column-when-dgv-is-binded%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









    0














    What you are describing is doable, however there are some drawbacks to doing it the way you describe. First off, introducing a new column to the grid that “IS NOT” part of the data source will require the code to “manage” that column. Meaning that if the grid is sorted/filtered etc.… your code is going to have to go through the grid and manage the “age > 18” column. I am guessing you may be looking for something that will do this automatically.



    I am curious as to why you need this column since the “age” column is already present and I would not think that the user would be challenged by “visually seeing” the age column and determining if the value is over 18. Possibly coloring the cell for values less than 18 instead of “adding” a new column. Just a thought.



    In addition, I am not sure “why” you do not want to add the “permission” property to the class. It is a “computed” value from the existing data, therefore, there is no need to actually keep the value in the original data source. What happens if you add the following code to the User class? I am betting it will display the “permission” value as you describe.



    public string Permission {
    get {
    if (Age > 18)
    return "Valid";
    else
    return "Invalid";
    }
    }


    This is one approach, however, using a List<T> as a DataSource for the grid has some drawbacks. Namely, the user will not be able to “add” new rows to the grid without more coding on your part. That is why I recommend you take a look at a BindingSource or BindingList<User> as an alternative to the List<User> … these Binding controls will make it easier to handle the adding of new rows, filtering, sorting, etc.






    share|improve this answer






























      0














      What you are describing is doable, however there are some drawbacks to doing it the way you describe. First off, introducing a new column to the grid that “IS NOT” part of the data source will require the code to “manage” that column. Meaning that if the grid is sorted/filtered etc.… your code is going to have to go through the grid and manage the “age > 18” column. I am guessing you may be looking for something that will do this automatically.



      I am curious as to why you need this column since the “age” column is already present and I would not think that the user would be challenged by “visually seeing” the age column and determining if the value is over 18. Possibly coloring the cell for values less than 18 instead of “adding” a new column. Just a thought.



      In addition, I am not sure “why” you do not want to add the “permission” property to the class. It is a “computed” value from the existing data, therefore, there is no need to actually keep the value in the original data source. What happens if you add the following code to the User class? I am betting it will display the “permission” value as you describe.



      public string Permission {
      get {
      if (Age > 18)
      return "Valid";
      else
      return "Invalid";
      }
      }


      This is one approach, however, using a List<T> as a DataSource for the grid has some drawbacks. Namely, the user will not be able to “add” new rows to the grid without more coding on your part. That is why I recommend you take a look at a BindingSource or BindingList<User> as an alternative to the List<User> … these Binding controls will make it easier to handle the adding of new rows, filtering, sorting, etc.






      share|improve this answer




























        0












        0








        0







        What you are describing is doable, however there are some drawbacks to doing it the way you describe. First off, introducing a new column to the grid that “IS NOT” part of the data source will require the code to “manage” that column. Meaning that if the grid is sorted/filtered etc.… your code is going to have to go through the grid and manage the “age > 18” column. I am guessing you may be looking for something that will do this automatically.



        I am curious as to why you need this column since the “age” column is already present and I would not think that the user would be challenged by “visually seeing” the age column and determining if the value is over 18. Possibly coloring the cell for values less than 18 instead of “adding” a new column. Just a thought.



        In addition, I am not sure “why” you do not want to add the “permission” property to the class. It is a “computed” value from the existing data, therefore, there is no need to actually keep the value in the original data source. What happens if you add the following code to the User class? I am betting it will display the “permission” value as you describe.



        public string Permission {
        get {
        if (Age > 18)
        return "Valid";
        else
        return "Invalid";
        }
        }


        This is one approach, however, using a List<T> as a DataSource for the grid has some drawbacks. Namely, the user will not be able to “add” new rows to the grid without more coding on your part. That is why I recommend you take a look at a BindingSource or BindingList<User> as an alternative to the List<User> … these Binding controls will make it easier to handle the adding of new rows, filtering, sorting, etc.






        share|improve this answer















        What you are describing is doable, however there are some drawbacks to doing it the way you describe. First off, introducing a new column to the grid that “IS NOT” part of the data source will require the code to “manage” that column. Meaning that if the grid is sorted/filtered etc.… your code is going to have to go through the grid and manage the “age > 18” column. I am guessing you may be looking for something that will do this automatically.



        I am curious as to why you need this column since the “age” column is already present and I would not think that the user would be challenged by “visually seeing” the age column and determining if the value is over 18. Possibly coloring the cell for values less than 18 instead of “adding” a new column. Just a thought.



        In addition, I am not sure “why” you do not want to add the “permission” property to the class. It is a “computed” value from the existing data, therefore, there is no need to actually keep the value in the original data source. What happens if you add the following code to the User class? I am betting it will display the “permission” value as you describe.



        public string Permission {
        get {
        if (Age > 18)
        return "Valid";
        else
        return "Invalid";
        }
        }


        This is one approach, however, using a List<T> as a DataSource for the grid has some drawbacks. Namely, the user will not be able to “add” new rows to the grid without more coding on your part. That is why I recommend you take a look at a BindingSource or BindingList<User> as an alternative to the List<User> … these Binding controls will make it easier to handle the adding of new rows, filtering, sorting, etc.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 22:06

























        answered Nov 25 '18 at 21:56









        JohnGJohnG

        3,35221021




        3,35221021






























            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%2f53470200%2fhow-to-assign-values-to-some-data-grid-view-column-when-dgv-is-binded%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)