Monday, 18 January 2016

Update Datatable using Linq in C# .Net

Update Datatable using Linq in C#

Description:-

In this example we explain that how to update datatable using LINQ in C# .Net. Or update datatable column value from other datatable using linq.

Here we explain with simply data table you can also use it with database.

Suppose you have two datatable in which first datatable contain employee id and employee name and second data table contain salary for each employee.so in this situation if you want to display employee name with salary in a single datatable or any data control you must have to use the following code like:


Default.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="WebApplication1_WebApplication1_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>Update Datatable using Linq in C# </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Default.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class WebApplication1_WebApplication1_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable _dtMaster = dt();
        _dtMaster.Columns.Add("Salary");
        DataTable _dtChild = dttt();
        _dtMaster.AsEnumerable().Join(_dtChild.AsEnumerable(), _dtmater => Convert.ToString(_dtmater["id"]), _dtchild => Convert.ToString(_dtchild["id"]), (_dtmater, _dtchild) => new { _dtmater, _dtchild }).ToList().ForEach(o => o._dtmater.SetField("Salary", o._dtchild["Salary"].ToString()));
        GridView1.DataSource = _dtMaster;
        GridView1.DataBind();
    }
    //Master table-----------------
    private DataTable dt()
    {
        DataTable _dtt = new DataTable();
        _dtt.Columns.Add("id");
        _dtt.Columns.Add("Name");
        _dtt.Rows.Add("1", "Kirit");
        _dtt.Rows.Add("2", "Pintu");
        _dtt.Rows.Add("3", "Rahul");
        _dtt.Rows.Add("4", "Amit");
        _dtt.Rows.Add("5", "Prakash");
        return _dtt;
    }
    //Child table---------------
    private DataTable dttt()
    {
        DataTable _dtt = new DataTable();
        _dtt.Columns.Add("id");
        _dtt.Columns.Add("Name");
        _dtt.Columns.Add("Salary");
        _dtt.Rows.Add("1", "Kirit", 24000);
        _dtt.Rows.Add("2", "Pintu", 20000);
        _dtt.Rows.Add("3", "Rahul", 5000);
        return _dtt;
    }
}


0 comments:

Post a Comment