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
-
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
-
/// <summary>
-
/// Summary description for Customer
-
/// </summary>
-
public class Customer
-
{
-
private int _id;
-
private string _name;
-
-
#region " Properties "
-
-
public int ID
-
{
-
get { return _id; }
-
set { _id = value; }
-
}
-
-
public string Name
-
{
-
get { return _name; }
-
set { _name = value; }
-
}
-
-
#endregion
-
-
#region " Constructors "
-
-
public Customer(int id, string name)
-
{
-
_id = id;
-
_name = name;
-
}
-
-
#endregion
-
}
-
2- create a SimpleList that implemet the IList as in http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx
-
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Collections;
-
-
/// <summary>
-
/// Summary description for SimpleList
-
/// </summary>
-
public class SimpleList : IList
-
{
-
private object[] _contents = new object[8];
-
private int _count;
-
-
public SimpleList()
-
{
-
_count = 0;
-
}
-
-
// IList Members
-
public int Add(object value)
-
{
-
if (_count < _contents.Length)
-
{
-
_contents[_count] = value;
-
_count++;
-
-
return (_count – 1);
-
}
-
else
-
{
-
return -1;
-
}
-
}
-
-
public void Clear()
-
{
-
_count = 0;
-
}
-
-
public bool Contains(object value)
-
{
-
bool inList = false;
-
for (int i = 0; i < Count; i++)
-
{
-
if (_contents[i] == value)
-
{
-
inList = true;
-
break;
-
}
-
}
-
return inList;
-
}
-
-
public int IndexOf(object value)
-
{
-
int itemIndex = -1;
-
for (int i = 0; i < Count; i++)
-
{
-
if (_contents[i] == value)
-
{
-
itemIndex = i;
-
break;
-
}
-
}
-
return itemIndex;
-
}
-
-
public void Insert(int index, object value)
-
{
-
if ((_count + 1 <= _contents.Length) && (index < Count) && (index >= 0))
-
{
-
_count++;
-
-
for (int i = Count – 1; i > index; i–)
-
{
-
_contents[i] = _contents[i - 1];
-
}
-
_contents[index] = value;
-
}
-
}
-
-
public bool IsFixedSize
-
{
-
get
-
{
-
return true;
-
}
-
}
-
-
public bool IsReadOnly
-
{
-
get
-
{
-
return false;
-
}
-
}
-
-
public void Remove(object value)
-
{
-
RemoveAt(IndexOf(value));
-
}
-
-
public void RemoveAt(int index)
-
{
-
if ((index >= 0) && (index < Count))
-
{
-
for (int i = index; i < Count – 1; i++)
-
{
-
_contents[i] = _contents[i + 1];
-
}
-
_count–;
-
}
-
}
-
-
public object this[int index]
-
{
-
get
-
{
-
return _contents[index];
-
}
-
set
-
{
-
_contents[index] = value;
-
}
-
}
-
-
// ICollection Members
-
-
public void CopyTo(Array array, int index)
-
{
-
int j = index;
-
for (int i = 0; i < Count; i++)
-
{
-
array.SetValue(_contents[i], j);
-
j++;
-
}
-
}
-
-
public int Count
-
{
-
get
-
{
-
return _count;
-
}
-
}
-
-
public bool IsSynchronized
-
{
-
get
-
{
-
return false;
-
}
-
}
-
-
// Return the current instance since the underlying store is not
-
// publicly available.
-
public object SyncRoot
-
{
-
get
-
{
-
return this;
-
}
-
}
-
-
// IEnumerable Members
-
-
public IEnumerator GetEnumerator()
-
{
-
// Refer to the IEnumerator documentation for an example of
-
// implementing an enumerator.
-
throw new Exception("The method or operation is not implemented.");
-
}
-
-
public void PrintContents()
-
{
-
Console.WriteLine("List has a capacity of {0} and currently has {1} elements.", _contents.Length, _count);
-
Console.Write("List contents:");
-
for (int i = 0; i < Count; i++)
-
{
-
Console.Write(" {0}", _contents[i]);
-
}
-
Console.WriteLine();
-
}
-
}
-
3- Add this function to you code behind
-
-
/// <summary>
-
/// This method gets a default set of customer
-
/// </summary>
-
/// <returns>A collection of orders.</returns>
-
private SimpleList GetDefaultOrders()
-
{
-
SimpleList customers = new SimpleList();
-
customers.Add(new Customer(1, "HP"));
-
customers.Add(new Customer(2, "IPM"));
-
customers.Add(new Customer(2, "Dell"));
-
return customers;
-
}
-
4- Now this is what goes into your Page_Load()
-
-
SimpleList customers = this.GetDefaultOrders();
-
-
DataTable table = new DataTable();
-
table.Columns.Add("ID");
-
table.Columns.Add("Name");
-
-
for (int i = 0; i < customers.Count; i++)
-
{
-
Customer p = (Customer)customers[i];
-
int ID = p.ID;
-
string Name = p.Name;
-
DataRow dr = table.NewRow();
-
dr["ID"] = ID;
-
dr["Name"] = Name;
-
table.Rows.Add(dr);
-
}
-
-
//Bind the collection to the gridview
-
this.gvwCustomer.DataSource = table;
-
this.gvwCustomer.DataBind();
-
5 – this should be your gvwCustomer GridView:
-
-
<asp:GridView ID="gvwCustomer" CssClass="GridView" ToolTip="Calculation" runat="server" AutoGenerateColumns="False" Width="100%">
-
<Columns>
-
<asp:BoundField DataField="ID" HeaderText="PO Line" ReadOnly="True" SortExpression="ID" />
-
<asp:BoundField DataField="Name" HeaderText="Description" ReadOnly="True" SortExpression="Name" />
-
</Columns>
-
</asp:GridView>
-
6 – Good Luck
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.


