Breaking News

Editors Picks

Friday, July 13, 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-table td
        {
            padding: 4px;
            margin: 3px;
            border: 1px solid #ccc;
        }
        .tableizer-table th
        {
            background-color: #104E8B;
            color: #FFF;
            font-weight: bold;
        }
        .style3
        {
            width: 258px;
        }
    style>

    <script type="text/javascript">
     function addhiddenfieldValue(cellvalue,portvalue)
     {      
        if(cellvalue == 2)
        {
            document.getElementById('hdnfsplicvalue').value=portvalue;
        }
        else
        {
            var SpliceValue = document.getElementById('hdnfsplicvalue').value;
            if(SpliceValue != '')
            {
                var objCell =  document.getElementById(portvalue);
                if(objCell)
                {
                    objCell.value = SpliceValue;                   
                    document.getElementById('hdnfsplicvalue').value = '';
                }
            }
        }      
     }
     function setReadonly()
     {
      var a=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;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, 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(object sender, GridViewRowEventArgs e)
    {
        DataRowView row = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

      TextBox cell1value = e.Row.Cells[0].FindControl("txtFrom") as TextBox;
      TextBox cell3value = 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 + "');");
        }
    }
    protected void grdSection_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView row = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
       TextBox cell1value = e.Row.Cells[0].FindControl("txtFrom") as TextBox;
       TextBox cell3value = 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# 

No comments :

Post a Comment

Contact Us

Name

Email *

Message *