2013年9月11日星期三

C # datagridview data binding problem

I have a datagridview control winform Riga , and then design the columns of the table header dg , namely: No. Well , the top depth (m), bottom depth (m), OD (mm). Then I learned from a table " BHA " could read the data, want them to bind to the datagridview headers already designed inside those columns .
how to get ?
My code is written:
string zjstr = "select * from BHA where hash = " + " ; '"+ jichushuju.jinghao +"' "+" order by the end of deep " ;
OleDbConnection myconn1 = new OleDbConnection ();
myconn1.ConnectionString = "Provider = Microsoft.Jet.OleDb.4.0; Data Source = shuili.mdb ";
OleDbCommand zjcomd = new OleDbCommand (zjstr, ; myconn1);
zjzhdataSet.Clear ();
myconn1.Open ();
OleDbDataReader myreader1 = zjcomd.ExecuteReader ();
while (myreader1.Read ())
{
OleDbDataAdapter myadapter = ; new OleDbDataAdapter (zjstr, myconn1);
myadapter.Fill (zjzhdataSet, " BHA " ) ;
dataGridView1.DataSource = ; zjzhdataSet. Tables [" BHA " ] ;

}
myconn1.Close ();
run out the effect of this is that the data read out, it is bound to datagridview inside , but I am not bound to design a good header inside those columns , but in the back re- add a few columns to display these data .
I want to know , if you want to read data from the table , it shows good design in my column inside the column name , code, how to get , or feasible do ?
------ Solution ---------------------------------------- ----
In fact, the direct use of sql statement can handle, such as
string zjstr = "select hash , dingshen top depth (m) from hole assembly where the pound sign = " + " '"+ jichushuju.jinghao +"' "+" order by the end of deep " ;


------ Solution ------------------------------------ --------
you if it is through code design , it can refer to the following code:
GridView.Columns.Add ("COLUMN_NAME", " Column Name ") ;/ / "COLUMN_NAME" is the name of the GridView Column " column name" is
/ / show name
GridView.Columns [0]. DataPropertyName = "dt column name" ;
GridView.DataSource = dt ;/ / dt is the data source
---- - Solution --------------------------------------------
landlord very persistent ah , ha ha , put a few columns DataPropertyName attribute specifies what
------ Solution ------------------------ --------------------
write SQL statements when using an alias , select the field name AS (" you have to define the field name" ) , then check out the query directly after filling out of the dataset to datagridview directly inside .
------ For reference only -------------------------------------- -
not design the column directly tied
------ For reference only ---------------------------- -----------
dataGridView1.DataSource = zjzhdataSet. Tables [" BHA " ] ;
plus
dataGridView1.DataBind ();
------ For reference only ---------------------------- -----------
I wanted it to bind to the I -designed inside
------ For reference only ----------- ----------------------------

And do not forget the special character handling column names
------ For reference only ---------------------------------------
add this

dataGridView1.AutoGenerateColumns = false;
------ For reference only ------------------------- --------------
big you are God's way data binding , but does not appear in front of me a good job inside the column header , all newly built in the back row
Is there a way to make it appear in front of me compiled a good header of the column inside ah ?
Guiqiu
------ For reference only -------------------------------- -------
directly with SQL queries
------ For reference only ----------------------- ----------------
encounter the same problem. . .
------ For reference only -------------------------------------- -
the first time , also encountered the same problem ....

没有评论:

发表评论