what is datagrid how to bind dynamic retrieved records to datagrid control
Displaying data in tabular format is an essential part of any web application. In classis ASP to get data in a table, programmers write intermixed html table elements and ASP script. Programmers create a recordset and then dynamically generate table by using response.write in table TD elements .Such type of ASP scripting is very difficult to manage.
ASP.NET simplifies this task by providing powerful and useful Datagrid control.You can bind data grid to various data sources such as arrays and dynamically retrieved database records. Here we are going to illustrate how to bind datagrid to database records.
Import System. Data. Sqlclient
public sub page_load (sender as object, e as eventargs)
if page.ispostback = false then
‘ function which binds data to datagrid
‘Connection string for making a connection to Sql Server Databse mydb on local ‘machine
Dim connString as String
connString=“provider = Sqlserver; Data Source = (local); Database = mydb; User = sa ; pwd = sa;”
Dim query as String
‘ query string which fetches active employees from employee master table
query=”Select emp_no, emp_name, emp_dob from emp_master where emp_status = ’ A’”
Dim conn as new SqlConnection (connString)
DIM objCmd as new SqlCommand ( query, conn)
Dim rd as SqlDataReader
rd = objCmd.executeReader()
‘ assign sqldatareader to datasource property of datagrid.
mydatagrid.datasource = rd
In html form add this line
asp:datagrid id = “mydatagrid” runat = “server”
To make datgrid more neat and colored we can various attributes.These attributes are similar to those we find in Html elements like backcolor, bordercolor, borderstyle, footerstyle , headerstyle, cellpading and many more.
Start earning now, Join my network on Bukisa
Other articles you may be interested in