Default Insert Value for RadGrid

15 posts, 0 answers
  1. Michael
    Michael avatar
    13 posts
    Member since:
    Sep 2008

    Posted 18 Feb 2010 Link to this post

    hello ,
    i am using radgrid and i would like to set a default insert value at bounded column value .but in insert mode the defualt value didn't set to the text box in edit form.. 
    please help me to find out the issue. 
  2. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 18 Feb 2010 Link to this post

    Hello Michael,

    I recommend you try the DefaultInsertValue property:
    <telerik:GridBoundColumn 
       DataField="Area" 
       HeaderText="Area"
       DefaultInsertValue="24" />

    Alternatively you could insert the desired value programatically:
    Inserting values in-place and EditForms
    Inserting values using UserControl/FormTemplate

    Best regards,
    Daniel
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Michael
    Michael avatar
    13 posts
    Member since:
    Sep 2008

    Posted 18 Feb 2010 Link to this post

    thanks for reply , 

    i tired the DefaultInsertValue property and this what i talked about but it  doesn't work.
    about the Alternatively  solution i can't do this,  i build my grid programmatically  so i can't set the default insert value easy. i will take more time and more code. 

    please help me to find a solution to make DefaultInsertValue Property works fine.
  5. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 23 Feb 2010 Link to this post

    Hello Michael,

    I'm not sure that I understand what exactly mean that "it doesn't work" - please elaborate a bit on the problem.

    Also I would like to know, which version of RadControls you use in your scenario? Could you provide an example where the DefaultInsertValue property doesn't work??

    Thank you for your assistance.

    Best regards,
    Daniel
    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.
  6. Mike
    Mike avatar
    1 posts
    Member since:
    Sep 2010

    Posted 24 Nov 2010 Link to this post

    Using Telerik.Web.UI (Telerik RadControls for ASP.NET Ajax) 2010.3.1109.40, .Net Runtime v4.0.30319.
    DefaultInsertValue is a static text while I need to set default insert value of the column to current date. I tried setting DefaultInsertValue to current date in several events of the grid, but it didn't help (value of the column editor remained empty).
    I found only one solution:

     

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
      if (e.Item is GridEditFormInsertItem) {
        GridDateTimeColumnEditor editor = (e.Item as GridEditFormInsertItem).EditManager.GetColumnEditor("WORK_DATE") as GridDateTimeColumnEditor;
        editor.Text = DateTime.Today.ToShortDateString();
      }
    }
  7. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 24 Nov 2010 Link to this post

    Hello Mike,

    There are two common ways to insert predefined values dynamically (depending on the edit mode):

    In-place and Edit forms modes:
    Inserting values in-place and EditForms 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            e.Canceled = true;
            Hashtable values = new Hashtable();
            values["Date"] = DateTime.Now;
            //...
            e.Item.OwnerTableView.InsertItem(values);
        }
    }

    Web User Control and Edit Form template:
    Inserting values using UserControl/FormTemplate

    Best regards,
    Daniel
    the Telerik team
    Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  8. Stacy
    Stacy avatar
    98 posts
    Member since:
    Dec 2010

    Posted 17 Jan 2011 Link to this post

    Hi,
    I am using a grid and when a user goes into InsertMode, I have a date control that I want to be able to set a default value for.  The user is free to change the date but I want a default value to show when the user first enters the InsertMode.  I have seen some questions in the forum but the answers seem to insert a default value in the db without the user knowing.  I want the user to be able to see the default value when they are trying to add a new record.  Any ideas?

    Thanks.
  9. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jan 2011 Link to this post

    Hello Stacy,

    ASPX:
    <telerik:GridTemplateColumn>
        <EditItemTemplate>
            <telerik:RadDatePicker runat="server" ID="RadDatePicker1" DbSelectedDate='<%#Bind("BirthDate") %>'>
            </telerik:RadDatePicker>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>

    You can try any of the following approach to achieve this.

    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                e.Canceled = true;
                Hashtable values = new Hashtable();
                values["BirthDate"] = DateTime.Now;
                e.Item.OwnerTableView.InsertItem(values);
            }
        }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
           {
               if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
               //if the grid is in insert mode
               {
                   GridEditFormInsertItem editItem = (GridEditFormInsertItem)e.Item;
                   RadDatePicker datepicker = (RadDatePicker)editItem.FindControl("RadDatePicker1");
                   datepicker.SelectedDate = DateTime.Now;
               }
           }

    Thanks,
    Princy.
  10. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 17 Apr 2012 Link to this post

    I having the same type of issue I have a value that I want to save to the database with our the user knowing on a insert only.

    I have tried this:

    <telerik:GridBoundColumn  UniqueName="translation" DataField="Translation" DefaultInsertValue="false" Display="false" />

    but then the grid goes into insert mode you can still see the column. I tried making it read only the column will disappear but then the value will not save.

    any ideas?

     

     

     

     


  11. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Apr 2012 Link to this post

    Hello Andrew,

    If you want to hide the column in insert mode, then you can set ReadOnly as true. From InsertCommand you can insert the value of the particular field by giving a default value. Here is the sample code.

    aspx:
    <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID"></telerik:GridBoundColumn>
    <telerik:GridBoundColumn UniqueName="EmployeeID" DataField="EmployeeID" HeaderText="EmployeeID"></telerik:GridBoundColumn>
    <telerik:GridBoundColumn UniqueName="ShipName" DataField="ShipName" HeaderText="ShipName" ReadOnly="true" ></telerik:GridBoundColumn>

    C#:
    protected void radgrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
      GridEditableItem editItem = (GridEditableItem)e.Item;
      string OrderId = (editItem["OrderID"].Controls[0] as TextBox).Text;
      string EmployeeID = (editItem["EmployeeID"].Controls[0] as TextBox).Text;
      string ShipName = (editItem["ShipName"].Controls[0] as TextBox).Text;
      string insertQuery = "insert into Orders (OrderID,EmployeeID,ShipName) values ('" + OrderId + "','" + EmployeeID + "','Titanic')";
      conn.Open();
      SqlCommand.CommandText = insertQuery;
      SqlCommand.Connection = conn;
      SqlCommand.ExecuteNonQuery();
      conn.Close();
    }

    Thanks,
    Shinu.
  12. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 18 Apr 2012 Link to this post

    ya I know I can do it that way but I would like to do it declaratively, you can almost do it just need to be able to hind the column in edit/insert mode
  13. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Apr 2012 Link to this post

    Hi Andrew,

    You can use OnInserting server-side events to set values dynamically. Sample code is given below.

    ASPX:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
           InsertCommand="INSERT INTO Employees (OrderID,EmployeeID,ShipName) values(@OrderID,@EmployeeID,@ShipName)"
           OnInserting="SqlDataSource1_Inserting">
           <InsertParameters>
               <asp:Parameter Name="OrderID" />
               <asp:Parameter Name="EmployeeID" />
               <asp:Parameter Name="ShipName" />
           </InsertParameters>
    </asp:SqlDataSource>

    C#:
    protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
      {
          e.Command.Parameters["@ShipName"].Value = "newvalue";
      }

    Thanks,
    Shinu.
  14. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 18 Apr 2012 Link to this post

    again I know that but I do not want to use the code behind I would like to do it declaratively
  15. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Apr 2012 Link to this post

    Hi Andrew,

    Try setting the  DefaultValue in the InsertParameters.

    ASPX:
    <InsertParameters>
        <asp:Parameter Name="ShipName" Type="String" DefaultValue="newvalue"/>
    </InsertParameters>

    Thanks,
    Shinu.
  16. shervinrv
    shervinrv avatar
    10 posts
    Member since:
    May 2007

    Posted 01 Aug 2014 Link to this post

    hi guys .
    i know this thread is old .
    but it's on top google result
    so i post here
    to insert default value use EmptyDataText="*****" property .
    it will work
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017