How do I add/delete row into/from a DataGridView

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
 
M

Marc Gravell

Try using BindingList<Person> instead of ArrayList.

You should find all your problems go away; if not, let me/us know what
problems you still get.

Marc
 
T

Tony

Hello!

I do some Test now on the DataGridView so we know how to add new rows and
remove rows from the DataGridView.

Now I have added a event handler for the button named button1_Click se
below. This works but it's not the way that we want to add new rows.
We want to add new rows just by writing directly into the DataGridView. I
think that I have seen somewhere that a kind of symbol is displayed in the
column
far to the left and when clicking on that symbol a new empty row is given
and here new values can be entered.

So my question is how do I get the kind of symbol to the far left that give
me a new row where I can enter new values.

private void button1_Click(object sender, EventArgs e)
{
Person p = new Person("875634-9876", "Sven", 13);
bl.Add(p);
}


//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top