[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Column 'RowNumber' does not belong to table Table1.

Posted on 2011-03-03
6
Medium Priority
?
2,351 Views
Last Modified: 2012-05-11
I am generating a row dynamically on my Gridview, I get this error


Column 'RowNumber' does not belong to table Table1.
.aspx
      <asp:gridview ID="Gridview1" runat="server" ShowFooter="True" 
            AutoGenerateColumns="False" onrowcreated="Gridview1_RowCreated" 
            EnableModelValidation="True" CssClass = "GridViewStyle" HeaderStyle-CssClass ="HeaderStyle" 
RowStyle-CssClass ="RowStyle" AlternatingRowStyle-CssClass = "AltRowStyle"  RowStyle-Width="300px">
            
            <Columns>
         
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number"/>
            <asp:TemplateField  >
               <HeaderTemplate  >
               <asp:Label ID="Label1" runat="server">NT-1</asp:Label>
            
            <asp:TextBox ID="txt_Census1" runat="server" Width="30px" ></asp:TextBox>
            </HeaderTemplate>


                <ItemTemplate>
                    Bed:
                    <asp:TextBox ID="txt_Bed1" runat="server" Width="30px"></asp:TextBox>
                    Status:
                    <asp:DropDownList ID="txt_Comment1" runat="server" AppendDataBoundItems="True" 
                    style="text-align: left" >
                    <asp:ListItem value="0" text="Select">  </asp:ListItem>
                    <asp:ListItem>Assigned</asp:ListItem>
                    <asp:ListItem>BR</asp:ListItem>
                    <asp:ListItem>Open NS</asp:ListItem>
                                      
                </asp:DropDownList>

                </ItemTemplate>
            </asp:TemplateField>


.cs

public partial class GridViewWithTextBoxes : System.Web.UI.Page
{

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;

        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));


        dt.Columns.Add(new DataColumn("NT1", typeof(string)));
        dt.Columns.Add(new DataColumn("NT1_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("NT2", typeof(string)));
        dt.Columns.Add(new DataColumn("NT2_Status", typeof(string)));



        dt.Columns.Add(new DataColumn("NT3", typeof(string)));
        dt.Columns.Add(new DataColumn("NT3_Status", typeof(string)));


        // dt.Columns.Add(new DataColumn("NT3_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("ICU", typeof(string)));
        dt.Columns.Add(new DataColumn("ICU_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("SDU", typeof(string)));
        dt.Columns.Add(new DataColumn("SDU_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele3rd1", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele3rd1_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele3rd2", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele3rd2_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele4th1", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele4th1_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele4th2", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele4th2_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Pedi3rd", typeof(string)));
        dt.Columns.Add(new DataColumn("Pedi3rd_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Pedi4th", typeof(string)));
        dt.Columns.Add(new DataColumn("Pedi4th_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("PICU_SD", typeof(string)));
        dt.Columns.Add(new DataColumn("PICU_SD_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("ER", typeof(string)));
        dt.Columns.Add(new DataColumn("ER_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("MICU", typeof(string)));
        dt.Columns.Add(new DataColumn("MICU_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CPV", typeof(string)));
        dt.Columns.Add(new DataColumn("CPV_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CVOR", typeof(string)));
        dt.Columns.Add(new DataColumn("CVOR_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CLOF", typeof(string)));
        dt.Columns.Add(new DataColumn("CLOF_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CVOR_HOLDING", typeof(string)));
        dt.Columns.Add(new DataColumn("CVOR_HOLDING_Status", typeof(string)));



        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["NT1"] = string.Empty;
        dr["NT2"] = string.Empty;
        dr["NT3"] = string.Empty;
        dr["ICU"] = string.Empty;
        dr["SDU"] = string.Empty;
        dr["Tele3rd1"] = string.Empty;
        dr["Tele3rd2"] = string.Empty;
        dr["Tele4th1"] = string.Empty;
        dr["Tele4th2"] = string.Empty;
        dr["Pedi3rd"] = string.Empty;
        dr["Pedi4th"] = string.Empty;
        dr["PICU_SD"] = string.Empty;
        dr["ER"] = string.Empty;
        dr["MICU"] = string.Empty;
        dr["CPV"] = string.Empty;
        dr["CVOR"] = string.Empty;
        dr["CLOF"] = string.Empty;
        dr["CVOR_HOLDING"] = string.Empty;

        dt.Rows.Add(dr);
        //dr = dt.NewRow();

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

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

    }


    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox  and DropDown values
                    

                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Bed1");
                    DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Comment1");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Bed2");
                    DropDownList box4 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Comment2");

                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Bed3");
                    DropDownList box6 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Comment3");

                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Bed4");
                    DropDownList box8 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Comment4");

                    TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Bed5");
                    DropDownList box10 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Comment5");

                    TextBox box11 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Bed6");
                    DropDownList box12 = (DropDownList)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Comment6");


                    TextBox box13 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Bed7");
                    DropDownList box14 = (DropDownList)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Comment7");

                    TextBox box15 = (TextBox)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Bed8");
                    DropDownList box16 = (DropDownList)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Comment8");

                    TextBox box17 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Bed9");
                    DropDownList box18 = (DropDownList)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Comment9");

                    TextBox box19 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Bed10");
                    DropDownList box20 = (DropDownList)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Comment10");


                    TextBox box21 = (TextBox)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Bed11");
                    DropDownList box22 = (DropDownList)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Comment11");

                    TextBox box23 = (TextBox)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Bed12");
                    DropDownList box24 = (DropDownList)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Comment12");

                    TextBox box25 = (TextBox)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Bed13");
                    DropDownList box26 = (DropDownList)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Comment13");

                    TextBox box27 = (TextBox)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Bed14");
                    DropDownList box28 = (DropDownList)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Comment14");

                    TextBox box29 = (TextBox)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Bed15");
                    DropDownList box30 = (DropDownList)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Comment15");

                    TextBox box31 = (TextBox)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Bed16");
                    DropDownList box32 = (DropDownList)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Comment16");

                    TextBox box33 = (TextBox)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Bed17");
                    DropDownList box34 = (DropDownList)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Comment17");


                    TextBox box35 = (TextBox)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Bed18");
                    DropDownList box36 = (DropDownList)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Comment18");


                    drCurrentRow = dtCurrentTable.NewRow();
                    
                    drCurrentRow["RowNumber"] = i + 1;

                    drCurrentRow["NT1"] = box1.Text;
                    drCurrentRow["NT1_Status"] = box2.SelectedValue;


                    drCurrentRow["NT2"] = box3.Text;
                    drCurrentRow["NT2_Status"] = box4.SelectedValue;


                    drCurrentRow["NT3"] = box5.Text;
                    drCurrentRow["NT3_Status"] = box6.SelectedValue;


                    drCurrentRow["ICU"] = box7.Text;
                    drCurrentRow["ICU_Status"] = box8.SelectedValue;

                    // drCurrentRow["SDU_Census"] = box1.Text;
                    drCurrentRow["SDU"] = box9.Text;
                    drCurrentRow["SDU_Status"] = box10.SelectedValue;

                    //drCurrentRow["Tele3rd1_Census"] = box1.Text;
                    drCurrentRow["Tele3rd1"] = box11.Text;
                    drCurrentRow["Tele3rd1_Status"] = box12.SelectedValue;

                    //drCurrentRow["Tele3rd2_Census"] = box1.Text;
                    drCurrentRow["Tele3rd2"] = box13.Text;
                    drCurrentRow["Tele3rd2_Status"] = box14.SelectedValue;

                    //drCurrentRow["Tele4th1_Census"] = box1.Text;
                    drCurrentRow["Tele4th1"] = box15.Text;
                    drCurrentRow["Tele4th1_Status"] = box16.SelectedValue;

                    //drCurrentRow["Tele4th2_Census"] = box1.Text;
                    drCurrentRow["Tele4th2"] = box17.Text;
                    drCurrentRow["Tele4th2_Status"] = box18.SelectedValue;

                    //drCurrentRow["Pedi3rd_Census"] = box1.Text;
                    drCurrentRow["Pedi3rd"] = box19.Text;
                    drCurrentRow["Pedi3rd_Status"] = box20.SelectedValue;

                    //drCurrentRow["Pedi4th_Census"] = box1.Text;
                    drCurrentRow["Pedi4th"] = box21.Text;
                    drCurrentRow["Pedi4th_Status"] = box22.SelectedValue;

                    // drCurrentRow["PICU_SD_Census"] = box1.Text;
                    drCurrentRow["PICU_SD"] = box23.Text;
                    drCurrentRow["PICU_SD_Status"] = box24.SelectedValue;

                    //drCurrentRow["ER_Census"] = box1.Text;
                    drCurrentRow["ER"] = box25.Text;
                    drCurrentRow["ER_Status"] = box26.SelectedValue;

                    // drCurrentRow["MICU_Census"] = box1.Text;
                    drCurrentRow["MICU"] = box27.Text;
                    drCurrentRow["MICU_Status"] = box28.SelectedValue;

                    //drCurrentRow["CPV_Census"] = box1.Text;
                    drCurrentRow["CPV"] = box29.Text;
                    drCurrentRow["CPV_Status"] = box30.SelectedValue;

                    //drCurrentRow["CVOR_Census"] = box1.Text;
                    drCurrentRow["CVOR"] = box31.Text;
                    drCurrentRow["CVOR_Status"] = box32.SelectedValue;

                    drCurrentRow["CLOF"] = box33.Text;
                    drCurrentRow["CLOF_Status"] = box34.SelectedValue;


                    //drCurrentRow["CVOR_HOLDING_Census"] = box1.Text;
                    drCurrentRow["CVOR_HOLDING"] = box35.Text;
                    drCurrentRow["CVOR_HOLDING_Status"] = box36.SelectedValue;

                    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 = 1; i < dt.Rows.Count; i++)
                {

                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Bed1");
                    DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Comment1");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Bed2");
                    DropDownList box4 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Comment2");

                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Bed3");
                    DropDownList box6 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Comment3");

                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Bed4");
                    DropDownList box8 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Comment4");

                    TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Bed5");
                    DropDownList box10 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Comment5");

                    TextBox box11 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Bed6");
                    DropDownList box12 = (DropDownList)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Comment6");


                    TextBox box13 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Bed7");
                    DropDownList box14 = (DropDownList)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Comment7");

                    TextBox box15 = (TextBox)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Bed8");
                    DropDownList box16 = (DropDownList)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Comment8");

                    TextBox box17 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Bed9");
                    DropDownList box18 = (DropDownList)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Comment9");

                    TextBox box19 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Bed10");
                    DropDownList box20 = (DropDownList)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Comment10");


                    TextBox box21 = (TextBox)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Bed11");
                    DropDownList box22 = (DropDownList)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Comment11");

                    TextBox box23 = (TextBox)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Bed12");
                    DropDownList box24 = (DropDownList)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Comment12");

                    TextBox box25 = (TextBox)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Bed13");
                    DropDownList box26 = (DropDownList)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Comment13");

                    TextBox box27 = (TextBox)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Bed14");
                    DropDownList box28 = (DropDownList)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Comment14");

                    TextBox box29 = (TextBox)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Bed15");
                    DropDownList box30 = (DropDownList)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Comment15");

                    TextBox box31 = (TextBox)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Bed16");
                    DropDownList box32 = (DropDownList)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Comment16");

                    TextBox box33 = (TextBox)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Bed17");
                    DropDownList box34 = (DropDownList)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Comment17");


                    TextBox box35 = (TextBox)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Bed18");
                    DropDownList box36 = (DropDownList)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Comment18");

                    // Assign Values to textboxes


                    box1.Text = dt.Rows[i]["NT1"].ToString();
                    box2.Text = dt.Rows[i]["NT1_Status"].ToString();


                    box3.Text = dt.Rows[i]["NT2"].ToString();
                    box4.Text = dt.Rows[i]["NT2_Status"].ToString();


                    box5.Text = dt.Rows[i]["NT3"].ToString();
                    box6.Text = dt.Rows[i]["NT3_Status"].ToString();


                    box7.Text = dt.Rows[i]["ICU"].ToString();
                    box8.Text = dt.Rows[i]["ICU_Status"].ToString();


                    box9.Text = dt.Rows[i]["SDU"].ToString();
                    box10.Text = dt.Rows[i]["SDU_Status"].ToString();


                    box11.Text = dt.Rows[i]["Tele3rd1"].ToString();
                    box12.Text = dt.Rows[i]["Tele3rd1_Status"].ToString();


                    box13.Text = dt.Rows[i]["Tele3rd2"].ToString();
                    box14.Text = dt.Rows[i]["Tele3rd2_Status"].ToString();


                    box15.Text = dt.Rows[i]["Tele4th1"].ToString();
                    box16.Text = dt.Rows[i]["Tele4th1_Status"].ToString();


                    box17.Text = dt.Rows[i]["Tele4th2"].ToString();
                    box18.Text = dt.Rows[i]["Tele4th2_Status"].ToString();


                    box19.Text = dt.Rows[i]["Pedi3rd"].ToString();
                    box20.Text = dt.Rows[i]["Pedi3rd_Status"].ToString();


                    box21.Text = dt.Rows[i]["Pedi4th"].ToString();
                    box22.Text = dt.Rows[i]["Pedi4th_Status"].ToString();


                    box23.Text = dt.Rows[i]["PICU_SD"].ToString();
                    box24.Text = dt.Rows[i]["PICU_SD_Status"].ToString();


                    box25.Text = dt.Rows[i]["ER"].ToString();
                    box26.Text = dt.Rows[i]["ER_Status"].ToString();


                    box27.Text = dt.Rows[i]["MICU"].ToString();
                    box28.Text = dt.Rows[i]["MICU_Status"].ToString();


                    box29.Text = dt.Rows[i]["CPV"].ToString();
                    box30.Text = dt.Rows[i]["CPV_Status"].ToString();


                    box31.Text = dt.Rows[i]["CVOR"].ToString();
                    box32.Text = dt.Rows[i]["CVOR_Status"].ToString();


                    box33.Text = dt.Rows[i]["CLOF"].ToString();
                    box34.Text = dt.Rows[i]["CLOF_Status"].ToString();


                    box35.Text = dt.Rows[i]["CVOR_HOLDING"].ToString();
                    box36.Text = dt.Rows[i]["CVOR_HOLDING_Status"].ToString();

                    rowIndex++;

                }
            }
            // ViewState["CurrentTable"] = dt;

        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string authUserName;
        authUserName = User.Identity.Name;

        /*If page is not a Postback */


        dateLabel.Text = DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm");


        if (!Page.IsPostBack)
        {
            SetInitialRow();
            SetInitialRow2();
        }
    }


    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }

Open in new window

0
Comment
Question by:TonyReba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35029965
I didn't see it but I didn't look super close. I think you are adding your row incorrectly.

This is just a snippet...

dt.LoadDataRow(new object[]{TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text});

Session["CACHEDATA1"] = dt;

GridView1.DataSource = dt;

GridView1.DataBind();

Open in new window


Additionally with your current code I think after you add the new DataRow to the DataTable you need to call DataTable.AcceptChanges();

This is pretty close to how I've done it without me actually writing it out:

http://forums.asp.net/t/1147158.aspx/1?Adding+rows+into+Gridview+in+C+Asp+net
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35030150
The problem I believe is u have two Gridviews in same form. Could the problem be the viewstate . Is used by both gridvuew. How do I fix this?
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35030670
If you see below I am trying to create two data tables one for each Gridview,   can the Viewstate be the problem?? and if so how can I fix it?
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;
using System.Collections.Specialized;
using System.Text;
using System.Data.SqlClient;

public partial class GridViewWithTextBoxes : System.Web.UI.Page
{

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;

        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));


        dt.Columns.Add(new DataColumn("NT1", typeof(string)));
        dt.Columns.Add(new DataColumn("NT1_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("NT2", typeof(string)));
        dt.Columns.Add(new DataColumn("NT2_Status", typeof(string)));



        dt.Columns.Add(new DataColumn("NT3", typeof(string)));
        dt.Columns.Add(new DataColumn("NT3_Status", typeof(string)));


        // dt.Columns.Add(new DataColumn("NT3_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("ICU", typeof(string)));
        dt.Columns.Add(new DataColumn("ICU_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("SDU", typeof(string)));
        dt.Columns.Add(new DataColumn("SDU_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele3rd1", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele3rd1_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele3rd2", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele3rd2_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele4th1", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele4th1_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Tele4th2", typeof(string)));
        dt.Columns.Add(new DataColumn("Tele4th2_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Pedi3rd", typeof(string)));
        dt.Columns.Add(new DataColumn("Pedi3rd_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("Pedi4th", typeof(string)));
        dt.Columns.Add(new DataColumn("Pedi4th_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("PICU_SD", typeof(string)));
        dt.Columns.Add(new DataColumn("PICU_SD_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("ER", typeof(string)));
        dt.Columns.Add(new DataColumn("ER_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("MICU", typeof(string)));
        dt.Columns.Add(new DataColumn("MICU_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CPV", typeof(string)));
        dt.Columns.Add(new DataColumn("CPV_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CVOR", typeof(string)));
        dt.Columns.Add(new DataColumn("CVOR_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CLOF", typeof(string)));
        dt.Columns.Add(new DataColumn("CLOF_Status", typeof(string)));


        dt.Columns.Add(new DataColumn("CVOR_HOLDING", typeof(string)));
        dt.Columns.Add(new DataColumn("CVOR_HOLDING_Status", typeof(string)));



        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["NT1"] = string.Empty;
        dr["NT2"] = string.Empty;
        dr["NT3"] = string.Empty;
        dr["ICU"] = string.Empty;
        dr["SDU"] = string.Empty;
        dr["Tele3rd1"] = string.Empty;
        dr["Tele3rd2"] = string.Empty;
        dr["Tele4th1"] = string.Empty;
        dr["Tele4th2"] = string.Empty;
        dr["Pedi3rd"] = string.Empty;
        dr["Pedi4th"] = string.Empty;
        dr["PICU_SD"] = string.Empty;
        dr["ER"] = string.Empty;
        dr["MICU"] = string.Empty;
        dr["CPV"] = string.Empty;
        dr["CVOR"] = string.Empty;
        dr["CLOF"] = string.Empty;
        dr["CVOR_HOLDING"] = string.Empty;

        dt.Rows.Add(dr);
        //dr = dt.NewRow();

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

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

    }


    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox  and DropDown values
                    

                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Bed1");
                    DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Comment1");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Bed2");
                    DropDownList box4 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Comment2");

                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Bed3");
                    DropDownList box6 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Comment3");

                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Bed4");
                    DropDownList box8 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Comment4");

                    TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Bed5");
                    DropDownList box10 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Comment5");

                    TextBox box11 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Bed6");
                    DropDownList box12 = (DropDownList)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Comment6");


                    TextBox box13 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Bed7");
                    DropDownList box14 = (DropDownList)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Comment7");

                    TextBox box15 = (TextBox)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Bed8");
                    DropDownList box16 = (DropDownList)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Comment8");

                    TextBox box17 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Bed9");
                    DropDownList box18 = (DropDownList)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Comment9");

                    TextBox box19 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Bed10");
                    DropDownList box20 = (DropDownList)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Comment10");


                    TextBox box21 = (TextBox)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Bed11");
                    DropDownList box22 = (DropDownList)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Comment11");

                    TextBox box23 = (TextBox)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Bed12");
                    DropDownList box24 = (DropDownList)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Comment12");

                    TextBox box25 = (TextBox)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Bed13");
                    DropDownList box26 = (DropDownList)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Comment13");

                    TextBox box27 = (TextBox)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Bed14");
                    DropDownList box28 = (DropDownList)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Comment14");

                    TextBox box29 = (TextBox)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Bed15");
                    DropDownList box30 = (DropDownList)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Comment15");

                    TextBox box31 = (TextBox)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Bed16");
                    DropDownList box32 = (DropDownList)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Comment16");

                    TextBox box33 = (TextBox)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Bed17");
                    DropDownList box34 = (DropDownList)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Comment17");


                    TextBox box35 = (TextBox)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Bed18");
                    DropDownList box36 = (DropDownList)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Comment18");


                    drCurrentRow = dtCurrentTable.NewRow();
                    
                    drCurrentRow["RowNumber"] = i + 1;

                    drCurrentRow["NT1"] = box1.Text;
                    drCurrentRow["NT1_Status"] = box2.SelectedValue;


                    drCurrentRow["NT2"] = box3.Text;
                    drCurrentRow["NT2_Status"] = box4.SelectedValue;


                    drCurrentRow["NT3"] = box5.Text;
                    drCurrentRow["NT3_Status"] = box6.SelectedValue;


                    drCurrentRow["ICU"] = box7.Text;
                    drCurrentRow["ICU_Status"] = box8.SelectedValue;

                    // drCurrentRow["SDU_Census"] = box1.Text;
                    drCurrentRow["SDU"] = box9.Text;
                    drCurrentRow["SDU_Status"] = box10.SelectedValue;

                    //drCurrentRow["Tele3rd1_Census"] = box1.Text;
                    drCurrentRow["Tele3rd1"] = box11.Text;
                    drCurrentRow["Tele3rd1_Status"] = box12.SelectedValue;

                    //drCurrentRow["Tele3rd2_Census"] = box1.Text;
                    drCurrentRow["Tele3rd2"] = box13.Text;
                    drCurrentRow["Tele3rd2_Status"] = box14.SelectedValue;

                    //drCurrentRow["Tele4th1_Census"] = box1.Text;
                    drCurrentRow["Tele4th1"] = box15.Text;
                    drCurrentRow["Tele4th1_Status"] = box16.SelectedValue;

                    //drCurrentRow["Tele4th2_Census"] = box1.Text;
                    drCurrentRow["Tele4th2"] = box17.Text;
                    drCurrentRow["Tele4th2_Status"] = box18.SelectedValue;

                    //drCurrentRow["Pedi3rd_Census"] = box1.Text;
                    drCurrentRow["Pedi3rd"] = box19.Text;
                    drCurrentRow["Pedi3rd_Status"] = box20.SelectedValue;

                    //drCurrentRow["Pedi4th_Census"] = box1.Text;
                    drCurrentRow["Pedi4th"] = box21.Text;
                    drCurrentRow["Pedi4th_Status"] = box22.SelectedValue;

                    // drCurrentRow["PICU_SD_Census"] = box1.Text;
                    drCurrentRow["PICU_SD"] = box23.Text;
                    drCurrentRow["PICU_SD_Status"] = box24.SelectedValue;

                    //drCurrentRow["ER_Census"] = box1.Text;
                    drCurrentRow["ER"] = box25.Text;
                    drCurrentRow["ER_Status"] = box26.SelectedValue;

                    // drCurrentRow["MICU_Census"] = box1.Text;
                    drCurrentRow["MICU"] = box27.Text;
                    drCurrentRow["MICU_Status"] = box28.SelectedValue;

                    //drCurrentRow["CPV_Census"] = box1.Text;
                    drCurrentRow["CPV"] = box29.Text;
                    drCurrentRow["CPV_Status"] = box30.SelectedValue;

                    //drCurrentRow["CVOR_Census"] = box1.Text;
                    drCurrentRow["CVOR"] = box31.Text;
                    drCurrentRow["CVOR_Status"] = box32.SelectedValue;

                    drCurrentRow["CLOF"] = box33.Text;
                    drCurrentRow["CLOF_Status"] = box34.SelectedValue;


                    //drCurrentRow["CVOR_HOLDING_Census"] = box1.Text;
                    drCurrentRow["CVOR_HOLDING"] = box35.Text;
                    drCurrentRow["CVOR_HOLDING_Status"] = box36.SelectedValue;

                    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 = 1; i < dt.Rows.Count; i++)
                {

                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Bed1");
                    DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Comment1");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Bed2");
                    DropDownList box4 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Comment2");

                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Bed3");
                    DropDownList box6 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Comment3");

                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Bed4");
                    DropDownList box8 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Comment4");

                    TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Bed5");
                    DropDownList box10 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Comment5");

                    TextBox box11 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Bed6");
                    DropDownList box12 = (DropDownList)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Comment6");


                    TextBox box13 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Bed7");
                    DropDownList box14 = (DropDownList)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Comment7");

                    TextBox box15 = (TextBox)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Bed8");
                    DropDownList box16 = (DropDownList)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Comment8");

                    TextBox box17 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Bed9");
                    DropDownList box18 = (DropDownList)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Comment9");

                    TextBox box19 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Bed10");
                    DropDownList box20 = (DropDownList)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Comment10");


                    TextBox box21 = (TextBox)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Bed11");
                    DropDownList box22 = (DropDownList)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Comment11");

                    TextBox box23 = (TextBox)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Bed12");
                    DropDownList box24 = (DropDownList)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Comment12");

                    TextBox box25 = (TextBox)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Bed13");
                    DropDownList box26 = (DropDownList)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Comment13");

                    TextBox box27 = (TextBox)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Bed14");
                    DropDownList box28 = (DropDownList)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Comment14");

                    TextBox box29 = (TextBox)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Bed15");
                    DropDownList box30 = (DropDownList)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Comment15");

                    TextBox box31 = (TextBox)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Bed16");
                    DropDownList box32 = (DropDownList)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Comment16");

                    TextBox box33 = (TextBox)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Bed17");
                    DropDownList box34 = (DropDownList)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Comment17");


                    TextBox box35 = (TextBox)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Bed18");
                    DropDownList box36 = (DropDownList)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Comment18");

                    // Assign Values to textboxes


                    box1.Text = dt.Rows[i]["NT1"].ToString();
                    box2.Text = dt.Rows[i]["NT1_Status"].ToString();


                    box3.Text = dt.Rows[i]["NT2"].ToString();
                    box4.Text = dt.Rows[i]["NT2_Status"].ToString();


                    box5.Text = dt.Rows[i]["NT3"].ToString();
                    box6.Text = dt.Rows[i]["NT3_Status"].ToString();


                    box7.Text = dt.Rows[i]["ICU"].ToString();
                    box8.Text = dt.Rows[i]["ICU_Status"].ToString();


                    box9.Text = dt.Rows[i]["SDU"].ToString();
                    box10.Text = dt.Rows[i]["SDU_Status"].ToString();


                    box11.Text = dt.Rows[i]["Tele3rd1"].ToString();
                    box12.Text = dt.Rows[i]["Tele3rd1_Status"].ToString();


                    box13.Text = dt.Rows[i]["Tele3rd2"].ToString();
                    box14.Text = dt.Rows[i]["Tele3rd2_Status"].ToString();


                    box15.Text = dt.Rows[i]["Tele4th1"].ToString();
                    box16.Text = dt.Rows[i]["Tele4th1_Status"].ToString();


                    box17.Text = dt.Rows[i]["Tele4th2"].ToString();
                    box18.Text = dt.Rows[i]["Tele4th2_Status"].ToString();


                    box19.Text = dt.Rows[i]["Pedi3rd"].ToString();
                    box20.Text = dt.Rows[i]["Pedi3rd_Status"].ToString();


                    box21.Text = dt.Rows[i]["Pedi4th"].ToString();
                    box22.Text = dt.Rows[i]["Pedi4th_Status"].ToString();


                    box23.Text = dt.Rows[i]["PICU_SD"].ToString();
                    box24.Text = dt.Rows[i]["PICU_SD_Status"].ToString();


                    box25.Text = dt.Rows[i]["ER"].ToString();
                    box26.Text = dt.Rows[i]["ER_Status"].ToString();


                    box27.Text = dt.Rows[i]["MICU"].ToString();
                    box28.Text = dt.Rows[i]["MICU_Status"].ToString();


                    box29.Text = dt.Rows[i]["CPV"].ToString();
                    box30.Text = dt.Rows[i]["CPV_Status"].ToString();


                    box31.Text = dt.Rows[i]["CVOR"].ToString();
                    box32.Text = dt.Rows[i]["CVOR_Status"].ToString();


                    box33.Text = dt.Rows[i]["CLOF"].ToString();
                    box34.Text = dt.Rows[i]["CLOF_Status"].ToString();


                    box35.Text = dt.Rows[i]["CVOR_HOLDING"].ToString();
                    box36.Text = dt.Rows[i]["CVOR_HOLDING_Status"].ToString();

                    rowIndex++;

                }
            }
            // ViewState["CurrentTable"] = dt;

        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string authUserName;
        authUserName = User.Identity.Name;

        /*If page is not a Postback */


        dateLabel.Text = DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm");


        if (!Page.IsPostBack)
        {
            SetInitialRow();
            SetInitialRow2();
        }
    }


    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        int rowIndex = 0;
        StringCollection sc = new StringCollection();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox and DropDowns values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Bed1");
                    DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_Comment1");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Bed2");
                    DropDownList box4 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_Comment2");

                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Bed3");
                    DropDownList box6 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_Comment3");

                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Bed4");
                    DropDownList box8 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_Comment4");

                    TextBox box9 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Bed5");
                    DropDownList box10 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_Comment5");

                    TextBox box11 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Bed6");
                    DropDownList box12 = (DropDownList)Gridview1.Rows[rowIndex].Cells[6].FindControl("txt_Comment6");


                    TextBox box13 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Bed7");
                    DropDownList box14 = (DropDownList)Gridview1.Rows[rowIndex].Cells[7].FindControl("txt_Comment7");

                    TextBox box15 = (TextBox)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Bed8");
                    DropDownList box16 = (DropDownList)Gridview1.Rows[rowIndex].Cells[8].FindControl("txt_Comment8");

                    TextBox box17 = (TextBox)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Bed9");
                    DropDownList box18 = (DropDownList)Gridview1.Rows[rowIndex].Cells[9].FindControl("txt_Comment9");

                    TextBox box19 = (TextBox)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Bed10");
                    DropDownList box20 = (DropDownList)Gridview1.Rows[rowIndex].Cells[10].FindControl("txt_Comment10");


                    TextBox box21 = (TextBox)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Bed11");
                    DropDownList box22 = (DropDownList)Gridview1.Rows[rowIndex].Cells[11].FindControl("txt_Comment11");

                    TextBox box23 = (TextBox)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Bed12");
                    DropDownList box24 = (DropDownList)Gridview1.Rows[rowIndex].Cells[12].FindControl("txt_Comment12");

                    TextBox box25 = (TextBox)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Bed13");
                    DropDownList box26 = (DropDownList)Gridview1.Rows[rowIndex].Cells[13].FindControl("txt_Comment13");

                    TextBox box27 = (TextBox)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Bed14");
                    DropDownList box28 = (DropDownList)Gridview1.Rows[rowIndex].Cells[14].FindControl("txt_Comment14");

                    TextBox box29 = (TextBox)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Bed15");
                    DropDownList box30 = (DropDownList)Gridview1.Rows[rowIndex].Cells[15].FindControl("txt_Comment15");

                    TextBox box31 = (TextBox)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Bed16");
                    DropDownList box32 = (DropDownList)Gridview1.Rows[rowIndex].Cells[16].FindControl("txt_Comment16");

                    TextBox box33 = (TextBox)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Bed17");
                    DropDownList box34 = (DropDownList)Gridview1.Rows[rowIndex].Cells[17].FindControl("txt_Comment17");


                    TextBox box35 = (TextBox)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Bed18");
                    DropDownList box36 = (DropDownList)Gridview1.Rows[rowIndex].Cells[18].FindControl("txt_Comment18");

                    DateTime datevar = DateTime.Parse(dateLabel.Text.ToString());
                    String dateLabelFx = DateTime.Now.ToString("dd MMMM yyyy HH:mm");

                    // Noth Tower Header Census


                    TextBox NT1Census = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census1");
                    TextBox NT2Census = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census2");
                    TextBox NT3Census = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census3");
                    TextBox ICUCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census4");
                    TextBox SDUCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census5");
                    TextBox Tele3rd1Census = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census6");
                    TextBox Tele3rd2Census = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census7");
                    TextBox Tele4th1Census = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census8");
                    TextBox Tele4th2Census = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census9");
                    TextBox Pedi3rdCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census10");
                    TextBox Pedi4thCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census11");
                    TextBox PICUCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census12");
                    TextBox MICUCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census13");
                    TextBox ERCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census14");
                    TextBox CPUCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census15");
                    TextBox CVORCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census16");
                    TextBox CLOFCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census17");
                    TextBox CVORHCensus = (TextBox)Gridview1.HeaderRow.FindControl("txt_Census18");


                    //General Census Variables 
                    string bh_Census = txtCensusBehavioral.Text;
                    string wmCensus = txtCensusWomens.Text;
                    string rh_Census = txtCensusREHAB.Text;

                    //get the values from the TextBoxes
                    //then add it to the collections with a comma "," as the delimited values
                    sc.Add(dateLabelFx + "," + bh_Census + "," + wmCensus + "," + rh_Census + "," + NT1Census.Text + "," + NT2Census.Text + "," + NT3Census.Text + "," + ICUCensus.Text + "," + SDUCensus.Text + "," + Tele3rd1Census.Text + "," + Tele3rd2Census.Text + "," + Tele4th1Census.Text + "," + Tele4th2Census.Text + "," + Pedi3rdCensus.Text + "," + Pedi4thCensus.Text + "," + PICUCensus.Text + "," + MICUCensus.Text + "," + ERCensus.Text + "," + CPUCensus.Text + "," + CVORCensus.Text + "," + CLOFCensus.Text + "," + CVORHCensus.Text + "," + box1.Text + "," + box2.Text + "," + box3.Text + "," + box4.Text + "," + box5.Text + "," + box6.Text + "," + box7.Text + "," + box8.Text + "," + box9.Text + "," + box10.Text + "," + box11.Text + "," + box12.Text + "," + box13.Text + "," + box14.Text + "," + box15.Text + "," + box16.Text + "," + box17.Text + "," + box18.Text + "," + box19.Text + "," + box20.Text + "," + box21.Text + "," + box22.Text + "," + box23.Text + "," + box24.Text + "," + box25.Text + "," + box26.Text + "," + box27.Text + "," + box28.Text + "," + box29.Text + "," + box30.Text + "," + box31.Text + "," + box32.Text + "," + box33.Text + "," + box34.Text + "," + box35.Text + "," + box36.Text + ",");
                    rowIndex++;
                }
                //Call the method for executing inserts from main grid
                InsertRecords(sc);


            }
        }
    }
    //A method that returns a string which calls the connection string from the web.config
    private string GetConnectionString()
    {
        //"DBConnection" is the name of the Connection String
        //that was set up from the web.config file
        return System.Configuration.ConfigurationManager.ConnectionStrings["appdevConnectionString2"].ConnectionString;
    }
    //A method that Inserts the records to the database
    private void InsertRecords(StringCollection sc)
    {

        SqlConnection conn = new SqlConnection(GetConnectionString());
        StringBuilder sb = new StringBuilder(string.Empty);
        
        string[] splitItems = null;


        foreach (string item in sc)
        {

            const string sqlStatement = "INSERT INTO tb_Bed_Control (Date,Behavioral_Census,Womens_Census,Rehab_Census,NT1_Census,NT2_Census,NT3_Census,ICU_Census,SDU_Census,Tele3rd1_Census,Tele3rd2_Census,Tele4th1_Census,Tele4th2_Census,Pedi3rd_Census,Pedi4th_Census,PICU_SD_Census,ER_Census,MICU_Census,CPV_Census,CVOR_Census,CLOF_Census,CVOR_HOLDING_Census,NT1,NT1_Status,NT2,NT2_Status,NT3,NT3_Status,ICU,ICU_Status,SDU,SDU_Status,Tele3rd1,Tele3rd1_Status,Tele3rd2,Tele3rd2_Status,Tele4th1,Tele4th1_Status,Tele4th2,Tele4th2_Status,Pedi3rd,Pedi3rd_Status,Pedi4th,Pedi4th_Status,PICU_SD,PICU_SD_Status,ER,ER_Status,MICU,MICU_Status,CPV,CPV_Status,CVOR,CVOR_Status,CLOF,CLOF_Status,CVOR_HOLDING,CVOR_HOLDING_Status) VALUES";
            

            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());

                sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}','{34}','{35}','{36}','{37}','{38}','{39}','{40}','{41}','{42}','{43}','{44}','{45}','{46}','{47}','{48}','{49}','{50}','{51}','{52}','{53}','{54}','{55}','{56}','{57}','{58}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3], splitItems[4], splitItems[5], splitItems[6], splitItems[7], splitItems[8], splitItems[9], splitItems[10], splitItems[11], splitItems[12], splitItems[13], splitItems[14], splitItems[15], splitItems[16], splitItems[17], splitItems[18], splitItems[19], splitItems[20], splitItems[21], splitItems[22], splitItems[23], splitItems[24], splitItems[25], splitItems[26], splitItems[27], splitItems[28], splitItems[29], splitItems[30], splitItems[31], splitItems[32], splitItems[33], splitItems[34], splitItems[35], splitItems[36], splitItems[37], splitItems[38], splitItems[39], splitItems[40], splitItems[41], splitItems[42], splitItems[43], splitItems[44], splitItems[45], splitItems[46], splitItems[47], splitItems[48], splitItems[49], splitItems[50], splitItems[51], splitItems[52], splitItems[53], splitItems[54], splitItems[55], splitItems[56], splitItems[57]);

            }



        }


        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sb.ToString(), conn);


            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Available Beds Succesfully Updated');", true);

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            conn.Close();
        }
    }
    // Hide the Remove Button at the last row of the GridView
    protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
            if (lb != null)
            {
                if (dt.Rows.Count > 1)
                {
                    if (e.Row.RowIndex == dt.Rows.Count - 1)
                    {
                        lb.Visible = false;
                    }
                }
                else
                {
                    lb.Visible = false;
                }
            }
        }
    }



    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
        int rowID = gvRow.RowIndex + 1;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 1)
            {
                if (gvRow.RowIndex < dt.Rows.Count - 1)
                {
                    //Remove the Selected Row data
                    dt.Rows.Remove(dt.Rows[rowID]);
                }
            }
            //Store the current data in ViewState for future reference
            ViewState["CurrentTable"] = dt;
            //Re bind the GridView for the updated data
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }

    // --------------------------ENDS FIRST DATA TABLE ----------------------------------//


    //-----------------------------BEGIN SECOND DATA TABLE------------------------------------//



    private void SetInitialRow2()
    {
        DataTable dt2 = new DataTable();
        DataRow dr2 = null;

        dt2.Columns.Add(new DataColumn("RowNumber2", typeof(string)));
     

        dt2.Columns.Add(new DataColumn("NT1", typeof(string)));
        dt2.Columns.Add(new DataColumn("NT1_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("NT2", typeof(string)));
        dt2.Columns.Add(new DataColumn("NT2_Status", typeof(string)));
        //   dt.Columns.Add(new DataColumn("NT2_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("NT3", typeof(string)));
        dt2.Columns.Add(new DataColumn("NT3_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("ICU", typeof(string)));
        dt2.Columns.Add(new DataColumn("ICU_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("SDU", typeof(string)));
        dt2.Columns.Add(new DataColumn("SDU_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("Tele3rd1", typeof(string)));
        dt2.Columns.Add(new DataColumn("Tele3rd1_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("Tele3rd2", typeof(string)));
        dt2.Columns.Add(new DataColumn("Tele3rd2_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("Tele4th1", typeof(string)));
        dt2.Columns.Add(new DataColumn("Tele4th1_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("Tele4th2", typeof(string)));
        dt2.Columns.Add(new DataColumn("Tele4th2_Status", typeof(string)));

        dt2.Columns.Add(new DataColumn("Pedi3rd", typeof(string)));
        dt2.Columns.Add(new DataColumn("Pedi3rd_Status", typeof(string)));

        dt2.Columns.Add(new DataColumn("Pedi4th", typeof(string)));
        dt2.Columns.Add(new DataColumn("Pedi4th_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("PICU_SD", typeof(string)));
        dt2.Columns.Add(new DataColumn("PICU_SD_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("ER", typeof(string)));
        dt2.Columns.Add(new DataColumn("ER_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("MICU", typeof(string)));
        dt2.Columns.Add(new DataColumn("MICU_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("CPV", typeof(string)));
        dt2.Columns.Add(new DataColumn("CPV_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("CVOR", typeof(string)));
        dt2.Columns.Add(new DataColumn("CVOR_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("CLOF", typeof(string)));
        dt2.Columns.Add(new DataColumn("CLOF_Status", typeof(string)));


        dt2.Columns.Add(new DataColumn("CVOR_HOLDING", typeof(string)));
        dt2.Columns.Add(new DataColumn("CVOR_HOLDING_Status", typeof(string)));



        dr2 = dt2.NewRow();
        dr2["RowNumber2"] = 1;
        // dr["Date"] = string.Empty;
        dr2["NT1"] = string.Empty;
        dr2["NT2"] = string.Empty;
        dr2["NT3"] = string.Empty;
        dr2["ICU"] = string.Empty;
        dr2["SDU"] = string.Empty;
        dr2["Tele3rd1"] = string.Empty;
        dr2["Tele3rd2"] = string.Empty;
        dr2["Tele4th1"] = string.Empty;
        dr2["Tele4th2"] = string.Empty;
        dr2["Pedi3rd"] = string.Empty;
        dr2["Pedi4th"] = string.Empty;
        dr2["PICU_SD"] = string.Empty;
        dr2["ER"] = string.Empty;
        dr2["MICU"] = string.Empty;
        dr2["CPV"] = string.Empty;
        dr2["CVOR"] = string.Empty;
        dr2["CLOF"] = string.Empty;
        dr2["CVOR_HOLDING"] = string.Empty;

        dt2.Rows.Add(dr2);
        //dr = dt.NewRow();

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

        Gridview2.DataSource = dt2;
        Gridview2.DataBind();

    }


    private void AddNewRowToGrid2()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable2 = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow2 = null;
            if (dtCurrentTable2.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable2.Rows.Count; i++)
                {
                    //extract the TextBox  and DropDown values

                    
                    TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[1].FindControl("txt_Bed19");
                    DropDownList box2 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("txt_Comment19");

                    TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("txt_Bed20");
                    DropDownList box4 = (DropDownList)Gridview2.Rows[rowIndex].Cells[2].FindControl("txt_Comment20");

                    TextBox box5 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("txt_Bed21");
                    DropDownList box6 = (DropDownList)Gridview2.Rows[rowIndex].Cells[3].FindControl("txt_Comment21");

                    TextBox box7 = (TextBox)Gridview2.Rows[rowIndex].Cells[4].FindControl("txt_Bed22");
                    DropDownList box8 = (DropDownList)Gridview2.Rows[rowIndex].Cells[4].FindControl("txt_Comment22");

                    TextBox box9 = (TextBox)Gridview2.Rows[rowIndex].Cells[5].FindControl("txt_Bed23");
                    DropDownList box10 = (DropDownList)Gridview2.Rows[rowIndex].Cells[5].FindControl("txt_Comment23");

                    TextBox box11 = (TextBox)Gridview2.Rows[rowIndex].Cells[6].FindControl("txt_Bed24");
                    DropDownList box12 = (DropDownList)Gridview2.Rows[rowIndex].Cells[6].FindControl("txt_Comment24");


                    TextBox box13 = (TextBox)Gridview2.Rows[rowIndex].Cells[7].FindControl("txt_Bed25");
                    DropDownList box14 = (DropDownList)Gridview2.Rows[rowIndex].Cells[7].FindControl("txt_Comment25");

                    TextBox box15 = (TextBox)Gridview2.Rows[rowIndex].Cells[8].FindControl("txt_Bed26");
                    DropDownList box16 = (DropDownList)Gridview2.Rows[rowIndex].Cells[8].FindControl("txt_Comment26");

                    TextBox box17 = (TextBox)Gridview2.Rows[rowIndex].Cells[9].FindControl("txt_Bed27");
                    DropDownList box18 = (DropDownList)Gridview2.Rows[rowIndex].Cells[9].FindControl("txt_Comment27");

                    TextBox box19 = (TextBox)Gridview2.Rows[rowIndex].Cells[10].FindControl("txt_Bed28");
                    DropDownList box20 = (DropDownList)Gridview2.Rows[rowIndex].Cells[10].FindControl("txt_Comment28");


                    TextBox box21 = (TextBox)Gridview2.Rows[rowIndex].Cells[11].FindControl("txt_Bed29");
                    DropDownList box22 = (DropDownList)Gridview2.Rows[rowIndex].Cells[11].FindControl("txt_Comment29");

                    TextBox box23 = (TextBox)Gridview2.Rows[rowIndex].Cells[12].FindControl("txt_Bed30");
                    DropDownList box24 = (DropDownList)Gridview2.Rows[rowIndex].Cells[12].FindControl("txt_Comment30");

                    TextBox box25 = (TextBox)Gridview2.Rows[rowIndex].Cells[13].FindControl("txt_Bed31");
                    DropDownList box26 = (DropDownList)Gridview2.Rows[rowIndex].Cells[13].FindControl("txt_Comment31");

                    TextBox box27 = (TextBox)Gridview2.Rows[rowIndex].Cells[14].FindControl("txt_Bed32");
                    DropDownList box28 = (DropDownList)Gridview2.Rows[rowIndex].Cells[14].FindControl("txt_Comment32");

                    TextBox box29 = (TextBox)Gridview2.Rows[rowIndex].Cells[15].FindControl("txt_Bed33");
                    DropDownList box30 = (DropDownList)Gridview2.Rows[rowIndex].Cells[15].FindControl("txt_Comment33");

                    TextBox box31 = (TextBox)Gridview2.Rows[rowIndex].Cells[16].FindControl("txt_Bed34");
                    DropDownList box32 = (DropDownList)Gridview2.Rows[rowIndex].Cells[16].FindControl("txt_Comment34");

                    TextBox box33 = (TextBox)Gridview2.Rows[rowIndex].Cells[17].FindControl("txt_Bed35");
                    DropDownList box34 = (DropDownList)Gridview2.Rows[rowIndex].Cells[17].FindControl("txt_Comment35");


                    TextBox box35 = (TextBox)Gridview2.Rows[rowIndex].Cells[18].FindControl("txt_Bed36");
                    DropDownList box36 = (DropDownList)Gridview2.Rows[rowIndex].Cells[18].FindControl("txt_Comment36");

                    drCurrentRow2 = dtCurrentTable2.NewRow();


                    drCurrentRow2["RowNumber2"] = i + 1;

                    drCurrentRow2["NT1"] = box1.Text;
                    drCurrentRow2["NT1_Status"] = box2.SelectedValue;


                    drCurrentRow2["NT2"] = box3.Text;
                    drCurrentRow2["NT2_Status"] = box4.SelectedValue;


                    drCurrentRow2["NT3"] = box5.Text;
                    drCurrentRow2["NT3_Status"] = box6.SelectedValue;


                    drCurrentRow2["ICU"] = box7.Text;
                    drCurrentRow2["ICU_Status"] = box8.SelectedValue;

                    // drCurrentRow["SDU_Census"] = box1.Text;
                    drCurrentRow2["SDU"] = box9.Text;
                    drCurrentRow2["SDU_Status"] = box10.SelectedValue;

                    //drCurrentRow["Tele3rd1_Census"] = box1.Text;
                    drCurrentRow2["Tele3rd1"] = box11.Text;
                    drCurrentRow2["Tele3rd1_Status"] = box12.SelectedValue;

                    //drCurrentRow["Tele3rd2_Census"] = box1.Text;
                    drCurrentRow2["Tele3rd2"] = box13.Text;
                    drCurrentRow2["Tele3rd2_Status"] = box14.SelectedValue;

                    //drCurrentRow["Tele4th1_Census"] = box1.Text;
                    drCurrentRow2["Tele4th1"] = box15.Text;
                    drCurrentRow2["Tele4th1_Status"] = box16.SelectedValue;

                    //drCurrentRow["Tele4th2_Census"] = box1.Text;
                    drCurrentRow2["Tele4th2"] = box17.Text;
                    drCurrentRow2["Tele4th2_Status"] = box18.SelectedValue;

                    //drCurrentRow["Pedi3rd_Census"] = box1.Text;
                    drCurrentRow2["Pedi3rd"] = box19.Text;
                    drCurrentRow2["Pedi3rd_Status"] = box20.SelectedValue;

                    //drCurrentRow["Pedi4th_Census"] = box1.Text;
                    drCurrentRow2["Pedi4th"] = box21.Text;
                    drCurrentRow2["Pedi4th_Status"] = box22.SelectedValue;

                    // drCurrentRow["PICU_SD_Census"] = box1.Text;
                    drCurrentRow2["PICU_SD"] = box23.Text;
                    drCurrentRow2["PICU_SD_Status"] = box24.SelectedValue;

                    //drCurrentRow["ER_Census"] = box1.Text;
                    drCurrentRow2["ER"] = box25.Text;
                    drCurrentRow2["ER_Status"] = box26.SelectedValue;

                    // drCurrentRow["MICU_Census"] = box1.Text;
                    drCurrentRow2["MICU"] = box27.Text;
                    drCurrentRow2["MICU_Status"] = box28.SelectedValue;

                    //drCurrentRow["CPV_Census"] = box1.Text;
                    drCurrentRow2["CPV"] = box29.Text;
                    drCurrentRow2["CPV_Status"] = box30.SelectedValue;

                    //drCurrentRow["CVOR_Census"] = box1.Text;
                    drCurrentRow2["CVOR"] = box31.Text;
                    drCurrentRow2["CVOR_Status"] = box32.SelectedValue;

                    drCurrentRow2["CLOF"] = box33.Text;
                    drCurrentRow2["CLOF_Status"] = box34.SelectedValue;


                    //drCurrentRow["CVOR_HOLDING_Census"] = box1.Text;
                    drCurrentRow2["CVOR_HOLDING"] = box35.Text;
                    drCurrentRow2["CVOR_HOLDING_Status"] = box36.SelectedValue;

                    rowIndex++;
                }


                dtCurrentTable2.Rows.Add(drCurrentRow2);
                ViewState["CurrentTable"] = dtCurrentTable2;

                Gridview2.DataSource = dtCurrentTable2;
                Gridview2.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData2();
    }


    private void SetPreviousData2()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt2 = (DataTable)ViewState["CurrentTable"];
            if (dt2.Rows.Count > 0)
            {
                for (int i = 1; i < dt2.Rows.Count; i++)
                {


                    TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[1].FindControl("txt_Bed19");
                    DropDownList box2 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("txt_Comment19");

                    TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("txt_Bed20");
                    DropDownList box4 = (DropDownList)Gridview2.Rows[rowIndex].Cells[2].FindControl("txt_Comment20");

                    TextBox box5 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("txt_Bed21");
                    DropDownList box6 = (DropDownList)Gridview2.Rows[rowIndex].Cells[3].FindControl("txt_Comment21");

                    TextBox box7 = (TextBox)Gridview2.Rows[rowIndex].Cells[4].FindControl("txt_Bed22");
                    DropDownList box8 = (DropDownList)Gridview2.Rows[rowIndex].Cells[4].FindControl("txt_Comment22");

                    TextBox box9 = (TextBox)Gridview2.Rows[rowIndex].Cells[5].FindControl("txt_Bed23");
                    DropDownList box10 = (DropDownList)Gridview2.Rows[rowIndex].Cells[5].FindControl("txt_Comment23");

                    TextBox box11 = (TextBox)Gridview2.Rows[rowIndex].Cells[6].FindControl("txt_Bed24");
                    DropDownList box12 = (DropDownList)Gridview2.Rows[rowIndex].Cells[6].FindControl("txt_Comment24");


                    TextBox box13 = (TextBox)Gridview2.Rows[rowIndex].Cells[7].FindControl("txt_Bed25");
                    DropDownList box14 = (DropDownList)Gridview2.Rows[rowIndex].Cells[7].FindControl("txt_Comment25");

                    TextBox box15 = (TextBox)Gridview2.Rows[rowIndex].Cells[8].FindControl("txt_Bed26");
                    DropDownList box16 = (DropDownList)Gridview2.Rows[rowIndex].Cells[8].FindControl("txt_Comment26");

                    TextBox box17 = (TextBox)Gridview2.Rows[rowIndex].Cells[9].FindControl("txt_Bed27");
                    DropDownList box18 = (DropDownList)Gridview2.Rows[rowIndex].Cells[9].FindControl("txt_Comment27");

                    TextBox box19 = (TextBox)Gridview2.Rows[rowIndex].Cells[10].FindControl("txt_Bed28");
                    DropDownList box20 = (DropDownList)Gridview2.Rows[rowIndex].Cells[10].FindControl("txt_Comment28");


                    TextBox box21 = (TextBox)Gridview2.Rows[rowIndex].Cells[11].FindControl("txt_Bed29");
                    DropDownList box22 = (DropDownList)Gridview2.Rows[rowIndex].Cells[11].FindControl("txt_Comment29");

                    TextBox box23 = (TextBox)Gridview2.Rows[rowIndex].Cells[12].FindControl("txt_Bed30");
                    DropDownList box24 = (DropDownList)Gridview2.Rows[rowIndex].Cells[12].FindControl("txt_Comment30");

                    TextBox box25 = (TextBox)Gridview2.Rows[rowIndex].Cells[13].FindControl("txt_Bed31");
                    DropDownList box26 = (DropDownList)Gridview2.Rows[rowIndex].Cells[13].FindControl("txt_Comment31");

                    TextBox box27 = (TextBox)Gridview2.Rows[rowIndex].Cells[14].FindControl("txt_Bed32");
                    DropDownList box28 = (DropDownList)Gridview2.Rows[rowIndex].Cells[14].FindControl("txt_Comment32");

                    TextBox box29 = (TextBox)Gridview2.Rows[rowIndex].Cells[15].FindControl("txt_Bed33");
                    DropDownList box30 = (DropDownList)Gridview2.Rows[rowIndex].Cells[15].FindControl("txt_Comment33");

                    TextBox box31 = (TextBox)Gridview2.Rows[rowIndex].Cells[16].FindControl("txt_Bed34");
                    DropDownList box32 = (DropDownList)Gridview2.Rows[rowIndex].Cells[16].FindControl("txt_Comment34");

                    TextBox box33 = (TextBox)Gridview2.Rows[rowIndex].Cells[17].FindControl("txt_Bed35");
                    DropDownList box34 = (DropDownList)Gridview2.Rows[rowIndex].Cells[17].FindControl("txt_Comment35");


                    TextBox box35 = (TextBox)Gridview2.Rows[rowIndex].Cells[18].FindControl("txt_Bed36");
                    DropDownList box36 = (DropDownList)Gridview2.Rows[rowIndex].Cells[18].FindControl("txt_Comment36");

                    // Assign Values to textboxes
                    
                    box1.Text = dt2.Rows[i]["NT1"].ToString();
                    box2.Text = dt2.Rows[i]["NT1_Status"].ToString();


                    box3.Text = dt2.Rows[i]["NT2"].ToString();
                    box4.Text = dt2.Rows[i]["NT2_Status"].ToString();


                    box5.Text = dt2.Rows[i]["NT3"].ToString();
                    box6.Text = dt2.Rows[i]["NT3_Status"].ToString();


                    box7.Text = dt2.Rows[i]["ICU"].ToString();
                    box8.Text = dt2.Rows[i]["ICU_Status"].ToString();


                    box9.Text = dt2.Rows[i]["SDU"].ToString();
                    box10.Text = dt2.Rows[i]["SDU_Status"].ToString();


                    box11.Text = dt2.Rows[i]["Tele3rd1"].ToString();
                    box12.Text = dt2.Rows[i]["Tele3rd1_Status"].ToString();


                    box13.Text = dt2.Rows[i]["Tele3rd2"].ToString();
                    box14.Text = dt2.Rows[i]["Tele3rd2_Status"].ToString();


                    box15.Text = dt2.Rows[i]["Tele4th1"].ToString();
                    box16.Text = dt2.Rows[i]["Tele4th1_Status"].ToString();


                    box17.Text = dt2.Rows[i]["Tele4th2"].ToString();
                    box18.Text = dt2.Rows[i]["Tele4th2_Status"].ToString();


                    box19.Text = dt2.Rows[i]["Pedi3rd"].ToString();
                    box20.Text = dt2.Rows[i]["Pedi3rd_Status"].ToString();


                    box21.Text = dt2.Rows[i]["Pedi4th"].ToString();
                    box22.Text = dt2.Rows[i]["Pedi4th_Status"].ToString();


                    box23.Text = dt2.Rows[i]["PICU_SD"].ToString();
                    box24.Text = dt2.Rows[i]["PICU_SD_Status"].ToString();


                    box25.Text = dt2.Rows[i]["ER"].ToString();
                    box26.Text = dt2.Rows[i]["ER_Status"].ToString();


                    box27.Text = dt2.Rows[i]["MICU"].ToString();
                    box28.Text = dt2.Rows[i]["MICU_Status"].ToString();


                    box29.Text = dt2.Rows[i]["CPV"].ToString();
                    box30.Text = dt2.Rows[i]["CPV_Status"].ToString();


                    box31.Text = dt2.Rows[i]["CVOR"].ToString();
                    box32.Text = dt2.Rows[i]["CVOR_Status"].ToString();


                    box33.Text = dt2.Rows[i]["CLOF"].ToString();
                    box34.Text = dt2.Rows[i]["CLOF_Status"].ToString();


                    box35.Text = dt2.Rows[i]["CVOR_HOLDING"].ToString();
                    box36.Text = dt2.Rows[i]["CVOR_HOLDING_Status"].ToString();

                    rowIndex++;

                }
            }
            // ViewState["CurrentTable"] = dt;

        }
    }



    protected void ButtonAdd2_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid2();
    }




    protected void Button2_Click(object sender, EventArgs e)
    {
        int rowIndex = 0;
        StringCollection sc = new StringCollection();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable2 = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable2.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable2.Rows.Count; i++)
                {
                    //extract the TextBox and DropDowns values

                    TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[1].FindControl("txt_Bed19");
                    DropDownList box2 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("txt_Comment19");

                    TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("txt_Bed20");
                    DropDownList box4 = (DropDownList)Gridview2.Rows[rowIndex].Cells[2].FindControl("txt_Comment20");

                    TextBox box5 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("txt_Bed21");
                    DropDownList box6 = (DropDownList)Gridview2.Rows[rowIndex].Cells[3].FindControl("txt_Comment21");

                    TextBox box7 = (TextBox)Gridview2.Rows[rowIndex].Cells[4].FindControl("txt_Bed22");
                    DropDownList box8 = (DropDownList)Gridview2.Rows[rowIndex].Cells[4].FindControl("txt_Comment22");

                    TextBox box9 = (TextBox)Gridview2.Rows[rowIndex].Cells[5].FindControl("txt_Bed23");
                    DropDownList box10 = (DropDownList)Gridview2.Rows[rowIndex].Cells[5].FindControl("txt_Comment23");

                    TextBox box11 = (TextBox)Gridview2.Rows[rowIndex].Cells[6].FindControl("txt_Bed24");
                    DropDownList box12 = (DropDownList)Gridview2.Rows[rowIndex].Cells[6].FindControl("txt_Comment24");


                    TextBox box13 = (TextBox)Gridview2.Rows[rowIndex].Cells[7].FindControl("txt_Bed25");
                    DropDownList box14 = (DropDownList)Gridview2.Rows[rowIndex].Cells[7].FindControl("txt_Comment25");

                    TextBox box15 = (TextBox)Gridview2.Rows[rowIndex].Cells[8].FindControl("txt_Bed26");
                    DropDownList box16 = (DropDownList)Gridview2.Rows[rowIndex].Cells[8].FindControl("txt_Comment26");

                    TextBox box17 = (TextBox)Gridview2.Rows[rowIndex].Cells[9].FindControl("txt_Bed27");
                    DropDownList box18 = (DropDownList)Gridview2.Rows[rowIndex].Cells[9].FindControl("txt_Comment27");

                    TextBox box19 = (TextBox)Gridview2.Rows[rowIndex].Cells[10].FindControl("txt_Bed28");
                    DropDownList box20 = (DropDownList)Gridview2.Rows[rowIndex].Cells[10].FindControl("txt_Comment28");


                    TextBox box21 = (TextBox)Gridview2.Rows[rowIndex].Cells[11].FindControl("txt_Bed29");
                    DropDownList box22 = (DropDownList)Gridview2.Rows[rowIndex].Cells[11].FindControl("txt_Comment29");

                    TextBox box23 = (TextBox)Gridview2.Rows[rowIndex].Cells[12].FindControl("txt_Bed30");
                    DropDownList box24 = (DropDownList)Gridview2.Rows[rowIndex].Cells[12].FindControl("txt_Comment30");

                    TextBox box25 = (TextBox)Gridview2.Rows[rowIndex].Cells[13].FindControl("txt_Bed31");
                    DropDownList box26 = (DropDownList)Gridview2.Rows[rowIndex].Cells[13].FindControl("txt_Comment31");

                    TextBox box27 = (TextBox)Gridview2.Rows[rowIndex].Cells[14].FindControl("txt_Bed32");
                    DropDownList box28 = (DropDownList)Gridview2.Rows[rowIndex].Cells[14].FindControl("txt_Comment32");

                    TextBox box29 = (TextBox)Gridview2.Rows[rowIndex].Cells[15].FindControl("txt_Bed33");
                    DropDownList box30 = (DropDownList)Gridview2.Rows[rowIndex].Cells[15].FindControl("txt_Comment33");

                    TextBox box31 = (TextBox)Gridview2.Rows[rowIndex].Cells[16].FindControl("txt_Bed34");
                    DropDownList box32 = (DropDownList)Gridview2.Rows[rowIndex].Cells[16].FindControl("txt_Comment34");

                    TextBox box33 = (TextBox)Gridview2.Rows[rowIndex].Cells[17].FindControl("txt_Bed35");
                    DropDownList box34 = (DropDownList)Gridview2.Rows[rowIndex].Cells[17].FindControl("txt_Comment35");


                    TextBox box35 = (TextBox)Gridview2.Rows[rowIndex].Cells[18].FindControl("txt_Bed36");
                    DropDownList box36 = (DropDownList)Gridview2.Rows[rowIndex].Cells[18].FindControl("txt_Comment36");

                    DateTime datevar = DateTime.Parse(dateLabel.Text.ToString());
                    String dateLabelFx = DateTime.Now.ToString("dd MMMM yyyy HH:mm");

                    // Noth Tower Header Census


                    TextBox NT1Census = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census19");
                    TextBox NT2Census = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census20");
                    TextBox NT3Census = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census21");
                    TextBox ICUCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census22");
                    TextBox SDUCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census23");
                    TextBox Tele3rd1Census = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census24");
                    TextBox Tele3rd2Census = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census25");
                    TextBox Tele4th1Census = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census26");
                    TextBox Tele4th2Census = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census27");
                    TextBox Pedi3rdCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census28");
                    TextBox Pedi4thCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census29");
                    TextBox PICUCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census30");
                    TextBox MICUCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census31");
                    TextBox ERCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census32");
                    TextBox CPUCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census33");
                    TextBox CVORCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census34");
                    TextBox CLOFCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census35");
                    TextBox CVORHCensus = (TextBox)Gridview2.HeaderRow.FindControl("txt_Census36");




                    //get the values from the TextBoxes
                    //then add it to the collections with a comma "," as the delimited values
                    sc.Add(dateLabelFx + "," + NT1Census.Text + "," + NT2Census.Text + "," + NT3Census.Text + "," + ICUCensus.Text + "," + SDUCensus.Text + "," + Tele3rd1Census.Text + "," + Tele3rd2Census.Text + "," + Tele4th1Census.Text + "," + Tele4th2Census.Text + "," + Pedi3rdCensus.Text + "," + Pedi4thCensus.Text + "," + PICUCensus.Text + "," + MICUCensus.Text + "," + ERCensus.Text + "," + CPUCensus.Text + "," + CVORCensus.Text + "," + CLOFCensus.Text + "," + CVORHCensus.Text + "," + box1.Text + "," + box2.Text + "," + box3.Text + "," + box4.Text + "," + box5.Text + "," + box6.Text + "," + box7.Text + "," + box8.Text + "," + box9.Text + "," + box10.Text + "," + box11.Text + "," + box12.Text + "," + box13.Text + "," + box14.Text + "," + box15.Text + "," + box16.Text + "," + box17.Text + "," + box18.Text + "," + box19.Text + "," + box20.Text + "," + box21.Text + "," + box22.Text + "," + box23.Text + "," + box24.Text + "," + box25.Text + "," + box26.Text + "," + box27.Text + "," + box28.Text + "," + box29.Text + "," + box30.Text + "," + box31.Text + "," + box32.Text + "," + box33.Text + "," + box34.Text + "," + box35.Text + "," + box36.Text + ",");
                    rowIndex++;
                }
                //Call the method for executing inserts from main grid
                InsertRecords2(sc);


            }
        }
    }
    //A method that returns a string which calls the connection string from the web.config
    private string GetConnectionString2()
    {
        //"DBConnection" is the name of the Connection String
        //that was set up from the web.config file
        return System.Configuration.ConfigurationManager.ConnectionStrings["appdevConnectionString2"].ConnectionString;
    }
    //A method that Inserts the records to the database
    private void InsertRecords2(StringCollection sc)
    {

        SqlConnection conn = new SqlConnection(GetConnectionString2());
        StringBuilder sb = new StringBuilder(string.Empty);


        string[] splitItems = null;


        foreach (string item in sc)
        {

            const string sqlStatement = "INSERT INTO tb_Bed_Control_Possible (Date,NT1_Census,NT2_Census,NT3_Census,ICU_Census,SDU_Census,Tele3rd1_Census,Tele3rd2_Census,Tele4th1_Census,Tele4th2_Census,Pedi3rd_Census,Pedi4th_Census,PICU_SD_Census,ER_Census,MICU_Census,CPV_Census,CVOR_Census,CLOF_Census,CVOR_HOLDING_Census,NT1,NT1_Status,NT2,NT2_Status,NT3,NT3_Status,ICU,ICU_Status,SDU,SDU_Status,Tele3rd1,Tele3rd1_Status,Tele3rd2,Tele3rd2_Status,Tele4th1,Tele4th1_Status,Tele4th2,Tele4th2_Status,Pedi3rd,Pedi3rd_Status,Pedi4th,Pedi4th_Status,PICU_SD,PICU_SD_Status,ER,ER_Status,MICU,MICU_Status,CPV,CPV_Status,CVOR,CVOR_Status,CLOF,CLOF_Status,CVOR_HOLDING,CVOR_HOLDING_Status) VALUES";


            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());

                sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}','{34}','{35}','{36}','{37}','{38}','{39}','{40}','{41}','{42}','{43}','{44}','{45}','{46}','{47}','{48}','{49}','{50}','{51}','{52}','{53}','{54}','{55}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3], splitItems[4], splitItems[5], splitItems[6], splitItems[7], splitItems[8], splitItems[9], splitItems[10], splitItems[11], splitItems[12], splitItems[13], splitItems[14], splitItems[15], splitItems[16], splitItems[17], splitItems[18], splitItems[19], splitItems[20], splitItems[21], splitItems[22], splitItems[23], splitItems[24], splitItems[25], splitItems[26], splitItems[27], splitItems[28], splitItems[29], splitItems[30], splitItems[31], splitItems[32], splitItems[33], splitItems[34], splitItems[35], splitItems[36], splitItems[37], splitItems[38], splitItems[39], splitItems[40], splitItems[41], splitItems[42], splitItems[43], splitItems[44], splitItems[45], splitItems[46], splitItems[47], splitItems[48], splitItems[49], splitItems[50], splitItems[51], splitItems[52], splitItems[53], splitItems[54]);

            }

        }

        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sb.ToString(), conn);


            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Pending Beds Succesfully Updated');", true);

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            conn.Close();
        }
    }
    // Hide the Remove Button at the last row of the GridView
    protected void Gridview2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable dt2 = (DataTable)ViewState["CurrentTable"];
            LinkButton lb2 = (LinkButton)e.Row.FindControl("LinkButton1");
            if (lb2 != null)
            {
                if (dt2.Rows.Count > 1)
                {
                    if (e.Row.RowIndex == dt2.Rows.Count - 1)
                    {
                        lb2.Visible = false;
                    }
                }
                else
                {
                    lb2.Visible = false;
                }
            }
        }
    }



    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        LinkButton lb2 = (LinkButton)sender;
        GridViewRow gvRow2 = (GridViewRow)lb2.NamingContainer;
        int rowID = gvRow2.RowIndex + 1;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt2 = (DataTable)ViewState["CurrentTable"];
            if (dt2.Rows.Count > 1)
            {
                if (gvRow2.RowIndex < dt2.Rows.Count - 1)
                {
                    //Remove the Selected Row data
                    dt2.Rows.Remove(dt2.Rows[rowID]);
                }
            }
            //Store the current data in ViewState for future reference
            ViewState["CurrentTable"] = dt2;
            //Re bind the GridView for the updated data
            Gridview2.DataSource = dt2;
            Gridview2.DataBind();
        }

        //Set Previous Data on Postbacks
        SetPreviousData2();
    }

    
    //END OF CLASS 

}

Open in new window

0
 
LVL 25

Accepted Solution

by:
apeter earned 2000 total points
ID: 35069128
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));


dr["RowNumber"] = 1;

From above you say the type is string but when u assign value it is integer. Either change type or assign value like below. Hope this helps.

dr["RowNumber"] = "1";
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question