mark
mark

Reputation: 619

how to create dynamically adjust width of column of dynamically created datagridview in c# windows application

I have dynamically created Datagrid , however I need to adjust width of columns, like I have Itemname its width should be 150 quantity should be 50 and so on. Heres my code for dynamic datagrid :

           dtitem = loadbl.itemonkot(dt.Rows[0][2].ToString());
            DataGrid dgv = new DataGrid();
            dgv.Location = new Point(3, 48);
            dgv.Width = 302;
            dgv.Height = 223;
            dgv.RowHeadersVisible = false;
            dgv.Font = new System.Drawing.Font("Microsoft Sans Serif", 10);
            dgv.DataSource = dtitem;
            grpbx.Controls.Add(dgv);
            grpbx.Name = "order";
            grpbx.Text = "Order";
            grpbx.Width = 311;
            grpbx.Height = 322;
            grpbx.Location = new Point(12, 12);

I tried google it said Here :

DataGridViewColumn column = dataGridView.Columns[0];
column.Width = 60;

How do I set to my datagrid ie; dgv

Please help, Thanks

Upvotes: 2

Views: 4182

Answers (2)

jaredbaszler
jaredbaszler

Reputation: 4829

An alternative is to auto size the columns after you populate the data. With:

dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

Upvotes: 1

Jeremy Thompson
Jeremy Thompson

Reputation: 65702

Have you tried something like:

DataGridView dgv = new DataGridView();
...
dgv.Columns[0].Width = 150;

Upvotes: 1

Related Questions