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

Disable add new record in Grid

9 Answers 655 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Panayiotis Pelekanos
Top achievements
Rank 1
Panayiotis Pelekanos asked on 02 Feb 2009, 10:55 AM
Hi there,

Can you please help me on how to disable the add new record button in grid when there is at least one record displayed?
The add new record should appear only in the event of the datasource not returning any records.
Moreover if the button is clicked i want some fields to be auto completed (linked) based on fields appearing in a formview in the same page.
 
Thanxs!


9 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 02 Feb 2009, 11:52 AM
Hi,

You can try out the following code to disable the AddNewRecord button based on the count of records returned from the database.
cs:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
       foreach(GridCommandItem cmditm in RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)) 
            { 
                if (RadGrid1.Items.Count > 0) 
                { 
                    Button btn1 = (Button)cmditm.FindControl("AddNewRecordButton"); 
                    btn1.Visible = false
                    LinkButton lnkbtn1 = (LinkButton)cmditm.FindControl("InitInsertButton"); 
                    lnkbtn1.Visible = false
                } 
            } 
    } 

You can also pre-populate the fields in the InsertForm as shown below:
cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    {       
       if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) 
        { 
            GridEditFormInsertItem insertItem =(GridEditFormInsertItem)e.Item; 
            TextBox txtbx = (TextBox)insertItem["ColumnUniqueName"].Controls[0]; 
            txtbx.Text = "Custom Text"
        } 
         
    } 



Thanks
Princy.
0
Panayiotis Pelekanos
Top achievements
Rank 1
answered on 02 Feb 2009, 01:40 PM
Thanxs Princy your code works just fine for my scenario.
Any help on how to pass  values that are displayed either in a formview or a label when adding a new record?
I didnt understand your second example..
Thanxs again..
0
Accepted
Princy
Top achievements
Rank 2
answered on 03 Feb 2009, 05:08 AM
Hi,

You can try out the following code to populate your InsertForm fields based on FormView and Label values.
aspx:
<asp:FormView ID="FormView1" DataSourceID="SqlDataSource1" runat="server"
     <ItemTemplate> 
          <asp:Label ID="Label1" runat="server" Text="LabelText"></asp:Label> 
     </ItemTemplate> 
</asp:FormView> 
         
<asp:Label ID="Label2" runat="server" Text="CustomText"></asp:Label> 
 
 <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource2" AutoGenerateColumns="true" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" > 
    <MasterTableView CommandItemDisplay="Top" EditMode="EditForms" > 
        ..... 
    </MasterTableView> 
 </telerik:RadGrid> 

cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) 
        { 
            Label lbl = (Label)FormView1.FindControl("Label1");// Access the Label in the FormView 
            GridEditFormInsertItem insertItem =(GridEditFormInsertItem)e.Item; 
            TextBox txtbx1 = (TextBox)insertItem["Column1UniqueName"].Controls[0];// Access the textbox for column1 in the InsertForm 
            txtbx1.Text = lbl.Text;// set the text for the textbox as the FormView label text 
            TextBox txtbx2 = (TextBox)insertItem["Column2UniqueName"].Controls[0];// Access the textbox for column2 in the InsertForm 
            txtbx2.Text = Label2.Text;// set the text for the textbox as the label text 
        }         
    } 

Thanks
Princy.
0
Panayiotis Pelekanos
Top achievements
Rank 1
answered on 03 Feb 2009, 08:10 AM
Thanxs Princy working as expected :).
I will take the courage and ask again another question. Is there any way of formatting the collumns of the grid, for example i would like to format the date columns. The columns are generetated at runtime so how do i access their properties? Moreover is there any way of implementing the standart asp net validation controls in the edit and insert view of the grid?

Thanxs again for your help!
0
Shinu
Top achievements
Rank 2
answered on 03 Feb 2009, 11:16 AM
Hi Panayiotis,

You can set the DataFormatString while creating the column.

CS:
GridBoundColumn Col = new GridBoundColumn(); 
RadGrid1.MasterTableView.Columns.Add(Col); 
Col.HeaderText = "HeaderText"
Col.UniqueName = "UniqueName"
Col.DataField = "RequiredDate"
Col.DataFormatString = "{0:dddd dd/MM/yy}"

It is possible to add ASP validation controls in edit and insert view. For this you can use FormTemplate edit form, Template Column or UserControl edit form. Checkout the link below.
Flexible server-side validation

Thanks,
Shinu.
0
Panayiotis Pelekanos
Top achievements
Rank 1
answered on 03 Feb 2009, 12:17 PM
Hi there thanxs for the reply, i am using the following code at the 

RadGrid1_ColumnCreated to format the columns:

If TypeOf e.Column Is GridBoundColumn AndAlso TryCast(e.Column, GridBoundColumn).DataField = "ApplicationDate" Then 
            TryCast(e.Column, GridBoundColumn).DataFormatString = "{0:dd/MM/yyyy}" 
        End If 

now i run into another required functionality. Is it possble to replace the autogenerated column types in the edit and insert mode to something else?
For instance instead of a textbox i would like a combobox that offers the user a list of available selections (bind to a datasource). If the edit mode is fired the combobox will display the value of the selected editable record and if a new record is added the combobox should prompt the user to select a value ex ("Please select status").

A more general question though, should i disable the autogenerated columns functionality and try to establish a form template or can
 i succeed with the aforementioned functionality with code.? Any tutorials on form templates?


Thaxns for your patience :)
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 Feb 2009, 05:14 AM
Hi again,

I would suggest you to use a FormTemplate rather than replacing the controls in the Edit/Insert mode via code. For information on FormTemplate, you can refer to the following help document:
Custom edit forms

Also the following online demo should help you in creating a FormTemplate custom edit form:
Form template edit form

Thanks
Princy.
0
Bill
Top achievements
Rank 2
answered on 08 Apr 2010, 08:04 PM
I'm using 2010 1st quarter, .net 3.5.

When I tried this code, I get a null reference exception:

Line 67: Button btn1 = (Button)cmditm.FindControl("AddNewRecordButton"); Line 68: btn1.Visible = false;

0
Sebastian
Telerik team
answered on 09 Apr 2010, 02:59 PM
Hi Bill,

The id of the add new record button is InitInsertButton (refer this help topic). Hence you have to modify your code as follows:

cmditm.FindControl("InitInsertButton").Visible = false;

Best regards,
Sebastian
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Panayiotis Pelekanos
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Panayiotis Pelekanos
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Bill
Top achievements
Rank 2
Sebastian
Telerik team
Share this question
or