What is Datagrid How to Bind Dynamic Retrieved Records to Datagrid Control

Google+ Pinterest LinkedIn Tumblr +

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

Import System.Data

public sub page_load (sender as object, e as eventargs)

if page.ispostback = false then


end if

end sub

‘ function which binds data to datagrid

Function BindData()

‘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



End Function

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.

Happy Coding

Start earning now, Join my network on Bukisa

Other articles you may be interested in

Writers and Bloggers take care of your eyes

How to know private IP address and public IP address

How to resolve time out expired error in ASP.NET

How to use querystring in asp.net for passing parameters

Setting custom error pages in asp.net using configuration file


About Author

Leave A Reply