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

Get GridDropDownColumn SelectedValue after insert/edit

7 Answers 263 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marc
Top achievements
Rank 1
Marc asked on 07 Jan 2013, 04:21 PM
Hi all,

I have a grid with the structure shown bellow. I would like to fill the SkuId Label when a selectedvalue on the radcombobox exists. I have a function to obtain the SKU data from the combo selectedValue (actually the productID from where I can obtain the SKU) when it's in edit mode. The problem is that I am not able to set the text property for the label because after an insert or an update I loose my combo selected value info.

I cannot figure how to write the SKU text in any grid condition... maybe an AJAX / independent approach would be better.

The grid allows automatic inserts and updates and the information is binded from an EF datasource. The SKU column has no bindings because it's only a visual reference for the user entering the information and should not be saved on the DB.

Thanks!
<telerik:GridBoundColumn DataField="Id" DataType="System.Int32" FilterControlAltText="Filter Id column" HeaderText="Id" ReadOnly="True" SortExpression="Id" UniqueName="Id" Visible="false"/>
                <telerik:GridBoundColumn DataField="OrderId" DataType="System.Int32" FilterControlAltText="Filter OrderId column" HeaderText="Order" SortExpression="OrderId" UniqueName="OrderId" Visible="false" ReadOnly="true" />
                 
                <telerik:GridDropDownColumn DataSourceID="SqlDSProductList" ListTextField="Name" ListValueField="Id" HeaderStyle-Width="400px"
                    UniqueName="VariantId" SortExpression="Name" HeaderText="Product" DataField="ProductVariantId"
                    DropDownControlType="RadComboBox" AllowAutomaticLoadOnDemand="true" AllowVirtualScrolling="true" ShowMoreResultsBox="true" ItemsPerRequest="25">
                </telerik:GridDropDownColumn>
 
                <telerik:GridTemplateColumn UniqueName="SKU">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="SkuId" OnLoad="SKU"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label runat="server" ID="SkuId" Text="" OnLoad="SKU"></asp:Label>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>

7 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 07 Jan 2013, 08:34 PM
Protected Sub gvVendor_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gvVendor.ItemDataBound
Dim dgItem As GridDataItem = Nothing
Dim litVariantID As Literal = Nothing
 
If TypeOf e.Item Is IGridInsertItem Then
ElseIf TypeOf e.Item Is GridDataItem Then
    dgItem = CType(e.Item, GridDataItem)
Else
    Exit Sub
End If
 
If TypeOf gdItem("VariantID").Controls(0) Is Literal Then
    litVariantID = CType(gdItem("VariantID").Controls(0), Literal)
    litVariantID.Text = what is it you want it to be?
End If
0
Shinu
Top achievements
Rank 2
answered on 08 Jan 2013, 04:51 AM
Hi,

Try the following code to achieve your scenario.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
 {
            GridEditableItem item = (GridEditableItem)e.Item;
            RadComboBox cmb = (RadComboBox)item["VariantId"].Controls[0];
            cmb.AutoPostBack = true;
            cmb.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(cmb_SelectedIndexChanged);
 }
}
void cmb_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
        RadComboBox cmb=(RadComboBox)sender;
        GridEditableItem item = (GridEditableItem)cmb.NamingContainer;
        Label lbl = (Label)item.FindControl("SkuId");
        lbl.Text = cmb.SelectedValue.ToString();
}

Thanks,
Shinu.
0
Marc
Top achievements
Rank 1
answered on 08 Jan 2013, 03:17 PM
Hi,

Thanks for your fast reply.

I already tried this approach, but unfortunately it doesn't work. After inserting or editing a row in the grid, this command is no longer firing since we are not in the edit mode and so, the label in the ItemTemplate section is not being updated.
My assumption is also that all edited values are lost when rebinding the grid after an insert or an update, because the gridtemplatecolumn is not binded to a datasource, and hence all written information is not being stored in any place. I think that the label is in fact initialized to its original status after any change on the grid.

