Wednesday, August 11, 2010

How to fill datagrid using a query in vb.net??

I need to know how to display select query result in the datagrid in vb.net ,


the database i am using is SQL ExpressHow to fill datagrid using a query in vb.net??
I'll assume your database has a Table called Shirts with columns ID, Size, and Color.





On your VB.net page you must add your datagrid


%26lt;asp:datagrid id=';dgShirts%26gt;


add columns for ID, Size and Color





On the code behind page create a datatable


Dim dtShirts as datatable


add columns for ID, Size and Color





Make your database connection and do your SQL





SELECT * FROM Shirts WHERE Color = 'GREEN'





Id use a Datareader to read the query result





While MyDataReader.Read = TRUE


Dim MyRow() as new Datatable.Row


MyRow(0) = MyDataReader(';ID';)


MyRow(0) = MyDataReader(';Size';)


MyRow(0) = MyDataReader(';Color';)





dtShirts.Add(MyRow)





End While





Now you have the Database info in a datatable so you need to bind that data to the datagrid





dgShirts.Source = dtShirts


dgShirts.Bind





Apologies for minor syntax errors but that should get you most of the way

No comments:

Post a Comment