This is a migrated thread and some comments may be shown as answers.

Insert new row dynamically into radgrid

2 Answers 244 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jayanthy Kapistalam
Top achievements
Rank 1
Jayanthy Kapistalam asked on 08 Dec 2008, 04:54 PM
I'm trying to insert a new row every time on button click function. I'm able to insert a row, but it overwrites on top of the previous row instead of creating a new row. Am I missing something?Here is my code.

void FillGrid()
{
DataTable DT=new DataTable();        
DT.Columns.Add("Name", typeof(string));
DT.Columns.Add("Header", typeof(string));
if (Name.Text.Length > 0)
         {           
             sName = QuestionText.Content;
         }
         if (Header.Text.Length > 0)
         {         
             sHeader = Header.Text;
         }
 DT.Rows.Add(sName, sHeader);
}
public void Nrow()
     {
         DT.Rows.InsertAt(DT.NewRow(),0);
     }
     public void BindDG()
     {
       
         QuestionsGrid.DataSource = DT;
         QuestionsGrid.DataBind();      
     }
 protected void Step2AddMore_Click(object sender, EventArgs e)
     {
         try
         {
             QuestionsGrid.CurrentPageIndex = 0;
             FillGrid();
             Nrow();
             BindDG();
             
         }
         catch (Exception ex)
         {
             ErrorMessage.Text = ex.ToString();
             ErrorMessage.Visible = true;
         }
     }  


2 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 09 Dec 2008, 04:16 PM
Hi Jayanthy,

From your code I can see that you are inserting the new row on top of your data table. Have you tried using the below code for adding the new row?

public void Nrow()  
{  
    DT.Rows.Add(DT.NewRow());  
}  
 

Let me know if it makes any difference.

Greetings,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jayanthy Kapistalam
Top achievements
Rank 1
answered on 11 Dec 2008, 03:29 PM
I tried it and it didn't work either. Here is what I did and it worked.

 protected void Step2AddMore_Click(object sender, EventArgs e)
        {
               QuestionsGrid.CurrentPageIndex = 0;              
                BindDG();
        }
BindDG()
{
if (Session["GridQuestion"] == null)
                {
                    DataTable DT = new DataTable("GridQuestion");                  
                    DT.Columns.Add("Question", typeof(string));
                    DT.Columns.Add("Header", typeof(string));                   
                    Session["GridQuestion"] = DT;
                }
DataTable dt = (DataTable)Session["GridQuestion"];
 dr = dt.NewRow();
dr["Question"]=txtQuestion.Text;
dr["Header"]=txtHeader.Text;
dt.Rows.Add(dr);
QuestionsGrid.DataSource = dt;
QuestionsGrid.DataBind();
}
Tags
Grid
Asked by
Jayanthy Kapistalam
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Jayanthy Kapistalam
Top achievements
Rank 1
Share this question
or