I am now testing to access the cell after committing the changes to the ds (after the edit mode). Attaching a function on the onload event of the label to query the text to display (SKU) is easy, but unfortunately I cannot obtain the SelectedValue from the RadComboBox because it's not in edit mode and the combo has been replaced by the text to display only...

Any suggestions?
0
Shinu
Top achievements
Rank 2
answered on 09 Jan 2013, 09:21 AM
Hi,

Try the following code to update the ItemTemplate field after editing.
C#:
void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = e.Item as GridEditableItem;
       RadComboBox cmb = (RadComboBox)item["VariantId"].Controls[0];
      if (Session["updatedValue"] != null)
        {
            cmb.SelectedValue = Session["updatedValue"];
        }
    }
    else if (e.Item is GridDataItem && !e.Item.IsInEditMode)
    {
        GridDataItem item = e.Item as GridDataItem;
        Label label = item.FindControl("SkuId") as Label;
        label.Text = Session["updatedValue"];
    }
}

Thanks,
Shinu.
0
Marc
Top achievements
Rank 1
answered on 11 Jan 2013, 03:23 PM
Hi,

Definitively, the code doesn’t works. Using the suggested method all SKUId Labels en each row are updated with the latest session value and not with the combobox corresponding value in each row...

Also, using this method implies to query the full table on each databound, including the cells that are already populated...
 
It should be an easier and more performing option...

Maybe a client-side approach will be better.

0
Accepted
Shinu
Top achievements
Rank 2
answered on 29 Jan 2013, 09:16 AM
Hi,

Please take a look into the following code snippet I tried which worked as expected.

ASPX:
<telerik:RadGrid ID="radgrid1" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
    AutoGenerateEditColumn="true" AllowAutomaticUpdates="true" runat="server" OnUpdateCommand="radgrid1_UpdateCommand">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="ProductID" DataType="System.Int32" FilterControlAltText="Filter Id column"
                HeaderText="Id" ReadOnly="false" SortExpression="Id" UniqueName="Id" Visible="true" />
            <telerik:GridBoundColumn DataField="CategoryID" DataType="System.Int32" FilterControlAltText="Filter OrderId column"
                HeaderText="Order" SortExpression="OrderId" UniqueName="OrderId" Visible="true"
                ReadOnly="true" />
            <telerik:GridDropDownColumn DataSourceID="SqlDataSource2" ListTextField="CategoryName"
                ListValueField="CategoryID" HeaderStyle-Width="400px" UniqueName="VariantId"
                SortExpression="Name" HeaderText="Product" DataField="CategoryID" DropDownControlType="RadComboBox"
                AllowVirtualScrolling="true" ShowMoreResultsBox="true" ItemsPerRequest="25">
            </telerik:GridDropDownColumn>
            <telerik:GridTemplateColumn UniqueName="SKU">
                <ItemTemplate>
                    <asp:Label runat="server" ID="SkuId"></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label runat="server" ID="SkuId"></asp:Label>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString3 %>"
    DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [CategoryID], [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock]) VALUES (@ProductName, @CategoryID, @UnitPrice, @Discontinued, @QuantityPerUnit, @UnitsInStock)"
    SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock] FROM [Products]"
    UpdateCommand="UPDATE [Products] SET  [CategoryID] = @CategoryID WHERE [ProductID] = @ProductID">
    <DeleteParameters>
        <asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
        <asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
        <asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
        <asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
        <asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
        <asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
        <asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
        <asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
        <asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
        <asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
        <asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
        <asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
    </UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString3 %>"
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
</asp:SqlDataSource>

C#:
protected void radgrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem ditem = (GridDataItem)e.Item;
        string selectedvalue = ditem["VariantId"].Text;
        Label lbl2 = (Label)ditem.FindControl("SkuId");
        lbl2.Text = selectedvalue;
    }
}

Please refer this forum thread also.


Thanks,
Shinu.
0
Marc
Top achievements
Rank 1
answered on 19 Feb 2013, 12:22 AM
Excellent answer! this is just exactly what I need.

Thank you so much!
Tags
Grid
Asked by
Marc
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Marc
Top achievements
Rank 1
Share this question
or