How to add a data grid view control in WinForm?

To add a DataGridView control in WinForm, you can follow these steps:

  1. Open Visual Studio and create a new WinForm application project.
  2. Drag a DataGridView control onto the form or manually create a DataGridView control in the code.
DataGridView dataGridView1 = new DataGridView();
  1. Set some basic properties of the DataGridView control, such as position, size, headers, etc.
dataGridView1.Location = new Point(10, 10);
dataGridView1.Size = new Size(300, 200);
dataGridView1.Name = "dataGridView1";
dataGridView1.ColumnHeadersVisible = true;
  1. You can add columns and data to the DataGridView control by dynamically adding them through code or by binding a data source.
// 添加列
dataGridView1.Columns.Add("Column1", "Column1");
dataGridView1.Columns.Add("Column2", "Column2");

// 添加数据
dataGridView1.Rows.Add("Data1", "Data2");
dataGridView1.Rows.Add("Data3", "Data4");

// 绑定数据源
// dataGridView1.DataSource = yourDataSource;
  1. Add the DataGridView control to the form.
this.Controls.Add(dataGridView1);

By following the steps above, you can successfully add a data grid view control in WinForm, set basic properties, add columns and data, and finally add the control to the form.

Leave a Reply 0

Your email address will not be published. Required fields are marked *