c# - Display tables from SQL in DataGridView -
i created database named charityah containing 5 tables. names listed in combobox.
when choose 1 of them want display content in datagridview.
what tried is: first linked datagridview database , tried code found:
sqlconnection connection = new sqlconnection(); private void combobox1_selectedindexchanged(object sender, eventargs e) { string s = combobox1.text; connection.connectionstring = @"data source=(localdb)\v11.0;attachdbfilename=c:\users\downloads\charityah.mdf;integrated security=true"; using (connection) { connection.open(); sqldataadapter adapter = new sqldataadapter("select * "+s, connection); dataset ds = new dataset(); adapter.fill(ds, s); datagridview1.datasource = ds.tables[0]; datagridview1.refresh(); } }
this method doesn't give me errors , finds tables, nothing seen in datagridview.
since report (comments) there rows, sounds primary problem (connection disposal aside) issue data-binding. few thoughts leap mind:
- is table in virtual mode?
- is adding columns?
- do columns exist?
you might want try adding:
datagridview1.virtualmode = false; datagridview1.columns.clear(); datagridview1.autogeneratecolumns = true;
before the:
datagridview1.datasource = ds.tables[0];
you might want check datagridview1.datamember
doesn't have value assigned.
Comments
Post a Comment