Bind Data to GridView From Different Data Sources

Introduction:

 In this Article, I have tried my best, to create  different data source and bind it to the grid view. Since datasource and grid view are very much important to work in any application. In the future, if I have found any another datasource will append it to this article. Hope it will be a good reference or starting point for beginners.

Attachement : SolutionFileDownload

 List of Different Collections Used in this Article:

  • Bind Gridview Using Array of Object
  • Bind Gridview Using One Dimensional Array
  • Bind Gridview Using Two Dimensional Array
  • Bind Gridview Using Multi Dimensional Array
  • Bind Gridview Using ArrayList
  • Bind Gridview Using GenericList
  • Bind Gridview Using DataTable
  • Bind Gridview Using Linq Query Result
  • Bind Gridview Using XML

 

Bind Gridview Using Array with class :

 First create a class like below:

Declare and Initialize array like below:

Output:

arraywithclass

Bind Grid view Using One Dimensional Array:

 Code:

Output:

arrayonedimesional

 

Bind Gridview Using Two Dimensional Array:

 

Code:

Output:

twodimensional array
twodimensional array

 

 Note:

 

if we try to bind two/multi dimensional directly to gridview, then we will get an exception with message below

                                         Array was not a one-dimensional array.

 

Bind Gridview Using Multi Dimensional Array:

 

Code:

 

Output:

arraymultidimensional

 Bind Gridview Using ArrayList:

 

Create  class:

 

Create Arraylist:

 

Output:

arraylist

  

Bind Gridview Using GenericList:

 

 

 

Create  Generic list:

 

 

Output:

 

genericlist

 Bind Gridview Using DataTable:

Create   DataTable:

 

 

Output:

bindgridviewdatatable

Bind Gridview Using Linq Query Result:

Linq Creation:

// The Three Parts of a LINQ Query:

//  1. Data source.

 

 

// 2. Query creation.

//Linq query syntax -justfYI

// from – data source to tempvar

//where – Conditional

//orderby -sorting

//select – column selection

//group  – groupby

 

 

 

// 3. Query execution.

 

 

Output:

bindgridfromlinqresult

 

 

Bind Gridview using XML Datasource:

 

XML :

 

 

Code:

 

Output:

xmldatasource

I have added this entire example, as a one solution file in the attachment section, Hope it was useful for you, kindly let me know your thoughts or feedbacks

Thanks

Karthik

 

 

 

 

 

 

821 total views, 9 views today

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *