Saturday, February 8, 2014

Creating Dataset Dynamically in c#

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView ID="GridVwRowColorchange" runat="server" AutoGenerateColumns="False"
                Font-Names="Verdana" PageSize="5" Width="75%"
                BorderColor="#CCCCCC" BorderStyle="Solid"
                BorderWidth="1px" onrowdatabound="GridVwRowColorchange_RowDataBound" >
                <AlternatingRowStyle BackColor="#BFE4FF" />
                <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
                <HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
                    BorderStyle="Solid" BorderWidth="1px" />
                <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
                    BorderWidth="1px" />
                <Columns>
                    <asp:BoundField DataField="Emp_Name" HeaderText="Employee Name"  />
                    <asp:BoundField DataField="Emp_id" HeaderText="Employee ID"  />
                    <asp:BoundField DataField="Emp_job" HeaderText="Job title"  />
                    <asp:BoundField DataField="Emp_Dep" HeaderText="Department"  />
                </Columns>
            </asp:GridView>
    </div>
    </form>
</body>
</html>


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 Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Emp_Name");
        dt.Columns.Add("Emp_id");
        dt.Columns.Add("Emp_job");
        dt.Columns.Add("Emp_Dep");
        DataRow rw1 = dt.NewRow();
        rw1["Emp_Name"] = "Narain Sidharth";
        rw1["Emp_id"] = "2020";
        rw1["Emp_job"] = "SOftware Engineer";
        rw1["Emp_Dep"] = "IT";
        dt.Rows.Add(rw1);
        DataRow rw2 = dt.NewRow();
        rw2["Emp_Name"] = "Prakalathan";
        rw2["Emp_id"] = "1978";
        rw2["Emp_job"] = "System Engineer";
        rw2["Emp_Dep"] = "IT";
        dt.Rows.Add(rw2);
        DataRow rw3 = dt.NewRow();
        rw3["Emp_Name"] = "Mathu kumar";
        rw3["Emp_id"] = "2700";
        rw3["Emp_job"] = "Support Enginner";
        rw3["Emp_Dep"] = "IT";
        dt.Rows.Add(rw3);
        DataRow rw4 = dt.NewRow();
        rw4["Emp_Name"] = "Arvind";
        rw4["Emp_id"] = "4678";
        rw4["Emp_job"] = "Sr Software Engineer";
        rw4["Emp_Dep"] = "IT";
        dt.Rows.Add(rw4);
        DataRow rw5 = dt.NewRow();
        rw5["Emp_Name"] = "Raja ram";
        rw5["Emp_id"] = "2300";
        rw5["Emp_job"] = "Test Engineer";
        rw5["Emp_Dep"] = "IT";
        dt.Rows.Add(rw5);
        GridVwRowColorchange.DataSource = dt;
        GridVwRowColorchange.DataBind();
    }
    protected void GridVwRowColorchange_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "self.MouseOverOldColor=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=self.MouseOverOldColor");
        }
    }
}

0 comments:

Post a Comment