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

Set value of bound field during InsertCommand

5 Answers 48 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 02 Jul 2013, 08:36 AM
I have a bound hidden control in a RadGrid that contains a BidID field.  During the InsertCommand event, I want to set the value of this hidden control.  I'm new to Telerik and for the life of me, I just can't figure this out.  

The closest thing I could come up with was this but it's not correct.  How do I access the value of a bound control, before the insert command is sent to SQL Server, and set a value?

Dim row As GridDataInsertItem = DirectCast(e.Item, GridDataItem)
        row.Item("BidID").Text = intBidID

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Jul 2013, 09:32 AM
Hello,

1. EditMode="InPlace"
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound"
    OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView CommandItemDisplay="Top" EditMode="InPlace">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn>
            </telerik:GridEditCommandColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
    Dim data As dynamic = New () {New With { _
        .ID = 1, _
        .Name = "Name1" _
    }, New With { _
        .ID = 2, _
        .Name = "Name2" _
    }, New With { _
        .ID = 3, _
        .Name = "Name3" _
    }, New With { _
        .ID = 4, _
        .Name = "Name4" _
    }, New With { _
        .ID = 5, _
        .Name = "Name5" _
    }}
 
    RadGrid1.DataSource = data
End Sub
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataInsertItem AndAlso e.Item.IsInEditMode Then
        Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
        item("ID").Text = "0"
    End If
End Sub

2.  EditMode="EditForms"

e.Item Is GridEditFormInsertItem


If you want to display inside TextBox control then.

TryCast(item("ID").Controls(0), TextBox).Text = "0"


Thanks,
Jayesh Goyani
0
Jon
Top achievements
Rank 1
answered on 02 Jul 2013, 10:08 AM
Ok, this is working almost perfectly.  One small question:  There is a RadDropDownList that does a postback when the user selects a different item (for a cascading effect).  When the postback occurs, the ID control loses its value.  How can I force it to retain the value?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Jul 2013, 10:59 AM
Hello,

is this dropDownList is inside Grid?

If possible then please provide your code.

Thanks,
Jayesh Goyani
0
Jon
Top achievements
Rank 1
answered on 02 Jul 2013, 11:16 AM
Yes, it's a ddl within the grid.  It is used to track Product Types.

<telerik:RadDropDownList ID="radddlProductTypes" runat="server" DataSourceID="sqlProductTypes" DataTextField="ProductType" DataValueField="ProductTypeID"
SelectedValue='<%# Bind("ProductTypeID") %>' Skin="Forest" AutoPostBack="True" OnSelectedIndexChanged="radddlProductTypes_SelectedIndexChanged">
</telerik:RadDropDownList>

When the postback occurs, the BidID is lost
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Jul 2013, 12:30 PM
Hello,

Please try with the below code snippet.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound"
        OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView CommandItemDisplay="Top" EditMode="InPlace">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <EditItemTemplate>
                        <telerik:RadDropDownList ID="radddlProductTypes" runat="server" DataSourceID="SqlDataSource1"
                            DataTextField="AcademyName" DataValueField="AcademyID" SelectedValue='<%# Bind("ID") %>'
                            AutoPostBack="True" OnSelectedIndexChanged="radddlProductTypes_SelectedIndexChanged">
                        </telerik:RadDropDownList>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JayeshTestConnectionString %>"
        SelectCommand="SELECT [AcademyID], [AcademyName] FROM [AcademyDetail]"></asp:SqlDataSource>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       dynamic data = new[] {
           new { ID = 1, Name ="Name1"},
           new { ID = 2, Name = "Name2"},
           new { ID = 3, Name = "Name3"},
            new { ID = 4, Name = "Name4"},
           new { ID = 5, Name = "Name5"}
       };
 
       RadGrid1.DataSource = data;
   }
 
   protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataInsertItem && e.Item.IsInEditMode)
       {
           GridEditableItem item = e.Item as GridEditableItem;
           (item["ID"].Controls[0] as TextBox).Text = "99";
       }
   }
   protected void radddlProductTypes_SelectedIndexChanged(object sender, DropDownListEventArgs e)
   {
 
   }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Jon
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Jon
Top achievements
Rank 1
Share this question
or