Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections;
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 Default16 : System.Web.UI.Page
{
    int result1, result2, result3;
    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;

        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));
        dt.Columns.Add(new DataColumn("Column6", typeof(string)));
        dr = dt.NewRow();

        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dr["Column5"] = string.Empty;
        dr["Column6"] = string.Empty;

        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();

        DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("DropDownList1");
        FillDropDownList(ddl1);
        DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("DropDownList2");
        FillDropDownList1(ddl2);

    }
    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int icount, count;
            icount = Gridview1.Rows.Count;
            count = Gridview1.Rows.Count;
            if (icount==1)
            {
                icount = 0;
            }
                   
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = icount; i <= count; i++)
                {
                    //extract the TextBox values
                    if (i==0)
                    {
                        dtCurrentTable.Rows[0]["RowNumber"] = i+1;
                        dtCurrentTable.Rows[0]["Column1"] = string.Empty;
                        dtCurrentTable.Rows[0]["Column2"] = string.Empty;
                        dtCurrentTable.Rows[0]["Column3"] = string.Empty;
                        dtCurrentTable.Rows[0]["Column4"] = string.Empty;
                        dtCurrentTable.Rows[0]["Column5"] = string.Empty;
                        dtCurrentTable.Rows[0]["Column6"] = string.Empty;
                        //Button b1 = (Button)Gridview1.Rows[rowIndex].Cells[6].FindControl("ButtonAdd123");
                        //b1.Visible = true;
                    }
                    else
                    {
                        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox3");
                        TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox4");
                        DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList1");
                        DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList2"); 

                        //Button b2 = (Button)Gridview1.Rows[rowIndex].Cells[7].FindControl("ButtonAdd123");
                        //b2.Visible = false;

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        dtCurrentTable.Rows[i]["Column1"] = box1.Text;
                        dtCurrentTable.Rows[i]["Column2"] = ddl1.SelectedItem.Text;
                        dtCurrentTable.Rows[i]["Column3"] = ddl2.SelectedItem.Text;
                        dtCurrentTable.Rows[i]["Column4"] = box2.Text;
                        dtCurrentTable.Rows[i]["Column5"] = box3.Text;
                        dtCurrentTable.Rows[i]["Column6"] = box4.Text;
                        rowIndex++;
                    }
                }
                //dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        //Set Previous Data on Postbacks
        SetPreviousData();
    }

    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox4");
                    DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList1");
                    DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList2");

                    FillDropDownList(ddl1);
                    FillDropDownList1(ddl2); 

                    box1.Text = dt.Rows[i]["Column1"].ToString();

                    ddl1.ClearSelection();

                    if (ddl1.Items.FindByText(dt.Rows[i]["Column2"].ToString()) != null)
                    {
                        ddl1.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true;
                    }
                    if (ddl2.Items.FindByText(dt.Rows[i]["Column3"].ToString()) != null)
                    {
                        ddl2.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true;
                    }
                    box2.Text = dt.Rows[i]["Column4"].ToString();
                    box3.Text = dt.Rows[i]["Column5"].ToString();
                    box4.Text = dt.Rows[i]["Column6"].ToString();
                    Button b2 = (Button)Gridview1.Rows[rowIndex].Cells[7].FindControl("ButtonAdd123");

                    if (i == 0)
                    {
                        b2.Visible = true;
                        box1.ReadOnly = false ;
                        box2.ReadOnly = false;
                        box3.ReadOnly = false;
                        box4.ReadOnly = false;
                        ddl1.Enabled = true ;
                        ddl2.Enabled = true;
                    }
                    else
                    {
                        b2.Visible = false;
                        box1.ReadOnly = true;
                        box2.ReadOnly = true;
                        box3.ReadOnly = true;
                        box4.ReadOnly = true;
                        ddl1.Enabled = false;
                        ddl2.Enabled = false;
                    }
                    rowIndex++;
                }
            }
        }
    }


    private ArrayList GetDummyData()
    {
        ArrayList arr = new ArrayList();
        arr.Add(new ListItem("Item1","1"));
  皜     arr.Add(new ListItem("Item2","2"));
        arr.Add(new ListItem("Item3","3"));
        arr.Add(new ListItem("Item4","4"));
        arr.Add(new ListItem("Item5","5"));
        return arr;
    }

    private void FillDropDownList(DropDownList ddl)
    {
        ArrayList arr = GetDummyData();

        foreach (ListItem item in arr)
        {
            ddl.Items.Add(item);
        }
    }

    private ArrayList GetDummyData1()
    {
        ArrayList arr = new ArrayList();
        arr.Add(new ListItem("KGS","1"));
        arr.Add(new ListItem("NOS","2"));
        arr.Add(new ListItem("LITRE","3"));
        arr.Add(new ListItem("PACKS","4"));
        arr.Add(new ListItem("BALE","5"));
        return arr;
    }

    private void FillDropDownList1(DropDownList ddl)
    {
        ArrayList arr = GetDummyData1();
        foreach (ListItem item in arr)
        {
            ddl.Items.Add(item);
        }
    } 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }

    //protected void ButtonAdd_Click(object sender, EventArgs e)
    //{
    //    AddNewRowToGrid();
    //}
    //protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    string cmdarg;
    //     int rowIndex = 0;
    //    cmdarg = e.CommandArgument.ToString();
    //    Button b2 = (Button)Gridview1.Rows[rowIndex].Cells[4].FindControl("ButtonAdd123");

    //    if (cmdarg == "1")
    //    {
    //        b2.Visible = true;
    //    }
    //    else
    //    {

    //        b2.Visible =false  ;
    //    }

    //}
    protected void ButtonAdd123_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();     
    }
    
    protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox TextBox2 = (TextBox)e.Row.FindControl("TextBox2");
            TextBox TextBox3 = (TextBox)e.Row.FindControl("TextBox3");
            TextBox TextBox4 = (TextBox)e.Row.FindControl("TextBox4");
        }
    }

        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    TextBox TextBox2 = (TextBox)e.Row.FindControl("TextBox2");
        //    TextBox TextBox3 = (TextBox)e.Row.FindControl("TextBox3");
        //    TextBox TextBox4 = (TextBox)e.Row.FindControl("TextBox4");
        //    string id = e.Row.Cells[4].Text;
        //    TextBox2.Text = id;
        //    string id2 = e.Row.Cells[5].Text;
        //    TextBox3.Text = id2;
        //    int mult = int.Parse(id) * int.Parse(id2);
        //    TextBox4.Text = mult;
        //    e.Row.Cells[e.Row.Cells.Count - 1].Text = mult.ToString();
        //}


    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        try
        {
            int value;
            for (int i = 0; i < Gridview1.Rows.Count; i++)
            {
                GridViewRow row = Gridview1.Rows[i];
                TextBox TextBox2 = (TextBox)row.FindControl("TextBox2");
                TextBox TextBox3 = (TextBox)row.FindControl("TextBox3");
                TextBox TextBox4 = (TextBox)row.FindControl("TextBox4");
                value = Convert.ToInt32(TextBox2.Text) * Convert.ToInt32(TextBox3.Text);
                TextBox4.Text = Convert.ToString(value);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}
Posted
Updated 2-Dec-10 21:10pm
v3
Comments
Manfred Rudolf Bihy 3-Dec-10 3:09am    
Would you care to ask a question?
Manfred Rudolf Bihy 3-Dec-10 3:11am    
Added ASP.NET tag, guessing "Futures" was meant to be "Features"

Hey man, may I know what are you doing? Also found your other 3 questions. You are posting your content here as a question. You may post that as a Tips/Trick[^] or Blog[^]
 
Share this answer
 
Give us a clue.

Is this posted because you have a problem?

If so Please ask the question.

If not, then best to post it somewhere else.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900