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

Default Insert Value for RadGrid

14 Answers 983 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 2
Michael asked on 18 Feb 2010, 12:10 PM
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. 

14 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 18 Feb 2010, 01:36 PM
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.
0
Michael
Top achievements
Rank 2
answered on 18 Feb 2010, 05:57 PM
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.
0
Daniel
Telerik team
answered on 23 Feb 2010, 08:36 PM
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.
0
Mike
Top achievements
Rank 1
answered on 24 Nov 2010, 07:03 AM
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();
  }
}
0
Daniel
Telerik team
answered on 24 Nov 2010, 10:58 PM
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.
0
Stacy
Top achievements
Rank 1
answered on 17 Jan 2011, 02:12 PM
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.
0
Princy
Top achievements
Rank 1
answered on 18 Jan 2011, 06:07 AM
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.
0
Andrew
Top achievements
Rank 1
Veteran
answered on 17 Apr 2012, 02:11 PM
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?

 

 

 

 


0
Shinu
Top achievements
Rank 1
answered on 18 Apr 2012, 08:42 AM
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.
0
Andrew
Top achievements
Rank 1
Veteran
answered on 18 Apr 2012, 01:11 PM
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
0
Shinu
Top achievements
Rank 1
answered on 18 Apr 2012, 01:38 PM
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.
0
Andrew
Top achievements
Rank 1
Veteran
answered on 18 Apr 2012, 02:14 PM
again I know that but I do not want to use the code behind I would like to do it declaratively
0
Shinu
Top achievements
Rank 1
answered on 18 Apr 2012, 02:28 PM
Hi Andrew,

Try setting the  DefaultValue in the InsertParameters.

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

Thanks,
Shinu.
0
shervinrv
Top achievements
Rank 1
answered on 01 Aug 2014, 08:01 PM
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
Tags
Grid
Asked by
Michael
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Michael
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Stacy
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Andrew
Top achievements
Rank 1
Veteran
Shinu
Top achievements
Rank 1
shervinrv
Top achievements
Rank 1
Share this question
or