Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3391814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:50:48+00:00 2026-05-18T03:50:48+00:00

I work on C#. foreach (DataGridViewRow item in dataGridView1.Rows) { if (Convert.ToBoolean( item.Cells[0].Value) ==

  • 0

I work on C#.

foreach (DataGridViewRow item in dataGridView1.Rows)
{
    if (Convert.ToBoolean( item.Cells[0].Value) == true)
    {
        foreach (DataGridViewColumn col in dataGridView1.Columns)
        {
            **class oclass=new class();
            oclass.property = item.Cells[col.Name].Value.ToString();
            Collection.add(oclass);**
        }
    }

}

In the above syntax,i want to fill class property dynamically.I also add my class in collection.Is it possible in C#,fill class property dynamically?My grid contain more than one column.My grid’s column name are same as class property name.I want to do the bellow thing:

foreach (DataGridViewColumn col in dataGridView1.Columns)
{
    **class oclass=new class();
    oclass.col.Name= item.Cells[col.Name].Value.ToString();
    Collection.add(oclass);**
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:50:48+00:00Added an answer on May 18, 2026 at 3:50 am

    You can do this with PropertyInfo.SetValue().

    Within the example, I’ve added an explanation of what it all does.

    I’ve used the class TestClass for this example. First I get the type.

    var type = typeof(TestClass);
    

    Then, I build a dictionary of all column names and a PropertyInfo which is a reference to the property of the class with the same name as the column.

    var properties = new Dictionary<string, PropertyInfo>();
    
    foreach (DataGridViewColumn col in dataGridView1.Columns)
    {
        properties.Add(col.Name, type.GetProperty(col.Name));
    }
    

    Then, for each row.

    foreach (DataGridViewRow item in dataGridView1.Rows)
    {
        if (Convert.ToBoolean( item.Cells[0].Value) == true)
        {
    

    I create a new instance of TestClass.

            var instance = new TestClass();
    
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
    

    I take the property from the dictionary and use the method SetValue to set the value of the instance with the value from the column. But, to be able to do this, we must first ensure that the type is correct.

                object value = Convert.ChangeType(item.Cells[col.Name].Value, properties[col.Name].PropertyType);
    
                properties[col.Name].SetValue(instance, value, null);
            }
    

    And last, I add the instance to the collection.

            Collection.Add(instance);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How does foreach work when looping through function results? If I have
Work on asp.net vs05. I have three type of value Like:IsDesign,IsPrinting,IsInstall they are bit
I have some foreach, this could work well foreach ($umm as $data) { echo
i want to know how do we convert the following code to work with
I have the following smarty code: {foreach from=$additional_fields item=v} {if $v.fieldid eq 5} {php}
I'd love to know why this code puts the value 5 in the columns
So this doesn't work: foreach ($element->attributes as $attribute) { $element->removeAttribute($attribute->name); } If the node
I nominate me for village idiot. Why doesn't this work: foreach (XElement clientField in
For Each row As DataGridViewRow In DGV.Rows DGV.Rows.RemoveAt(CInt(row.Index.ToString)) Next The above code will remove
i use $myblogrepo = $this->_doctrine->getRepository('Entities\Blog')->findBy(array('id' => 12); i access via foreach($myblogrepo as $key =>$value){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.