Click here to Skip to main content
12,403,811 members (69,242 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
i have a list<song> ,a datagrivew
Class Song{
public string Name{get;set;}
}
List<Song> songs ;
void Form_Load{
 
 songs = new List<Song>
{
 new Song() {Name = "song1"},
 new Song() {Name = "song2"},
 new Song() {Name = "song3"}
}
 
datagridview1.datasource = songs;
 
}
void button1_click(){
 
songs.add(new Song() {Name = "song4"})
 
}

when i click to button 1 then datagridview1 not add new songs.everyone help me please.

i want click to button1 data will auto binding to datagridview1.

i have used a way

void button1_click(){ 
songs.add(new Song() {Name = "song4"}) 
datagridview1.datasource = null;
 
datagridview1.datasource = songs; 
}

but i feel it not handy. someone help me another way please.

thanks.
Posted 3-Nov-12 23:00pm
Updated 4-Nov-12 2:16am
v2
Comments
Abhishek Pant 4-Nov-12 5:21am
   
just retag also to Sql server or sql
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

everything seems ok except
you forgot:
datagridview1.bind();
?
  Permalink  
v2
Comments
Mohamed Mitwalli 4-Nov-12 8:17am
   
It's winform not web :)
dao dinh doan 5-Nov-12 0:14am
   
i dont understand your opinion .because in datagridview have not bind method.can you write more specific?.
thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi ,
Check this
First Tag your question winforms
List<Song> songs;
        private void Form1_Load(object sender, EventArgs e)
        {
            songs = new List<Song>
        {
                new Song() {Name = "song1"},
                new Song() {Name = "song2"},
                new Song() {Name = "song3"}
        };
            dataGridView1.DataSource = songs;
        }
 
        int i = 4;
        private void button1_Click(object sender, EventArgs e)
        {
 
            songs.Add(new Song() { Name = "song"+i });
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = songs;
            i++;
        }
    }
}
class Song
{
    public string Name { get; set; }
}

Best Regards
M.Mitwalli
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100