Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview which will generate new line when user click on add new row.
But im having issue when read the user input from c# ,i need insert all the user input by row into my database.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        SetInitialRow();
    }
}
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)));
    dr = dt.NewRow();
    dr["RowNumber"] = 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dt.Rows.Add(dr);

    //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 values
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("Date");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("Description");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("Hours");

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

                dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

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

    //Set Previous Data on Postbacks
    SetPreviousData();
}
private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("Date");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("Description");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("Hours");

                box1.Text = dt.Rows[i]["Column1"].ToString();
                box2.Text = dt.Rows[i]["Column2"].ToString();
                box3.Text = dt.Rows[i]["Column3"].ToString();

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

protected void SUBMIT_Click(object sender, EventArgs e)
{

}


my html
ASP.NET
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="No" />
            <asp:TemplateField HeaderText="Date">
                <ItemTemplate>
                    <asp:TextBox ID="Date" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Training Description">
                <ItemTemplate>
                    <asp:TextBox ID="Description" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hours">
                <ItemTemplate>
                     <asp:TextBox ID="Hours" runat="server"></asp:TextBox>
                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" 
                        onclick="ButtonAdd_Click" />
                </FooterTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:gridview>
     <asp:Button ID="SubmitBtn" runat="server" Text="Submit" onclick="SUBMIT_Click" />


What I have tried:

im tried to read user input using c# but keep it keep cant wont,fail loop,manage to read row 1 only,please guide me on this
Posted
Updated 6-Jul-16 20:35pm
Comments
Arasappan 7-Jul-16 0:43am    
Is Row IS Genereted Successfully Bro..

From what I understood, you want to insert all the gridview rows in database table.
One thing you can do is to iterate on gridview using for/foreach loop and store data in a datatable and then use SqlBulkCopy to insert data into database at once.
Other way would be to use loop and insert each row one by one in database.
 
Share this answer
 
Hai
i'm not exactly sure this is what you need as solution,here is my what i found in your method
Solution 1 - Alter Your Code like below
C#
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("Date");
                        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("Description");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("Hours");
                        if (String.IsNullOrEmpty(dt.Rows[i]["Column1"].ToString()))
                        {
                            if (!String.IsNullOrEmpty(box1.Text))
                            {
                                dt.Rows[i]["Column1"] = box1.Text;
                            }
                        }
                        if (String.IsNullOrEmpty(dt.Rows[i]["Column2"].ToString()))
                        {
                            if (!String.IsNullOrEmpty(box1.Text))
                            {
                                dt.Rows[i]["Column2"] = box2.Text;
                            }
                        }
                        if (String.IsNullOrEmpty(dt.Rows[i]["Column3"].ToString()))
                        {
                            if (!String.IsNullOrEmpty(box1.Text))
                            {
                                dt.Rows[i]["Column3"] = box3.Text;
                            }
                        }

                        box1.Text = dt.Rows[i]["Column1"].ToString();
                        box2.Text = dt.Rows[i]["Column2"].ToString();
                        box3.Text = dt.Rows[i]["Column3"].ToString();

                        rowIndex++;
                    }
                }
            }
        }



Solution 2 : Create a Method like this and call in your method
SUBMIT_Click


Advantages : - While your are reading from below lines of code : - if view state is expired then also u are able to read all the data directly from your datagrid
2: - after adding second row in your grid if u r changing data in your first row then the above method will give you a head ache of un-updated data in your view state

if u r using below method all the updated data will be

C#
private DataTable ReadAllGridValues()
      {
          DataTable dt = InitializeDataTable();
          int rowIndex = 1;
          DataRow dr = null;
          for(int rowCount=0;rowCount<Gridview1.Rows.Count;rowCount++)
          {
              TextBox box1 = (TextBox)Gridview1.Rows[rowCount].Cells[1].FindControl("Date");
              TextBox box2 = (TextBox)Gridview1.Rows[rowCount].Cells[2].FindControl("Description");
              TextBox box3 = (TextBox)Gridview1.Rows[rowCount].Cells[3].FindControl("Hours");
                  dr = dt.NewRow();
                  dr["RowNumber"] = rowIndex + 1;
                  dt.Rows[rowCount]["Column1"] = box1.Text;
                  dt.Rows[rowCount]["Column2"] = box2.Text;
                  dt.Rows[rowCount]["Column3"] = box3.Text;
                  dt.Rows.Add(dr);
          }
          return dt;
      }

C#
private DataTable InitializeDataTable()
       {
           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)));
           dr = dt.NewRow();
           dr["RowNumber"] = 1;
           dr["Column1"] = string.Empty;
           dr["Column2"] = string.Empty;
           dr["Column3"] = string.Empty;
           dt.Rows.Add(dr);
           return dt;
       }

C#
private void SetInitialRow()
       {

           DataTable dt = new DataTable();
           dt = InitializeDataTable();
           //Store the DataTable in ViewState
           ViewState["CurrentTable"] = dt;

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


C#
protected void SUBMIT_Click(object sender, EventArgs e)
        {            
            DataTable dt = ReadAllGridValues(); 
            foreach (DataRow dr in dt.Rows)
            {
 
            }
        }


*Note :- Kindly please Comment back if it had resolved your problem
 
Share this answer
 
v2

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