Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

im having a problem to build a gridview that has column and row that display data from database .is it possible to build it? i dont know how to do it.the template in visual studio only has the column.not the row.i already tried so may times but it didnt work.please help me on how to build it and also how to bind it.

eg: Column0 Column1 Column2

Row0

Row1

Row2

pls help me.thanks.
Posted
Updated 15-Jan-12 19:51pm
v3

C#
//create the connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myDatabase.mdb";

//create the database query
string query = "SELECT * FROM MyTable";

//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

//create a DataTable to hold the query results
DataTable dTable = new DataTable();

//fill the DataTable
dAdapter.Fill(dTable);

//the DataGridView
DataGridView dgView = new DataGridView();

//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();

//set the BindingSource DataSource
bSource.DataSource = dTable;

//set the DataGridView DataSource
dgView.DataSource = bSource;

dAdapter.Update(dTable);
 
Share this answer
 
Comments
musiw 16-Jan-12 3:36am    
my database:

table transshipment:
id
id_transshipment
no_of_train
date

table class_transshipment:
id_transshipment
desc_

id_transshipment= 1
desc_ =EMU

id_transshipment= 2
desc_ =intercity

eg: EMU Intercity
no.of trains Data Data

i want to display the data from transshipment table in row and column format.

eg:
RDBurmon 16-Jan-12 4:02am    
Ok give me a minute I will revert back.

in the mean time let me know can you use stored procedure instead of query ?
musiw 16-Jan-12 4:55am    
i dont use stored procedure.i just using query as usual..is it possible?cause i dont familiar using stored procedure.but if you think it is the best way, hope u can show me the code later.
RDBurmon 16-Jan-12 6:46am    
Use this

SqlDataAdapter sqlcom0 = new SqlDataAdapter("SELECT * FROM ( SELECT t.no_of_train,ct.desc_ FROM @transshipment t LEFT OUTER JOIN @class_transshipment ct ON t.id_transshipment=ct.id_transshipment where t.id_transshipment in (1,2)) AS Tbl1 PIVOT (AVG (no_of_train) FOR Desc_ IN ([EMU],[Intercity])) AS no_of_train", myConnection);

DataSet ds0 = new DataSet();
sqlcom0.Fill(ds0, "transshipment");
gd_transshipment.DataSource = ds0.Tables["transshipment"].DefaultView;
gd_transshipment.DataBind();
RDBurmon 16-Jan-12 6:46am    
this is the query for your reference

SELECT * FROM
( SELECT t.no_of_train,ct.desc_
FROM @transshipment t LEFT OUTER JOIN @class_transshipment ct ON t.id_transshipment=ct.id_transshipment where t.id_transshipment in (1,2)) AS Tbl1
PIVOT (AVG (no_of_train) FOR Desc_ IN ([EMU],[Intercity])) AS no_of_train
Are you looking for something like Pivot by any chance?
Ref: http://unboxedsolutions.com/sean/archive/2004/08/30/302.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900