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

Adding a row to the telerik radgrid from a button which is outside the rad grid

3 Answers 332 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Deepika
Top achievements
Rank 1
Deepika asked on 30 Nov 2011, 06:55 AM
Hello,

This is my requirement --

1. I do not want to have the Add New Record button in Grid.
[ I have handled this as below in ItemDataBound event --]

if

(e.Item is GridCommandItem)  

//Hide Addnew and refersh button from code behind  

 

GridCommandItem cmditem = e.Item as GridCommandItem;  

if (cmditem.FindControl("InitInsertButton") != null)

 {

cmditem.FindControl("InitInsertButton").Parent.Visible = false
}

}

2. I have a button "Add New Record" outside the grid, Onclick of this, I want to insert a row in Grid.
Additional Information --
1. I ahve the below piece of code in my aspx page -

<telerik:GridEditCommandColumn UniqueName="EditColumn" ButtonType="ImageButton" HeaderText="Edit"> 

<ItemStyle Width="10%" HorizontalAlign="Center" />
<HeaderStyle Width="10%" HorizontalAlign="Center" /> 

<FooterStyle Width="10%" HorizontalAlign="Center" />
</telerik:GridEditCommandColumn>

2. When I tried the below piece of code on buttn click,

GridName.MasterTableView.ShowFooter = true;
GridName.Rebind(); 
 

I am not getting the Edit image button.

Please suggest.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Nov 2011, 07:03 AM
Hello,

Try the following to insert new record on external button click.
CS:
protected void btn_Click(object sender, EventArgs e)
  {
      GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
      commandItem.FireCommandEvent("InitInsert", null);
  }

Thanks,
Princy.
0
Deepika
Top achievements
Rank 1
answered on 30 Nov 2011, 09:16 AM
Hello Princy,

Thanks for your suggestion. But I have another problem, I have a Checkbox column in the aspx page. That I set or Reset  based on a Database returned value.

Because this is a new row, I do not have nything in DB and the Edit Item template is getting  triggered and is causing problem. bec I have below line of code --

Checked

 

 

='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "ActiveStatus")) %>'

I have this error in my log --
[Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.]

Any suggestions?
Thanks Again ..

 

0
Marin
Telerik team
answered on 02 Dec 2011, 12:58 PM
Hello Deepika,

 The error suggests that the problematic line of code is trying to be execute outside of a databound control. Do you have the checkbox inside the RadGrid or in any other databound control? Can you post a sample of the markup? Additionally you can also add a check so that the Eval method is only executed in case the item is in EditMode. 

(Container is GridEditFormInsertItem) ? false :
Convert.ToBoolean(DataBinder.Eval(Container.DataItem,
"ActiveStatus")) %>'

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Deepika
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Deepika
Top achievements
Rank 1
Marin
Telerik team
Share this question
or