Rob GridView (-), DataGridView ( winforms).
winforms.
var persons = new[] {new {name = "aaa", salary = 40000},
new {name = "aaa", salary = 40000},
new {name = "aaa", salary = 40000},
new {name = "aaa", salary = 40000}};
DataGridView1.AutoGenerateColumns = false;
var NameField = new DataGridTextBoxColumn();
NameField.HeaderText = "Name";
NameField.DataPropertyName = "name";
DataGridView1.Columns.Add(NameField);
var SalaryField = new DataGridViewTextBoxColumn();
SalaryField.HeaderText = "Salary";
SalaryField.DataPropertyName = "salary";
SalaryField.DefaultCellStyle.Format = "{0:c2}";
DataGridView1.Columns.Add(SalaryField);
DataGridView1.DataSource = persons;
:
- The DataSource is installed at the end of the column definitions - this is because the DataGridView automatically binds the data when it is installed.
- Columns are set to DataGridViewTextBoxColumns. This is the standard way to display textual information in a DataGridView. If you use a DataGridViewColumn then it will not know how to display the data.
source
share