DatagridView with variable number of columns

I want to create a datagridview in C# (winform) with a variable number of columns. I have done this

dataGridView2 = _dm.CreateGrid();

where CreateGrid() is the following method:

public DataGridView CreateGrid()
    {
        DataGridView dtgr = new DataGridView();

        using (DbEntities db = new DbEntities())
        {

            foreach (Resource r in db.Resources)
            {
                DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

                col.ValueType = typeof(string);
                col.Name = r.Description;
                col.HeaderText = r.Description;
                col.DataPropertyName = r.Description;
                dtgr.Columns.Add(col);
            }

        }

        return dtgr;
    }

Description is of type string. I dont see the datagridview in my winform. Where is the problem ?

View More Answer And Comment : Stackoverflow


Top Answer 1

You need add your

DataGridView

to controls of the

Form

dataGridView2 = _dm.CreateGrid();
this.Controls.Add(dataGridView2);

Why you create columns manually.

DataGridView

will generate columns automatically based on the columns/fields of the datasource, if you set

AutoGenerateColumns = true

From MSDN DataGridView.AutoGenerateColumns Property

Top Answer 2

You need add your

DataGridView

to controls of the

Form

dataGridView2 = _dm.CreateGrid();
this.Controls.Add(dataGridView2);

Why you create columns manually.

DataGridView

will generate columns automatically based on the columns/fields of the datasource, if you set

AutoGenerateColumns = true

From MSDN DataGridView.AutoGenerateColumns Property

View More Answer And Comment : Stackoverflow

Setting difficulty not working

I am making a simple java text game in eclipse luna. Scanner.next() is not working. When I run it, after I ask for the user input, an error displays (java.util.NoSuchElementException). Have I just [Read More...]

Cannot assign to let value action

I'm trying to define a constant outside a switch statement so that I can use it after the switch statement is done executing and assign it within the switch statement: let action: SKAction! switch [Read More...]

How to find entity in doctrine2

I have entity developer who has field role, $unsubscribeDate, $tags and I need find developer who: - have role freelancer, - unsubscribeDate < NowDate - tag not=: blacklist, unsubscribed (inDB [Read More...]

POST canvas to web api

I am using cloudsight api http://cloudsight.readme.io/v1.0/docs And string where i post data. Also i have canvas image from webcam. I am trying to send this to api, but is blank when iam serialize [Read More...]

Using ripper in a rails app?

I want to check the source of a file (maybe a view) if it contains logic, so I thought of using Ripper.lex but I'm not sure if it's possible to pass a file to .lex and it will read the code IN the [Read More...]

Type mismatch in key from map

I'm getting this error: java.lang.Exception: java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.Text, received org.apache.hadoop.io.LongWritable at [Read More...]