Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to add blank rows in between gridview rows. The existing rows contains textbox controls as well. On initial get request the page is rendered correctly with proper value in respective textboxes. But on button click outside the gridview, the value of textbox of first grid row after the inserted new row is getting shifted to the new row in a textbox control. While inserting new row, there was no textbox control.

C#
public void AddNewRow(object sender, GridViewRowEventArgs e)
        {
            GridView GridView1 = (GridView)sender;
            GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.EmptyDataRow, DataControlRowState.Insert);
            NewTotalRow.Font.Bold = true;
            NewTotalRow.BackColor = System.Drawing.Color.Blue;
            TableCell HeaderCell = new TableCell();
            HeaderCell.Height = 25;
            HeaderCell.HorizontalAlign = HorizontalAlign.Center;
            HeaderCell.ColumnSpan = 10;
            HeaderCell.ForeColor = System.Drawing.Color.White;
            HeaderCell.Text = "1st Level Subordinates";
            NewTotalRow.Cells.Add(HeaderCell);
            GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow);
            rowIndex++;
        }

 protected void cmdBtn_saveSubmit_Click(object sender, CommandEventArgs e)
        { foreach (GridViewRow gvRow in grid_score.Rows)
                {
                    if (gvRow.RowType != DataControlRowType.EmptyDataRow)
                    {
                     TextBox txtscore = gvRow.Cells[7].FindControl("txtbox") as TextBox;
                    string score = txtscore.Text.ToString();
                     if (score =="")
                      {                                    System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert');", true);
                                        return;
                      }
                    }
                 }
        }


What I have tried:

here after return statement, when the page reappears, the formatting of emptydatarow is getting removed. Also when checking emptydatarow, it is showing as normal datarow and value of textbox just below emptydatarow is getting shifted to textbox in empty row.
Posted
Updated 29-Aug-16 20:26pm
v2

1 solution

C#
DataTable dt = new DataTable();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if(/* add ur condition wer control exist or not*/)
            {
            DataRow dr = dt.NewRow();
            dr[0] = 0;
            dt.Rows.InsertAt(dr, i);
            }
        }
 
Share this answer
 

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



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