c# - GridView doesn't show data -
private void fill() { adptr = new oledbdataadapter(@"select * libraryinfo first_name='"+loginname+"'", cn); //loginname string variable displaying users info after login ds.clear(); adptr.fill(ds); datagridview1.datasource = ""; datagridview1.datasource = ds.tables[0]; }
after database updated, gridview didn't show data.
taken msdn article on databind
property.
void page_load(object sender, eventargs e) { // illustration uses microsoft sql server , connects // northwind sample database. info source needs // bound gridview command when // page first loaded. thereafter, values // stored in view state. if(!ispostback) { // declare query string. string querystring = "select [customerid], [companyname], [address], [city], [postalcode], [country] [customers]"; // run query , bind resulting dataset // gridview control. dataset ds = getdata(querystring); if (ds.tables.count > 0) { authorsgridview.datasource = ds; authorsgridview.databind(); } else { message.text = "unable connect database."; } } } dataset getdata(string querystring) { // retrieve connection string stored in web.config file. string connectionstring = configurationmanager.connectionstrings["northwindconnectionstring"].connectionstring; dataset ds = new dataset(); seek { // connect database , run query. sqlconnection connection = new sqlconnection(connectionstring); sqldataadapter adapter = new sqldataadapter(querystring, connection); // fill dataset. adapter.fill(ds); } catch(exception ex) { // connection failed. display error message. message.text = "unable connect database."; } homecoming ds; }
this snippet shows how (a) bind dataset gridview, assigning using databind method.
the site says:
use databind() method bind info data source gridview control. method resolves data-binding expressions in active template of control.
solution
i reference line saying:
authorsgridview.databind();
which binds info control.
side note
to protect sqli, should read on sql parameters, , not direct concatenation.
winform exampletutorial found: here
//create connection string string connstring = "provider=microsoft.jet.oledb.4.0;data source=c:\\mydatabase.mdb"; //create database query string query = "select * mytable"; //create oledbdataadapter execute query oledbdataadapter dadapter = new oledbdataadapter(query, connstring); //create command builder oledbcommandbuilder cbuilder = new oledbcommandbuilder(dadapter); //create datatable hold query results datatable dtable = new datatable(); //fill datatable dadapter.fill(dtable);
also:
//the datagridview datagridview dgview = new datagridview(); //bindingsource sync datatable , datagridview bindingsource bsource = new bindingsource(); //set bindingsource datasource bsource.datasource = dtable; //set datagridview datasource dgview.datasource = bsource;
c# winforms gridview
No comments:
Post a Comment