Bind Data To Grid View From Different Data Sources

Introduction

In this blog, I have tried my best, to create different data sources and bind them to the GridView since data source and Grid View are very important to work in any application. In the future, if I have found any other datasource, I  will append it to this write-up. Hope it will be a good reference or starting point for beginners.

Attachement : SolutionFileDownload

List of different collections used in this blog

  • 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.

  1. public class intializeheaderformat {  
  2.     public int id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string worksheetname {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string headerformat {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public intializeheaderformat(int id, string wsname, string hf) {  
  15.         id = id;  
  16.         worksheetname = wsname;  
  17.         headerformat = hf;  
  18.     }  
  19. }  
Declare and Initialize array like below
  1. Array IHF = new [] {  
  2.     new intializeheaderformat(1, "test""a|b|C"), new intializeheaderformat(2, "test1""d|e|f")  
  3. };  
  4. GridView1.DataSource = IHF;  
  5. GridView1.DataBind();  
Output

Output

Bind Grid view Using One Dimensional Array

Code

  1. string[] arnames = {  
  2.     "karthik",  
  3.     "sachin",  
  4.     "dravid"  
  5. };  
  6. GridView1.DataSource = arnames;  
  7. GridView1.DataBind();  
Output

Output

Bind Gridview Using Two Dimensional Array

Code
  1. string[, ] arnameswithid = {  
  2.     {  
  3.         "1",  
  4.         "karthik"  
  5.     },  
  6.     {  
  7.         "2",  
  8.         "sachin"  
  9.     },  
  10.     {  
  11.         "3",  
  12.         "dravid"  
  13.     }  
  14. };  
  15. ArrayList arrList = new ArrayList();  
  16. for (int i = 0; i < 3; i++) {  
  17.     arrList.Add(new ListItem(arnameswithid[i, 0], arnameswithid[i, 1]));  
  18. }  
  19. GridView1.DataSource = arrList;  
  20. GridView1.DataBind();  
Output

Output

Note

If we try to bind two/multi dimensional data directly to GridView, then we will get an exception with the message below.

Array was not a one-dimensional array

Bind Gridview Using Multi Dimensional Array

Code
  1. string[, ] arrtable = {  
  2.     {  
  3.         "1",  
  4.         "karthik",  
  5.         "programmer"  
  6.     },  
  7.     {  
  8.         "2",  
  9.         "sachin",  
  10.         "CA"  
  11.     },  
  12.     {  
  13.         "3",  
  14.         "dravid",  
  15.         "doctor"  
  16.     }  
  17. };  
  18. DataTable tempdttogetarray = new DataTable();  
  19. tempdttogetarray.Columns.Add("ID"typeof(string));  
  20. tempdttogetarray.Columns.Add("Name"typeof(string));  
  21. tempdttogetarray.Columns.Add("desgination"typeof(string));  
  22. for (int row = 0; row < arrtable.GetLength(0); row++) {  
  23.     tempdttogetarray.Rows.Add();  
  24.     tempdttogetarray.Rows[tempdttogetarray.Rows.Count - 1]["ID"] = arrtable[row, 0];  
  25.     tempdttogetarray.Rows[tempdttogetarray.Rows.Count - 1]["Name"] = arrtable[row, 1];  
  26.     tempdttogetarray.Rows[tempdttogetarray.Rows.Count - 1]["desgination"] = arrtable[row, 2];  
  27. }  
  28. GridView1.DataSource = tempdttogetarray;  
  29. GridView1.DataBind();  
Output

Output

Bind Gridview Using ArrayList

Create class
  1. public class intializeheaderformat {  
  2.     //fields  
  3.     public int _id {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public string _worksheetname {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     public string _headerformat {  
  12.         get;  
  13.         set;  
  14.     }  
  15.     public intializeheaderformat(int id, string wsname, string hf) {  
  16.         _id = id;  
  17.         _worksheetname = wsname;  
  18.         _headerformat = hf;  
  19.     }  
  20. }  
Create Arraylist
  1. ArrayList IHF = new ArrayList();  
  2. intializeheaderformat objinitheader = new intializeheaderformat(102, "test2.xls""d|e|f");  
  3. IHF.Add(objinitheader);  
  4. objinitheader = new intializeheaderformat(103, "test3.xls""H|e|f");  
  5. IHF.Add(objinitheader);  
  6. GridView1.DataSource = IHF;  
  7. GridView1.DataBind();  
Output

Output

Bind Gridview Using GenericList
  1. Create Class: public class intializeheaderformat {  
  2.     //fields  
  3.     public int _id {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public string _worksheetname {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     public string _headerformat {  
  12.         get;  
  13.         set;  
  14.     }  
  15.     public intializeheaderformat(int id, string wsname, string hf) {  
  16.         _id = id;  
  17.         _worksheetname = wsname;  
  18.         _headerformat = hf;  
  19.     }  
  20. }  
Create Generic list
  1. List < intializeheaderformat > lstworksheetformat = new List < intializeheaderformat > ();  
  2. intializeheaderformat objinitheader = new intializeheaderformat(101, "test1.xls""a|b|c");  
  3. //(or)  
  4. //objinitheader._id = 101;  
  5. //objinitheader._worksheetname = "test1.xlsx";  
  6. //objinitheader._headerformat = "a|b|c";  
  7. lstworksheetformat.Add(objinitheader);  
  8. objinitheader = new intializeheaderformat(102, "test2.xls""d|e|f");  
  9. lstworksheetformat.Add(objinitheader);  
  10. objinitheader = new intializeheaderformat(103, "test3.xls""g|h|i");  
  11. lstworksheetformat.Add(objinitheader);  
  12. GridView1.DataSource = lstworksheetformat;  
  13. GridView1.DataBind();  
Output

Output

Bind Gridview Using DataTable

Create DataTable
  1. DataTable intializeheaderformat = new DataTable();  
  2. intializeheaderformat.Columns.Add("Id"typeof(int));  
  3. intializeheaderformat.Columns.Add("worksheetname"typeof(string));  
  4. intializeheaderformat.Columns.Add("headerformat"typeof(string));  
  5. intializeheaderformat.Rows.Add(1, "test1.xls""a|b|c");  
  6. intializeheaderformat.Rows.Add(2, "test2.xls""d|e|f");  
  7. intializeheaderformat.Rows.Add(3, "test3.xls""g|h|i");  
  8. GridView1.DataSource = intializeheaderformat;  
  9. GridView1.DataBind();  
Output

Output

Bind Gridview Using Linq Query Result:

Linq Creation - The Three Parts of a LINQ Query
  1. Data source
    1. List < intializeheaderformat > lstworksheetformat = new List < intializeheaderformat > ();  
    2. intializeheaderformat objinitheader = new intializeheaderformat(101, "test1.xls""a|b|c");  
    3. lstworksheetformat.Add(objinitheader);  
    4. objinitheader = new intializeheaderformat(102, "test2.xls""d|e|f");  
    5. lstworksheetformat.Add(objinitheader);  
    6. objinitheader = new intializeheaderformat(103, "test3.xls""g|h|i");  
    7. lstworksheetformat.Add(objinitheader);  
  2. Query creation
    1. //Linq query syntax -justfYI  
    2. // from – data source to tempvar  
    3. //where – Conditional  
    4. //orderby -sorting  
    5. //select – column selection  
    6. //group – groupby  
    7. var temptable = from tempresult in lstworksheetformat select new {  
    8.     tempresult._id, tempresult._headerformat  
    9. };  
  3. Query execution
    1. GridView1.DataSource = temptable;  
    2. GridView1.DataBind();  
    Output

    Output

Bind Gridview using XML Datasource

  1. XML << ? xml version = "1.0"  
  2. encoding = "utf-8" ? > < workbook > < sheet > < Id > 101 < /Id> < WorkSheetName > test1 < /WorkSheetName > < Format > A | B | C < /Format> < /sheet > < sheet > < Id > 102 < /Id> < WorkSheetName > test2 < /WorkSheetName > < Format > D | E | F < /Format> < /sheet > < sheet > < Id > 103 < /Id> < WorkSheetName > test3 < /WorkSheetName > < Format > G | H | I < /Format> < /sheet > < /workbook>  
Code
  1. DataSet ds = new DataSet("Worksheetformat");  
  2. ds.ReadXml(Server.MapPath("~/Format.xml"));  
  3. GridView1.DataSource = ds.Tables[0];  
  4. GridView1.DataBind();  
Output

Output

I have added this entire example as one solution file in the attachment section. I hope it was useful for you. Kindly let me know your feedback.

 

Ebook Download
View all
Learn
View all