Tuesday, March 3, 2009

Transfer records from one gridview to another gridview




Please visit my new Web Site https://coderstechzone.com



Transfer records from listview to another listview is a common task but sometimes client wants more which will be difficult to implement using such type of controls. In that case developer may choose gridview as another option where developer will get more flexibility with less effort. Here i am showing an example on that issue. Have a look at the picture & then go for implementation:










To do that we need two gridviews & two buttons. So add the below code in your aspx page:
<asp:GridView runat="server" ID="gvLeft" DataKeyNames="ID" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chk"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Code" HeaderText="Supplier code"> </asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Supplier name"> </asp:BoundField>
</Columns>
</asp:GridView>

<asp:Button runat="server" ID="cmdRight" Text=">>>>" OnClick="cmdRight_Click"/>
<asp:Button runat="server" ID="cmdLeft" Text="<<<<" OnClick="cmdLeft_Click"/>

<asp:GridView runat="server" ID="gvRight" EmptyDataText="NO DATA FOUND" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chk"/>
</ItemTemplate>

</asp:TemplateField>
<asp:BoundField DataField="Code" HeaderText="Supplier code"> </asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Supplier name"> </asp:BoundField>
</Columns>
</asp:GridView>

<asp:Button runat="server" ID="cmdUpdate" Text="Update" OnClick="cmdUpdate_Click"/>

Now we need to populate left side gridview. You can populate data from database but for simplicity here i am using datatable to provide "Paste & Run" facility. Write the below codes under page load event:
if (!IsPostBack)
{
DataTable dtLeft = new DataTable("Supplier");

dtLeft.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
dtLeft.Columns.Add(new DataColumn("Code"));
dtLeft.Columns.Add(new DataColumn("Name"));

dtLeft.Rows.Add(1, "st0001", "S.R. Steel");
dtLeft.Rows.Add(2, "ir0039", "Shadesh builders");
dtLeft.Rows.Add(3, "cr0042", "Orchard confec.");
dtLeft.Rows.Add(4, "er0078", "Windblow");
dtLeft.Rows.Add(5, "bd0301", "Rahimkarim");

gvLeft.DataSource = dtLeft;
gvLeft.DataBind();

DataTable dtRight = dtLeft.Clone();
dtRight.Rows.Add(6, "qq0000", "I.j.k ALAM");

gvRight.DataSource = dtRight;
gvRight.DataBind();

ViewState["dtLeft"] = dtLeft;
ViewState["dtRight"] = dtRight;
}

Now we need to make response when user wants to transfer data. To do that write the below server side method under two right & left navigation button click event respectively.
protected void cmdRight_Click(object sender, EventArgs e)
{
DataTable dtLeft = (DataTable)ViewState["dtLeft"];
DataTable dtRight = (DataTable)ViewState["dtRight"];
int tmp = 0;

foreach (GridViewRow oItemLeft in gvLeft.Rows)
{
if (((CheckBox)oItemLeft.FindControl("chk")).Checked)
{
dtRight.Rows.Add(gvLeft.DataKeys[oItemLeft.DataItemIndex].Value, oItemLeft.Cells[1].Text, oItemLeft.Cells[2].Text);
dtLeft.Rows.RemoveAt(oItemLeft.DataItemIndex - tmp);
tmp++;
}
}

gvRight.DataSource = dtRight;
gvRight.DataBind();
gvLeft.DataSource = dtLeft;
gvLeft.DataBind();

ViewState["dtLeft"] = dtLeft;
ViewState["dtRight"] = dtRight;
}

protected void cmdLeft_Click(object sender, EventArgs e)
{
DataTable dtLeft = (DataTable)ViewState["dtLeft"];
DataTable dtRight = (DataTable)ViewState["dtRight"];
int tmp = 0;

foreach (GridViewRow oItemRight in gvRight.Rows)
{
if (((CheckBox)oItemRight.FindControl("chk")).Checked)
{
dtLeft.Rows.Add(gvRight.DataKeys[oItemRight.DataItemIndex].Value, oItemRight.Cells[1].Text, oItemRight.Cells[2].Text);
dtRight.Rows.RemoveAt(oItemRight.DataItemIndex - tmp);
tmp++;
}
}

gvRight.DataSource = dtRight;
gvRight.DataBind();
gvLeft.DataSource = dtLeft;
gvLeft.DataBind();

ViewState["dtLeft"] = dtLeft;
ViewState["dtRight"] = dtRight;
}

Now you have to store the changes into database. To do that follow my below code under Update button:
protected void cmdUpdate_Click(object sender, EventArgs e)
{

foreach (GridViewRow oItem in gvLeft.Rows)
{
string sID=gvLeft.DataKeys[oItem.DataItemIndex].Value.ToString();
string sSQL = "UPDATE Supplier SET Status=0 WHERE ID=" + sID;
//NOW RUN EXECUTENONQUERY METHOD OR WHATEVER YOU WANT. ID NOW AVAILABLE.
}

foreach (GridViewRow oItem in gvRight.Rows)
{
string sID = gvRight.DataKeys[oItem.DataItemIndex].Value.ToString();
string sSQL = "UPDATE Supplier SET Status=1 WHERE ID=" + sID;
//NOW RUN EXECUTENONQUERY METHOD OR WHATEVER YOU WANT. ID NOW AVAILABLE.
}

}

Selecting all checkboxes within a gridview also an important task which i already described in the following post:
http://shawpnendu.blogspot.com/2009/02/selecting-all-check-boxes-in-gridview.html

4 comments:

MANIVANNAN said...

i have gridview(gvLeft) from objdatasource(pmv)where a database table(a) has the values already. what i need is the same process call from database (a) table:supplier
(1, "st0001", "S.R. Steel");
(2, "ir0039", "Shadesh builders");
(3, "cr0042", "Orchard confec.");
(4, "er0078", "Windblow");
(5, "bd0301", "Rahimkarim");
output alike
http://shawpnendu.blogspot.com/2009/03/transfer-records-from-one-gridview-to.html

Anonymous said...

good day. i have a question. is it possible to save the second gridview's records (the gvright)to a different database?

for example the first gridview is the product table, then the second gridview(the products to be selected) it will be saved on another table, in order table.

is that possible? i've been studying your code for almost 1 week and i really need that function. can you help me sir?

Madhu Mangalagiri said...

ThanQ Sir

Anonymous said...

i want to kept my original data also in first grid view

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash