DataGridView In Form Designer
-
Hi ! I have created a form designer from different examples I found here or elsewhere. But I have a trouble when I try to edit a DataGridView: "Edit columns" and "Add columns" automatically appear in my PropertyGrid, and I can click to show the "classic" VS Datagridview column editor. The trouble is that when I "Add" a column, no type is available, so when I click "Add", a exception is launched... As this column editor is launched "automatically", how can I extend it to add types in the Add window ? Looking forward to hearing from someone ;-) Best regards junxian
-
Hi ! I have created a form designer from different examples I found here or elsewhere. But I have a trouble when I try to edit a DataGridView: "Edit columns" and "Add columns" automatically appear in my PropertyGrid, and I can click to show the "classic" VS Datagridview column editor. The trouble is that when I "Add" a column, no type is available, so when I click "Add", a exception is launched... As this column editor is launched "automatically", how can I extend it to add types in the Add window ? Looking forward to hearing from someone ;-) Best regards junxian
The easiest way I know to work with a DataGridView is to create a DataTable… create its columns and then connect the DataTable to the DataGridView as its data source.. I created an empty form and throwed a DataGridView on it with the name "gridNumbers" here is the code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DataGridExample { public partial class frmMain : Form { DataTable oTable; public frmMain() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { oTable = new DataTable(); oTable.Columns.Add("Number", typeof(int)); //with cell's type.. oTable.Columns.Add("Text"); //a simple way.. gridNumbers.DataSource = oTable; //connecting the table to the grid AddRow(1, "One"); AddRow(2, "Two"); AddRow(3, "Three"); AddRow(4, "Four"); } protected void AddRow(int num, string text) { DataRow oRow = oTable.NewRow(); //getting a row with the table's scheme oRow["Number"] = num; oRow["Text"] = text; oTable.Rows.Add(oRow); } } }
I tried to make this example as clear as possible... hope you got your answer :-)