Transfer data from one Gridview to another in Asp.net

I struggled with this for hours, I couldn’t find any good and complete answer. The solution I found on line uses list and kept trowing exceptions at the gridView1.DataBind() line. so here is my way of doing this. I hope this may help someone else just trying to get the content of one gridview and displaying it in another:

In may case I had MultyView form that contains a gridview at the 6th step and I wanted to display the content of the the whole form in the final step as a summary before the user can submit it.
I’m not going to go through all the details, but this was part of the webform developed for a WorkflowGen Process.

1- create a custom list and save it into your App_Code Folder

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6.  
  7. /// <summary>
  8. /// Summary description for Customer
  9. /// </summary>
  10. public class Customer
  11. {
  12. private int _id;
  13. private string _name;
  14.  
  15. #region " Properties "
  16.  
  17. public int ID
  18. {
  19. get { return _id; }
  20. set { _id = value; }
  21. }
  22.  
  23. public string Name
  24. {
  25. get { return _name; }
  26. set { _name = value; }
  27. }
  28.  
  29. #endregion
  30.  
  31. #region " Constructors "
  32.  
  33. public Customer(int id, string name)
  34. {
  35. _id = id;
  36. _name = name;
  37. }
  38.  
  39. #endregion
  40. }
  41.  

2- create a SimpleList that implemet the IList as in http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Collections;
  7.  
  8. /// <summary>
  9. /// Summary description for SimpleList
  10. /// </summary>
  11. public class SimpleList : IList
  12. {
  13. private object[] _contents = new object[8];
  14. private int _count;
  15.  
  16. public SimpleList()
  17. {
  18. _count = 0;
  19. }
  20.  
  21. // IList Members
  22. public int Add(object value)
  23. {
  24. if (_count < _contents.Length)
  25. {
  26. _contents[_count] = value;
  27. _count++;
  28.  
  29. return (_count – 1);
  30. }
  31. else
  32. {
  33. return -1;
  34. }
  35. }
  36.  
  37. public void Clear()
  38. {
  39. _count = 0;
  40. }
  41.  
  42. public bool Contains(object value)
  43. {
  44. bool inList = false;
  45. for (int i = 0; i < Count; i++)
  46. {
  47. if (_contents[i] == value)
  48. {
  49. inList = true;
  50. break;
  51. }
  52. }
  53. return inList;
  54. }
  55.  
  56. public int IndexOf(object value)
  57. {
  58. int itemIndex = -1;
  59. for (int i = 0; i < Count; i++)
  60. {
  61. if (_contents[i] == value)
  62. {
  63. itemIndex = i;
  64. break;
  65. }
  66. }
  67. return itemIndex;
  68. }
  69.  
  70. public void Insert(int index, object value)
  71. {
  72. if ((_count + 1 <= _contents.Length) &amp;&amp; (index < Count) &amp;&amp; (index >= 0))
  73. {
  74. _count++;
  75.  
  76. for (int i = Count – 1; i > index; i–)
  77. {
  78. _contents[i] = _contents[i - 1];
  79. }
  80. _contents[index] = value;
  81. }
  82. }
  83.  
  84. public bool IsFixedSize
  85. {
  86. get
  87. {
  88. return true;
  89. }
  90. }
  91.  
  92. public bool IsReadOnly
  93. {
  94. get
  95. {
  96. return false;
  97. }
  98. }
  99.  
  100. public void Remove(object value)
  101. {
  102. RemoveAt(IndexOf(value));
  103. }
  104.  
  105. public void RemoveAt(int index)
  106. {
  107. if ((index >= 0) &amp;&amp; (index < Count))
  108. {
  109. for (int i = index; i < Count – 1; i++)
  110. {
  111. _contents[i] = _contents[i + 1];
  112. }
  113. _count–;
  114. }
  115. }
  116.  
  117. public object this[int index]
  118. {
  119. get
  120. {
  121. return _contents[index];
  122. }
  123. set
  124. {
  125. _contents[index] = value;
  126. }
  127. }
  128.  
  129. // ICollection Members
  130.  
  131. public void CopyTo(Array array, int index)
  132. {
  133. int j = index;
  134. for (int i = 0; i < Count; i++)
  135. {
  136. array.SetValue(_contents[i], j);
  137. j++;
  138. }
  139. }
  140.  
  141. public int Count
  142. {
  143. get
  144. {
  145. return _count;
  146. }
  147. }
  148.  
  149. public bool IsSynchronized
  150. {
  151. get
  152. {
  153. return false;
  154. }
  155. }
  156.  
  157. // Return the current instance since the underlying store is not
  158. // publicly available.
  159. public object SyncRoot
  160. {
  161. get
  162. {
  163. return this;
  164. }
  165. }
  166.  
  167. // IEnumerable Members
  168.  
  169. public IEnumerator GetEnumerator()
  170. {
  171. // Refer to the IEnumerator documentation for an example of
  172. // implementing an enumerator.
  173. throw new Exception("The method or operation is not implemented.");
  174. }
  175.  
  176. public void PrintContents()
  177. {
  178. Console.WriteLine("List has a capacity of {0} and currently has {1} elements.", _contents.Length, _count);
  179. Console.Write("List contents:");
  180. for (int i = 0; i < Count; i++)
  181. {
  182. Console.Write(" {0}", _contents[i]);
  183. }
  184. Console.WriteLine();
  185. }
  186. }
  187.  

3- Add this function to you code behind

  1.  
  2. /// <summary>
  3. /// This method gets a default set of customer
  4. /// </summary>
  5. /// <returns>A collection of orders.</returns>
  6. private SimpleList GetDefaultOrders()
  7. {
  8. SimpleList customers = new SimpleList();
  9. customers.Add(new Customer(1, "HP"));
  10. customers.Add(new Customer(2, "IPM"));
  11. customers.Add(new Customer(2, "Dell"));
  12. return customers;
  13. }
  14.  

4- Now this is what goes into your Page_Load()

  1.  
  2. SimpleList customers = this.GetDefaultOrders();
  3.  
  4. DataTable table = new DataTable();
  5. table.Columns.Add("ID");
  6. table.Columns.Add("Name");
  7.  
  8. for (int i = 0; i < customers.Count; i++)
  9. {
  10. Customer p = (Customer)customers[i];
  11. int ID = p.ID;
  12. string Name = p.Name;
  13. DataRow dr = table.NewRow();
  14. dr["ID"] = ID;
  15. dr["Name"] = Name;
  16. table.Rows.Add(dr);
  17. }
  18.  
  19. //Bind the collection to the gridview
  20. this.gvwCustomer.DataSource = table;
  21. this.gvwCustomer.DataBind();
  22.  

5 – this should be your gvwCustomer GridView:

  1.  
  2. <asp:GridView ID="gvwCustomer" CssClass="GridView" ToolTip="Calculation" runat="server" AutoGenerateColumns="False" Width="100%">
  3. <Columns>
  4. <asp:BoundField DataField="ID" HeaderText="PO Line" ReadOnly="True" SortExpression="ID" />
  5. <asp:BoundField DataField="Name" HeaderText="Description"  ReadOnly="True" SortExpression="Name" />
  6. </Columns>
  7. </asp:GridView>
  8.  

6 – Good Luck

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment