DataGridView là một điều khiển mạnh mẽ dùng để hiển thị dữ liệu.
Ví dụ ta cần hiển thị dữ liệu lên Datagridview như hình dưới, theo dòng và theo cột.
Ta đưa lên form một Datagridview có tên là Datagridview1, đưa code sau vào sự kiện form_load()
dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "Product ID"; dataGridView1.Columns[1].Name = "Product Name"; dataGridView1.Columns[2].Name = "Product Price"; string[] row = new string[] { "1", "Product 1", "1000" }; dataGridView1.Rows.Add(row); row = new string[] { "2", "Product 2", "2000" }; dataGridView1.Rows.Add(row); row = new string[] { "3", "Product 3", "3000" }; dataGridView1.Rows.Add(row); row = new string[] { "4", "Product 4", "4000" }; dataGridView1.Rows.Add(row);