Friday 13 July 2012

in ASP.NET copy cell value from one gridview and paste to another gridview


Introduction

Here I explained how to get and Set the ASP.Net Grid View Row value to other Grid View Row client side using JavaScript. This feature using Click on cell 2 value of grid view and then click on other grid view at any ROW then this value is copy the Second Grid View. And also this process also for second grid copy value from CELL 2 using click on cell then paste this value on first Gird view using click on first gird view.


 
<%@ Page Language="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Copy value from one grid to other gird viewtitle>
    <style type="text/css">
        table.tableizer-table
        {
            border: 1px solid #CCC;
            font-family: Arial, Helvetica,sans-serif;
            font-size: 12px;
        }
        .tableizer-tabletd
        {
            padding: 4px;
            margin: 3px;
            border: 1px solid #ccc;
        }
        .tableizer-tableth
        {
            background-color: #104E8B;
            color: #FFF;
            font-weight: bold;
        }
        .style3
        {
            width: 258px;
        }
    style>

    <script type="text/javascript">
     functionaddhiddenfieldValue(cellvalue,portvalue)
     {      
        if(cellvalue == 2)
        {
            document.getElementById('hdnfsplicvalue').value=portvalue;
        }
        else
        {
            varSpliceValue = document.getElementById('hdnfsplicvalue').value;
            if(SpliceValue != '')
            {
                varobjCell =  document.getElementById(portvalue);
                if(objCell)
                {
                    objCell.value = SpliceValue;                   
                    document.getElementById('hdnfsplicvalue').value = '';
                }
            }
        }      
     }
     functionsetReadonly()
     {
      vara=document.getElementsByTagName('input');
      for(i=0;i<=a.length;i++)
      {
        if(a[i])
        {
          a[i].readOnly=true;
         }   
      }    
     }
    script>

head>
<body>
    <form id="form1" runat="server">
    <div class="tableizer-table">
        <asp:HiddenField ID="hdnfsplicvalue" runat="server" />
        <table style="width: 42%; height: 393px;" class="tableizer-table"align="center">
            <tr>
                <td valign="top" id="from" runat="server">
                    First Grid
                    <br />
                    <asp:GridView runat="server" ID="GrdFrom" AutoGenerateColumns="false" Height="363px"
                        OnRowDataBound="GrdFrom_RowDataBound">
                        <Columns>
                            <asp:TemplateField HeaderText="From"SortExpression="From">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtFrom"BorderColor="#999999"BorderWidth="0"BorderStyle="None"
                                        runat="server"Text='<%# Bind("From") %>' Width="80px" Wrap="true">asp:TextBox>
                                ItemTemplate>
                            asp:TemplateField>
                            <asp:BoundField DataField="Port"HeaderText="Port"HeaderStyle-Width="80px">
                                <HeaderStyle Width="80px">HeaderStyle>
                            asp:BoundField>
                            <asp:TemplateField HeaderText="To"SortExpression="To">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtTo" BorderColor="#999999"BorderWidth="0"BorderStyle="None"
                                        runat="server"Text='<%# Bind("To") %>' Width="80px" Wrap="true">asp:TextBox>
                                ItemTemplate>
                            asp:TemplateField>
                        Columns>
                    asp:GridView>
                td>
                <td valign="top" id="section" runat="server">
                    Second Grid
                    <br />
                    <asp:GridView runat="server" ID="grdSection" AutoGenerateColumns="false" Height="362px"
                        OnRowDataBound="grdSection_RowDataBound">
                        <Columns>
                            <asp:TemplateField HeaderText="From"SortExpression="From">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtFrom"BorderColor="#999999"BorderWidth="0"BorderStyle="None"
                                        runat="server"Text='<%# Bind("From") %>' Width="80px" Wrap="true">asp:TextBox>
                                ItemTemplate>
                            asp:TemplateField>
                            <asp:BoundField DataField="Port"HeaderText="Port"HeaderStyle-Width="80px">
                                <HeaderStyle Width="80px">HeaderStyle>
                            asp:BoundField>
                            <asp:TemplateField HeaderText="To"SortExpression="To">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtTo" BorderColor="#999999"BorderWidth="0"BorderStyle="None"
                                        runat="server"Text='<%# Bind("To") %>' Width="80px" Wrap="true">asp:TextBox>
                                ItemTemplate>
                            asp:TemplateField>
                        Columns>
                    asp:GridView>
                td>
            tr>
        table>
    div>
    form>
body>
html>

using System;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(objectsender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.BindGridView();
        }
    }

    private void BindGridView()
    {
        DataTable_dtData = new DataTable();
        _dtData.Columns.Add("From");
        _dtData.Columns.Add("Port");
        _dtData.Columns.Add("To");      
        for (int i = 0; i < 5; i++)
        {
            _dtData.Rows.Add("","From-Port-"+i,"");
        }
        GrdFrom.DataSource = _dtData;
        GrdFrom.DataBind();
        DataTable_dtData1 = new DataTable();
        _dtData1.Columns.Add("From");
        _dtData1.Columns.Add("Port");
        _dtData1.Columns.Add("To");
        for (int i = 0; i < 5; i++)
        {
            _dtData1.Rows.Add("", "To-Port-"+ i, "");
        }
        grdSection.DataSource = _dtData1;
        grdSection.DataBind();
    }
   protected void GrdFrom_RowDataBound(objectsender, GridViewRowEventArgs e)
    {
        DataRowViewrow = e.Row.DataItem as DataRowView;
        if(e.Row.RowType == DataControlRowType.DataRow)
        {

      TextBox cell1value = e.Row.Cells[0].FindControl("txtFrom") asTextBox;
      TextBox cell3value = e.Row.Cells[2].FindControl("txtTo") asTextBox;
      e.Row.Cells[0].Attributes.Add("onclick", "addhiddenfieldValue('1','"+ cell1value.ClientID + "');");
       e.Row.Cells[1].Attributes.Add("onclick", "addhiddenfieldValue('2','"+ e.Row.Cells[1].Text + "');");
       e.Row.Cells[2].Attributes.Add("onclick", "addhiddenfieldValue('3','"+ cell3value.ClientID + "');");
        }
    }
    protected void grdSection_RowDataBound(objectsender, GridViewRowEventArgs e)
    {
        DataRowViewrow = e.Row.DataItem as DataRowView;
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
       TextBoxcell1value = e.Row.Cells[0].FindControl("txtFrom") as TextBox;
       TextBoxcell3value = e.Row.Cells[2].FindControl("txtTo") as TextBox;
       e.Row.Cells[0].Attributes.Add("onclick", "addhiddenfieldValue('1','"+ cell1value.ClientID + "');");
       e.Row.Cells[1].Attributes.Add("onclick", "addhiddenfieldValue('2','"+ e.Row.Cells[1].Text + "');");
        e.Row.Cells[2].Attributes.Add("onclick", "addhiddenfieldValue('3','"+ cell3value.ClientID + "');");
        }
    }

}
  

Description

I got requirement like Copy Cell value from One Grid View and Paste this value to Second Grid View cell using Client Site JavaScript so I implemented this code for this activity.

Downloads
You can download the complete source code in C# 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger