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

Adding new row - strainge behavior

3 Answers 136 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yuriy Rogach
Top achievements
Rank 1
Yuriy Rogach asked on 13 Oct 2008, 08:19 PM
Hi,

I have a very simple question, how to add new row to radgridview properly ?
I need to add new row from clicking on my button, and begin edit it.

OK, let me describe what is ugly from my point of view.

I have binding source connected with gridview. Here is my buttonClick event handler:
 private void button1_Click(object sender, EventArgs e)  
        {  
            bindingSource1.AddNew();  
            radGridView2.Rows[radGridView2.Rows.Count - 1].Cells[1].BeginEdit();  
        } 

If
 
radGridView2.MasterGridViewTemplate.AllowAddNewRow = true

everything is OK, but if
radGridView2.MasterGridViewTemplate.AllowAddNewRow = false

behavior is ugly. Clicking first time, I'm getting new row selected, but not in edit mode, clicking one more and getting second row selected but not in edit mode (two rows selected !), and finally clicking one more time, I'm getting new row in  edit mode.

I need to add new rows, but don't need "Click here to add new row" label (functionality)

Thank you.

3 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 14 Oct 2008, 03:12 PM
Hi Yuriy Rogach,

Thank you for your question.

Please set AllowAddNewRow to false and check the following code snippet:

BindingList<CustomObject> l;  
 
private void Form1_Load(object sender, EventArgs e)  
{  
 
            l = new BindingList<CustomObject>();  
            l.Add(new CustomObject("one"));  
            l.Add(new CustomObject("two"));  
 
            radGridView1.DataSource = l;    
 
}  
 
          
private void radButton1_Click(object sender, EventArgs e)  
{  
       l.Add(new CustomObject("three"));  
       this.radGridView1.Rows[this.radGridView1.Rows.Count - 1].IsCurrent= true;
}  
 
....  
 
class CustomObject  
{  
        string a;  
        public CustomObject(String a)  
        {  
            this.a = a;  
        }  
 
        public string A  
        {  
 
 
            get { return a; }  
            set { a = value; }  
        }  
 

The easiest way to add the new row is to modify your datasource. In the case above, the BindingList<CustomObject>.

You may experience a problem with the scrollbars which will be fixed in the next release.

 
Regards,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Yuriy Rogach
Top achievements
Rank 1
answered on 15 Oct 2008, 09:12 AM
Hi Nick,

Thanks for reply, but there is a bug inside.

I need to add new row and begin edit it.

Modify your code snippet as follow:

remove these two lines, so binding source will be empty

l.Add(new CustomObject("one"));  
l.Add(new CustomObject("two"));  

then modify event handler as follow

private void radButton1_Click(object sender, EventArgs e)  
{  
       l.Add(new CustomObject("three"));  
       this.radGridView1.Rows[this.radGridView1.Rows.Count - 1].IsCurrent= true;
       this.radGridView1.SelectedRows[0].Cells[1].BeginEdit();
}

then test it, clicking first time you will get inserted row selected,but not in edit mode, second click - two! rows selected, no one in edit mode, third click - third rows selected and in edit mode. So behaviour is good when grid has more then two rows, which is bad for me. And yes, scrooling is annoy, but not critical.
0
Nick
Telerik team
answered on 15 Oct 2008, 02:17 PM
Hi Yuriy,

We managed to reproduce this with your instructions. We will try to provide a fix for the upcoming Q3 release. I have updated your Telerik points for the report.
 

All the best,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Yuriy Rogach
Top achievements
Rank 1
Answers by
Nick
Telerik team
Yuriy Rogach
Top achievements
Rank 1
Share this question
or