T
Tony
Hello!
I have created a class named Person defined below. I have made this class
a DataSource by clicking and selecting (Data->Add New Data Source) from the
Data option in the meny.
So now in the area named Data Source on my screen I can see Person with its
field
Age, Name and Person_number. I have also dragged this Data Source named
Person as a DataGridView into my Form.
When I run my program the contents of my arrList is displayed in the
DataGridView as three rows which is correct because
I have added three rows to my created ArrayList which is named arrList in
the program
Now to my 2 questions:
Question number 1: I want to be able to add a new row into the DataGridView
by writing a Age, Name and Person_number ?
Question number 2: I want to be able to delete any row from the displayed
DataGridView ?
Can somebody help me with this?
I have listed all of the relevant code below.
public partial class Form1 : Form
{
ArrayList arrList = new ArrayList();
public Form1()
{
InitializeComponent();
arrList.Add(new Person("532513-1234", "Olle", 34));
arrList.Add(new Person("123456-4321", "Pelle", 12));
arrList.Add(new Person("987654-5678", "Stina", 44));
personDataGridView.DataSource = arrList;
}
}
class Person
{
private string person_number;
private string name;
private int age;
public Person(string pers_nr, string name, int age)
{
person_number = pers_nr;
this.name = name;
this.age = age;
}
public string Person_number
{
get { return person_number; }
set { person_number = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.personDataGridView = new
System.Windows.Forms.DataGridView();
this.dataGridViewTextBoxColumn1 = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn2 = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn3 = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.personBindingSource = new
System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.personDataGridView)).BeginI
nit();
((System.ComponentModel.ISupportInitialize)(this.personBindingSource)).Begin
Init();
this.SuspendLayout();
//
// personDataGridView
//
this.personDataGridView.AutoGenerateColumns = false;
this.personDataGridView.Columns.AddRange(new
System.Windows.Forms.DataGridViewColumn[] {
this.dataGridViewTextBoxColumn1,
this.dataGridViewTextBoxColumn2,
this.dataGridViewTextBoxColumn3});
this.personDataGridView.DataSource = this.personBindingSource;
this.personDataGridView.Location = new System.Drawing.Point(135,
94);
this.personDataGridView.Name = "personDataGridView";
this.personDataGridView.Size = new System.Drawing.Size(385,
220);
this.personDataGridView.TabIndex = 0;
//
// dataGridViewTextBoxColumn1
//
this.dataGridViewTextBoxColumn1.DataPropertyName = "Age";
this.dataGridViewTextBoxColumn1.HeaderText = "Age";
this.dataGridViewTextBoxColumn1.Name =
"dataGridViewTextBoxColumn1";
//
// dataGridViewTextBoxColumn2
//
this.dataGridViewTextBoxColumn2.DataPropertyName =
"Person_number";
this.dataGridViewTextBoxColumn2.HeaderText = "Person_number";
this.dataGridViewTextBoxColumn2.Name =
"dataGridViewTextBoxColumn2";
//
// dataGridViewTextBoxColumn3
//
this.dataGridViewTextBoxColumn3.DataPropertyName = "Name";
this.dataGridViewTextBoxColumn3.HeaderText = "Name";
this.dataGridViewTextBoxColumn3.Name =
"dataGridViewTextBoxColumn3";
//
// personBindingSource
//
this.personBindingSource.DataSource =
typeof(WindowsApplication7.Person);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(627, 399);
this.Controls.Add(this.personDataGridView);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.personDataGridView)).EndIni
t();
((System.ComponentModel.ISupportInitialize)(this.personBindingSource)).EndIn
it();
this.ResumeLayout(false);
}
private System.Windows.Forms.BindingSource personBindingSource;
private System.Windows.Forms.DataGridView personDataGridView;
private System.Windows.Forms.DataGridViewTextBoxColumn
dataGridViewTextBoxColumn1;
private System.Windows.Forms.DataGridViewTextBoxColumn
dataGridViewTextBoxColumn2;
private System.Windows.Forms.DataGridViewTextBoxColumn
dataGridViewTextBoxColumn3;
//Tony
I have created a class named Person defined below. I have made this class
a DataSource by clicking and selecting (Data->Add New Data Source) from the
Data option in the meny.
So now in the area named Data Source on my screen I can see Person with its
field
Age, Name and Person_number. I have also dragged this Data Source named
Person as a DataGridView into my Form.
When I run my program the contents of my arrList is displayed in the
DataGridView as three rows which is correct because
I have added three rows to my created ArrayList which is named arrList in
the program
Now to my 2 questions:
Question number 1: I want to be able to add a new row into the DataGridView
by writing a Age, Name and Person_number ?
Question number 2: I want to be able to delete any row from the displayed
DataGridView ?
Can somebody help me with this?
I have listed all of the relevant code below.
public partial class Form1 : Form
{
ArrayList arrList = new ArrayList();
public Form1()
{
InitializeComponent();
arrList.Add(new Person("532513-1234", "Olle", 34));
arrList.Add(new Person("123456-4321", "Pelle", 12));
arrList.Add(new Person("987654-5678", "Stina", 44));
personDataGridView.DataSource = arrList;
}
}
class Person
{
private string person_number;
private string name;
private int age;
public Person(string pers_nr, string name, int age)
{
person_number = pers_nr;
this.name = name;
this.age = age;
}
public string Person_number
{
get { return person_number; }
set { person_number = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.personDataGridView = new
System.Windows.Forms.DataGridView();
this.dataGridViewTextBoxColumn1 = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn2 = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn3 = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.personBindingSource = new
System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.personDataGridView)).BeginI
nit();
((System.ComponentModel.ISupportInitialize)(this.personBindingSource)).Begin
Init();
this.SuspendLayout();
//
// personDataGridView
//
this.personDataGridView.AutoGenerateColumns = false;
this.personDataGridView.Columns.AddRange(new
System.Windows.Forms.DataGridViewColumn[] {
this.dataGridViewTextBoxColumn1,
this.dataGridViewTextBoxColumn2,
this.dataGridViewTextBoxColumn3});
this.personDataGridView.DataSource = this.personBindingSource;
this.personDataGridView.Location = new System.Drawing.Point(135,
94);
this.personDataGridView.Name = "personDataGridView";
this.personDataGridView.Size = new System.Drawing.Size(385,
220);
this.personDataGridView.TabIndex = 0;
//
// dataGridViewTextBoxColumn1
//
this.dataGridViewTextBoxColumn1.DataPropertyName = "Age";
this.dataGridViewTextBoxColumn1.HeaderText = "Age";
this.dataGridViewTextBoxColumn1.Name =
"dataGridViewTextBoxColumn1";
//
// dataGridViewTextBoxColumn2
//
this.dataGridViewTextBoxColumn2.DataPropertyName =
"Person_number";
this.dataGridViewTextBoxColumn2.HeaderText = "Person_number";
this.dataGridViewTextBoxColumn2.Name =
"dataGridViewTextBoxColumn2";
//
// dataGridViewTextBoxColumn3
//
this.dataGridViewTextBoxColumn3.DataPropertyName = "Name";
this.dataGridViewTextBoxColumn3.HeaderText = "Name";
this.dataGridViewTextBoxColumn3.Name =
"dataGridViewTextBoxColumn3";
//
// personBindingSource
//
this.personBindingSource.DataSource =
typeof(WindowsApplication7.Person);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(627, 399);
this.Controls.Add(this.personDataGridView);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.personDataGridView)).EndIni
t();
((System.ComponentModel.ISupportInitialize)(this.personBindingSource)).EndIn
it();
this.ResumeLayout(false);
}
private System.Windows.Forms.BindingSource personBindingSource;
private System.Windows.Forms.DataGridView personDataGridView;
private System.Windows.Forms.DataGridViewTextBoxColumn
dataGridViewTextBoxColumn1;
private System.Windows.Forms.DataGridViewTextBoxColumn
dataGridViewTextBoxColumn2;
private System.Windows.Forms.DataGridViewTextBoxColumn
dataGridViewTextBoxColumn3;
//Tony