c# - How to add new row to datagridview? -


i have datagridview filled data datasource (sql). want add new row, can't, because new data can't added bounded datagridview...

i trying :

datagridview1.source = null; datagridview1.rows.add("1"); 

but clears previous data in table. how it, add new row without deleting previous data?

when set datasource property null, removing all data datagridview (since doesn't know bind anymore).

you have 2 options here. first update underlying data source. let's assume it's datatable. in case, you'd like:

datatable dt = datagridview1.source datatable; dt.rows.add(new object[] { ... }); 

and datagridview pick on changes (note if not binding doesn't implement inotifycollectionchanged interface, you'll have call resetbindings method grid refresh).

the other option let datagridview manage rows. can manually adding each item using add method on datagridviewrowcollection returned rows property:

foreach (var item in source) {     datagridview1.rows.add("1", "2", "3", ...); } 

i wouldn't second solution optimal, work.

finally, assuming binding datatable (or other materialization of data underlying data source), doesn't updating underlying data source (that separate question).


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -