Click here to Skip to main content
15,038,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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);

        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++)
                {
                    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;
                        
                    }
                    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"); 
                       
                        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++;
                    }
                }
                ViewState["CurrentTable"] = dtCurrentTable;
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        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 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");
        }
    }



    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 22:18pm
v2
Comments
Sunasara Imdadhusen 3-Dec-10 4:20am
   
Please use PRE tag for readability!
Sunasara Imdadhusen 3-Dec-10 4:20am
   
What is your problem?

1 solution

That is a good code, well done.

Or have you got a question?

I think you either need to phrase a question or maybe submit this is a Tip/Trick or Article.

I suggest you read the submissions FAQs.

(Or work out what you want to ask)

People will help you, but a code dump just antagonises.
   

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