Mobin
Mobin

Reputation: 4920

How to dynamically create columns in a datagridview and assign titles to it and its rows?

I am using a datagridview for which I am not using any datasource. I want to dynamically allocate values to it. Create my own selected number of columns and rows and name them. Plus I want to add images to cells instead of data .

As for changing columns text we can use

grid.Columns[0].HeaderText = "First Column";

How to change use it for labeling rows?

Upvotes: 5

Views: 12392

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062865

Set the HeaderCell.Value for the row:

    DataGridView dgv = new DataGridView();
    dgv.Columns.Add("Foo", "Foo Text");
    dgv.Rows.Add();
    dgv.Rows[0].HeaderCell.Value = "Row Text";

    Form form = new Form();
    form.Controls.Add(dgv);
    Application.Run(form);

Upvotes: 4

Related Questions