开发者

Adding Class instance as a new Row in DataGridView (c#)

开发者 https://www.devze.com 2023-02-02 19:33 出处:网络
I have a class say [Serializable] public class Answer { [DisplayName(\"ID\")] public string ID { get; set; }

I have a class say

[Serializable]
    public class Answer
    {
        [DisplayName("ID")]
        public string ID { get; set; }
        [DisplayName("Value")]
        public string Value { get; set; }
    }

and I have a datagridview with bounded columns to 开发者_运维知识库the above class.

instances of this class Answer are created dynamically as and when required. How do I update datagridview when each and every instance of class is created.

is it possible to do something of this sort.

dataGridView.Rows.Add(classInstance);

Thanks in Advance,

Amit


Yes, but not the way you do it.

You need to use databinding.

Something like the following should work:

var ds = new BindingList<Answer>();
dgv.DataSource = ds;

Now when you do:

ds.Add( new Answer { ... });

it will be added (as you wanted).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号