Bind the static data to GridView control


Very often we come across the situation to bind the static data to GridView control in .NET, either that is for the POC purpose or training purpose. Are you looking for Bind the static data to GridView control? Then check this article. We can do bind the static data to GridView control in several ways. Find the code for Bind the static data to GridView control.

Learn how to bind the static data to GridView control


Below is the sample code snippet to bind the data to a GridView control in .NET.


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = bindGridView();
GrvExportExcel.DataSource = dt;
GrvExportExcel.DataBind();
}
}
public DataTable bindGridView()
{
DataTable table = new DataTable();

table.Columns.Add("StudentID", typeof(int));
table.Columns.Add("StudentName", typeof(string));
table.Columns.Add("FatherName", typeof(string));
table.Columns.Add("City", typeof(string));
table.Columns.Add("Country", typeof(string));

table.Rows.Add(101, "Satwik", "Mohan", "Hyderabad","India");
table.Rows.Add(102, "Siddu", "Mahesh", "Hyderabad", "India");
table.Rows.Add(104, "Sravan", "Sujan", "Hyderabad", "India");
table.Rows.Add(105, "Nishanth", "Venkat", "Hyderabad", "India");
table.Rows.Add(106, "Tejaswi", "Veera", "Hyderabad", "India");
table.Rows.Add(107, "Sushanth", "Swaroop", "Secbad", "India");
table.Rows.Add(108, "Swetha", "Chanti", "Secbad", "India");
table.Rows.Add(109, "Niraja", "Venkat", "Secbad", "India");
table.Rows.Add(110, "Nisha", "Naga", "Secbad", "India");
table.Rows.Add(111, "Satwik", "Mohan", "Hyderabad", "India");
table.Rows.Add(112, "Siddu", "Mahesh", "Hyderabad", "India");
table.Rows.Add(114, "Sravan", "Sujan", "Hyderabad", "India");

return table;
}


Below is the screen shot.
Static Data to Gridview


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: