Wednesday, 13 June 2012

Adding Datatable to HTML Table programatically and Binding the dropdowns using Dictionary

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            ddlLocation.DataSource = BindddlLocation();
            ddlLocation.DataTextField = "Value";
            //ddlLocation.DataValueField = "Key";
            ddlLocation.DataBind();

            ddlDepartment.DataSource = BindddlDepartment();
            ddlDepartment.DataTextField = "Value";
            // ddlDepartment.DataValueField = "Key";
            ddlDepartment.DataBind();
        }

    }
    public Dictionary<int, string> BindddlLocation()
    {
        Dictionary<int, string> dic = new Dictionary<int, string>();

        dic.Add(0, "-----");
        dic.Add(1, "BLR");
        dic.Add(2, "HYD");
        dic.Add(3, "CHN");
        dic.Add(4, "PUN");

        return dic;     
       
    }

    public Dictionary<int, string> BindddlDepartment()
    {
        Dictionary<int, string> dep = new Dictionary<int, string>();

        dep.Add(0, "-----");
        dep.Add(1, "BAS");
        dep.Add(2, "SAP");
        dep.Add(3, "VVT");
        dep.Add(4, "BI");

        return dep;
    }

    public static string ConvertDataTableToHtml(DataTable targetTable)
    {
        string htmlString = "";

        if (targetTable == null)
        {
            throw new System.ArgumentNullException("targetTable");
        }

        StringBuilder htmlBuilder = new StringBuilder();

        //Create Top Portion of HTML Document
        htmlBuilder.Append("<html>");
        htmlBuilder.Append("<head>");
        htmlBuilder.Append("<title>");
        htmlBuilder.Append("Page-");
        htmlBuilder.Append(Guid.NewGuid().ToString());
        htmlBuilder.Append("</title>");
        htmlBuilder.Append("</head>");
        htmlBuilder.Append("<body>");
        htmlBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
        htmlBuilder.Append("style='border: solid 1px Black; font-size: small;'>");

        //Create Header Row
        htmlBuilder.Append("<tr align='left' valign='top'>");

        foreach (DataColumn targetColumn in targetTable.Columns)
        {
            htmlBuilder.Append("<td align='left' valign='top'>");
            htmlBuilder.Append(targetColumn.ColumnName);
            htmlBuilder.Append("</td>");
        }

        htmlBuilder.Append("</tr>");

        //Create Data Rows
        foreach (DataRow myRow in targetTable.Rows)
        {
            htmlBuilder.Append("<tr align='left' valign='top'>");

            foreach (DataColumn targetColumn in targetTable.Columns)
            {
                htmlBuilder.Append("<td align='left' valign='top'>");
                htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
                htmlBuilder.Append("</td>");
            }

            htmlBuilder.Append("</tr>");
        }

        //Create Bottom Portion of HTML Document
        htmlBuilder.Append("</table>");
        htmlBuilder.Append("</body>");
        htmlBuilder.Append("</html>");

        //Create String to be Returned
        htmlString = htmlBuilder.ToString();
     

        return htmlString;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
        DataTable tab=new DataTable();
        tab =(DataTable) Session["Data"];
        if (tab==null)
        {
            tab = AddtoDataTable();
            Label1.Text = ConvertDataTableToHtml(tab);
        }
        else
        {
            DataTable dt = BindValues();
            Label1.Text = ConvertDataTableToHtml(dt);
        }
    }
    public DataTable BindValues()
    {
        DataTable dtNew = (DataTable)Session["Data"];

        DataRow dr = dtNew.NewRow();

        dr["Name"] = txtName.Text.ToString();
        dr["Email"] = txtEmail.Text.ToString();
        dr["Location"] = ddlLocation.SelectedValue;
        dr["Department"] = ddlDepartment.SelectedValue;
        dtNew.Rows.Add(dr);
        Session["dtN"] = dtNew;

        return dtNew;
    }

    public DataTable AddtoDataTable()
    {
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn();
        dt.Columns.Add("Name");
        dt.Columns.Add("Email");
        dt.Columns.Add("Location");
        dt.Columns.Add("Department");

        DataRow dr=dt.NewRow();

        dr["Name"] = txtName.Text.ToString();
        dr["Email"] = txtEmail.Text.ToString();
        dr["Location"] = ddlLocation.SelectedValue;
        dr["Department"] = ddlDepartment.SelectedValue;

        dt.Rows.Add(dr);

        Session["Data"] = dt;


        return dt;
      
       
    }
}

No comments:

Post a Comment