Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony - 29 Feb 2008 10:15 GMT
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
Marc Gravell - 29 Feb 2008 10:21 GMT
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
Tony - 29 Feb 2008 10:46 GMT
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

> 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
Tony - 29 Feb 2008 10:50 GMT
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

"Marc Gravell" <marc.gravell@gmail.com> skrev i meddelandet
news:OgvUbzreIHA.4056@TK2MSFTNGP06.phx.gbl...
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.