Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 4-Nov-12 0:00am
Edited 4-Nov-12 3:16am
v2
Comments
Abhishek Pant at 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 at 4-Nov-12 8:17am
   
It's winform not web :)
dao dinh doan at 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
0 OriginalGriff 410
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 105
4 PIEBALDconsult 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2014
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