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

want to assign Default value in one of the column when new row inserted

6 Answers 295 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Syed
Top achievements
Rank 1
Syed asked on 09 May 2009, 09:07 AM
Hi,

I want to put a default value in one the column  whenever a new is inserted. This is hidden column.

Suppose i have three fields :  productNumber,ProductName,ProductTitle_Hidden

I will display two fields and ProductTitle_Hidden is hidden field and i want to add some default value from a label which is placed on the form.

Its like  ProductTitle_Hidden=label1.text

whenever a new row created this should be added automatically.

Thanks.

Regards
Syed Arshad

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 11 May 2009, 06:46 AM
Hi Syed,

You may try accessing the textbox in edit mode in the ItemDataBound event and assign the value to it from the label 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["ProductTitle_Hidden"].Controls[0]; 
            txtbx.Text = Label1.Text; 
        } 
    } 
 
 protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e) 
    { 
      //access the newly inserted value here 
        GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item; 
        TextBox txtbx = (TextBox)insertItem["ProductTitle_Hidden"].Controls[0]; 
        string strxt = txtbx.Text; 
    } 


Regards
Shinu
0
Syed
Top achievements
Rank 1
answered on 11 May 2009, 08:59 AM
I try it but nothing is updating/Inserting the Label text value in the hidden field.  If i insert, in database i find null and edited nothing changes.

Please below is following code and aspx .

VB
----
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)  
        If (TypeOf e.Item Is GridEditFormInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then  
            Dim insertItem As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)  
            Dim txtbx As TextBox = DirectCast(insertItem("sname").Controls(0), TextBox)  
            txtbx.Text = Label1.Text  
        End If  
    End Sub  
 
    Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)  
        'access the newly inserted value here    
        Dim insertItem As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)  
        Dim txtbx As TextBox = DirectCast(insertItem("sname").Controls(0), TextBox)  
        Dim strxt As String = txtbx.Text  
    End Sub 
ASPX
<body> 
    <form id="form1" runat="server">  
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <br /> 
        <br /> 
        <div> 
            <asp:Label ID="Label1" runat="server" Text="Arshad"></asp:Label><br /> 
            <br /> 
            <br /> 
            <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" 
                AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateDeleteColumn="True" 
                AutoGenerateEditColumn="True" DataSourceID="SqlDataSource1" GridLines="None">  
                <MasterTableView AutoGenerateColumns="False" CellSpacing="-1" CommandItemDisplay="Top" 
                    DataSourceID="SqlDataSource1">  
                    <RowIndicatorColumn> 
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                    <ExpandCollapseColumn> 
                        <HeaderStyle Width="20px" /> 
                    </ExpandCollapseColumn> 
                    <Columns> 
                        <telerik:GridBoundColumn DataField="sno" DataType="System.Int32" HeaderText="sno" 
                            SortExpression="sno" UniqueName="sno">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="sname" Display="False" HeaderText="sname" ReadOnly="True" 
                            SortExpression="sname" UniqueName="sname">  
                        </telerik:GridBoundColumn> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" 
                ConnectionString="<%$ ConnectionStrings:peoplesConnectionString %>" DeleteCommand="DELETE FROM [cares] WHERE [sno] = @original_sno AND (([sname] = @original_sname) OR ([sname] IS NULL AND @original_sname IS NULL))" 
                InsertCommand="INSERT INTO [cares] ([sno], [sname]) VALUES (@sno, @sname)" OldValuesParameterFormatString="original_{0}" 
                SelectCommand="SELECT [sno], [sname] FROM [cares]" UpdateCommand="UPDATE [cares] SET [sname] = @sname WHERE [sno] = @original_sno AND (([sname] = @original_sname) OR ([sname] IS NULL AND @original_sname IS NULL))">  
                <DeleteParameters> 
                    <asp:Parameter Name="original_sno" Type="Int32" /> 
                    <asp:Parameter Name="original_sname" Type="String" /> 
                </DeleteParameters> 
                <UpdateParameters> 
                    <asp:Parameter Name="sname" Type="String" /> 
                    <asp:Parameter Name="original_sno" Type="Int32" /> 
                    <asp:Parameter Name="original_sname" Type="String" /> 
                </UpdateParameters> 
                <InsertParameters> 
                    <asp:Parameter Name="sno" Type="Int32" /> 
                    <asp:Parameter Name="sname" Type="String" /> 
                </InsertParameters> 
            </asp:SqlDataSource> 
        </div> 
    </form> 
</body> 


 
0
Syed
Top achievements
Rank 1
answered on 11 May 2009, 10:24 AM
 <telerik:GridBoundColumn DataField="sname" Display="False" HeaderText="sname" ReadOnly="True" 
                            SortExpression="sname" UniqueName="sname">  

if i put ReadOnly='true', the data is not storing. But when its false, its taking value from label1 and storing in database.

The column is invisible in grid but visible in edit/insert mode.

If i make invisible in grid and visible in these two modes.

Please tell me the solution as soon as possible.

Thanks.
0
Shinu
Top achievements
Rank 2
answered on 12 May 2009, 05:09 AM
Hi Syed,

Try setting the ForceExtractValue property which force RadGrid to extract values from EditableColumns that are ReadOnly (or IsEditable is false). 
ForceExtractValue Property

Shinu
0
Syed
Top achievements
Rank 1
answered on 12 May 2009, 09:49 AM
I don't find examples how to use this property. Can you give some sample code so that i can understand how to use it.

Thanks.

Regards
Syed Arshad
0
Shinu
Top achievements
Rank 2
answered on 12 May 2009, 10:59 AM
Hi Syed,

Try setting the ForceExtractValue property for the Grid column to Always as shown below.

CS:
 
   <telerik:GridBoundColumn UniqueName="ProductName" ForceExtractValue="Always" DataField="ProductName" HeaderText="ProductName" > 
                  </telerik:GridBoundColumn> 

Shinu.
Tags
Grid
Asked by
Syed
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Syed
Top achievements
Rank 1
Share this question
or