Tuesday, 8 May 2012

To Create Dynamic Rows in Gridview using Template Column in asp.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace IEB
{
    public partial class Budget : System.Web.UI.Page
    {
        DataTable dtBudgetCategory;
        DataTable dtResult;
     
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMonth.Text = System.DateTime.Now.Month.ToString();
            lblYear.Text = System.DateTime.Now.Year.ToString();
            if (!IsPostBack)
            {
              
                BindGrid(GetRows());
            }
            else
                dtResult = (DataTable)ViewState["Budget"];
        }
        private void BindGrid(DataTable Data)
        {
            gvBudget.DataSource = Data;
            gvBudget.DataBind();
        }
        private DataTable GetRows()
        {
            dtResult = new DataTable();
            dtResult.Columns.Add(new DataColumn("Category"));
            dtResult.Columns.Add(new DataColumn("Amount"));
            for (int i = 0; i < 5; i++)
            {
                dtResult.Rows.Add(dtResult.NewRow());
            }
         
            return dtResult;
        }
        protected void gvBudget_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;
            DropDownList ddlcategory = (DropDownList)e.Row.FindControl("cboCategory");
            ddlcategory.DataSource = GetBudgetCategory();
            ddlcategory.DataValueField = "CategoryID";
            ddlcategory.DataTextField = "Category";
            ddlcategory.DataBind();
            if(((System.Data.DataRowView)(e.Row.DataItem)) != null)
                ddlcategory.SelectedValue = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[0].ToString();
        }
        private DataTable GetBudgetCategory()
        {
            if (dtBudgetCategory == null || dtBudgetCategory.Rows.Count == 0)
            {
                dtBudgetCategory = objExpansesBL.CategoryList();
                return dtBudgetCategory;
            }
            return dtBudgetCategory;
           
           
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            UpdateRowToDataTable();
            dtResult.Rows.Add(dtResult.NewRow());
          
            BindGrid(dtResult);
        }
       
        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["Budget"] = dtResult;
        }
        private void UpdateRowToDataTable()
        {
        foreach(GridViewRow row in gvBudget.Rows)
        {
            DropDownList ddlcategory = (DropDownList)row.FindControl("cboCategory");
            TextBox txtAmount = (TextBox)row.FindControl("txtAmount");
            dtResult.Rows[row.RowIndex]["Category"] = ddlcategory.SelectedValue;
            dtResult.Rows[row.RowIndex]["Amount"] = txtAmount.Text ;
        }
        }
          }
}

No comments:

Post a Comment