Gohyu
Gohyu

Reputation: 478

ASP.NET dynamically add row in GridView not added more than 2 rows

I was created gridView with dynamic add rows on C# Class Library for SP07 WebPart

. My problem is wen I click Button(Create) not added more than 2 rows..

enter image description here

This is Create Button :

    void addBtn_Click(object sender, EventArgs e)
    {
        AddNewRowFun();
    }

My AddNewRowFun() method :

private void AddNewRowFun()
    {
        int rowIndex = 0;
        if (ViewState["Curtbl"] != null)
        {
            DataTable dt = (DataTable)ViewState["Curtbl"];
            DataRow drCurrentRow = null;
            if (dt.Rows.Count > 0)
            {
                for (int i = 1; i <= dt.Rows.Count; i++)
                {
                    TextBox txt1 = (TextBox)myGrid.Rows[rowIndex].Cells[1].FindControl("txt1");
                    DateTimeControl dt1 = (DateTimeControl)myGrid.Rows[rowIndex].Cells[2].FindControl("dt1");
                    DateTimeControl dt2 = (DateTimeControl)myGrid.Rows[rowIndex].Cells[3].FindControl("dt2");
                    TextBox txt2 = (TextBox)myGrid.Rows[rowIndex].Cells[4].FindControl("txt2");
                    TextBox txt3 = (TextBox)myGrid.Rows[rowIndex].Cells[5].FindControl("txt3");

                    drCurrentRow = dt.NewRow();
                    drCurrentRow["rowid"] = i + 1;
                    dt.Rows[i - 1]["txt1"] = txt1.Text;
                    dt.Rows[i - 1]["dt1"] = dt1.SelectedDate;
                    dt.Rows[i - 1]["dt2"] = dt2.SelectedDate;
                    dt.Rows[i - 1]["txt2"] = txt2.Text;
                    dt.Rows[i - 1]["txt3"] = txt3.Text;
                    rowIndex++;
                }
                dt.Rows.Add(drCurrentRow);
                ViewState["Curtbl"] = dt;
                myGrid.DataSource = dt;
                myGrid.DataBind();
            }
        }
        else
        {
            //Response.Write("ViewState Value is Null");
        }
        SetOldData();

Upvotes: 1

Views: 1117

Answers (1)

Maciej S.
Maciej S.

Reputation: 760

According to your comment about max 5 lines, please modify your method as below:

private void AddNewRowFun()
{
    if (dt.Rows.Count < 6)
    {
         [all your code here]
    }
}

Hope this solution will be helpful for you.

Upvotes: 2

Related Questions