Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a list<song> ,a datagrivew
C#
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

C#
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
Updated 4-Nov-12 2:16am
v2
Comments
Abhishek Pant 4-Nov-12 5:21am    
just retag also to Sql server or sql

everything seems ok except
you forgot:
C#
datagridview1.bind();
?
 
Share this answer
 
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
Hi ,
Check this
First Tag your question winforms
C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900