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

In row of RadGrid, 2 Comboboxes, unable to retrieve new value of first dropdown on Update/Insert Command

17 Answers 259 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Darryl
Top achievements
Rank 1
Darryl asked on 02 May 2013, 02:11 PM

Hello everyone,

I am currently building a RadGrid that is bound to an empty dataset. In this dataset, a new row will allow a selection of parameters, which will make up a query (eventually).

Each row has two dropdowns, the first one drives the second one's value. Both values are required for the end result.
Currently, when I click "Insert new" it creates the new row, I can select a value from the first dropdown, and it populates the second. When I click "Insert" to store the data, the data stored is the original value of the first dropdown (Unique Name: ObjectName) and the selected (correct value) of Unique Name: CounterName. This problem occurs both on Update and Insert, so I suspect its the same cause.

After trying to look up values, use hash tables, etc---I have not quite figured out what I am doing wrong. Suggestions would be appreciated, I suspect its something small.

Thanks!

<telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowEdit="True" EnableViewState="false"
    AutoGenerateEditColumn="True"
    OnNeedDataSource="RadGrid1_NeedDataSource"
    OnUpdateCommand="RadGrid1_UpdateCommand"
    OnInsertCommand="RadGrid1_InsertCommand"
    OnItemDataBound="RadGrid1_ItemDataBound"
    AutoGenerateColumns="False"
    ShowStatusBar="True"
    AllowAutomaticUpdates="False"
    AllowAutomaticInserts="False">
    <ExportSettings>
        <Pdf AllowPrinting="False" />
    </ExportSettings>
    <MasterTableView EditMode="InPlace" CommandItemDisplay="Top" DataKeyNames="ParamID">
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
        </RowIndicatorColumn>
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridDropDownColumn HeaderText="Objects" UniqueName="ObjectName" runat="server" DataSourceID="Avail_Objects_ByServer" ListValueField="ObjectName" ListTextField="ObjectName" DataField="ObjectName"  />
            <telerik:GridDropDownColumn HeaderText="Counters" UniqueName="CounterName" runat="server" ListValueField="CounterName" ListTextField="CounterName" DataField="CounterName" />
        </Columns>
        <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
            </EditColumn>
        </EditFormSettings>
        <PagerStyle PageSizeControlType="RadComboBox" />
    </MasterTableView>
    <PagerStyle PageSizeControlType="RadComboBox" />
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
</telerik:RadGrid>

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem editedItem = e.Item as GridEditableItem;
    GridEditManager editMan = editedItem.EditManager;
    DataTable ordersTable = this.GridSource;
    DataRow newRow = ordersTable.NewRow();
 
    newRow["ParamID"] = (int)this.GridSource.Rows.Count + 1;
    foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
    {
        if (column is IGridEditableColumn)
        {
            IGridEditableColumn editableCol = (column as IGridEditableColumn);
            if (editableCol.IsEditable)
            {
                IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
 
                string editorText = "unknown";
                object editorValue = null;
                if (editor is GridDropDownColumnEditor)
                {
                    editorText = (editor as GridDropDownColumnEditor).SelectedText + "; "
                     (editor as GridDropDownColumnEditor).SelectedValue; 
 
                    editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                    newRow[column.UniqueName] = editorValue;
                }
            }
        }
    }
 
     
 
    object Test1 = newRow[0];
    object Test2 = newRow[1];
    object Test3 = newRow[2];
 
    //ordersTable.Rows.Add(newRow);
    this.GridSource.AcceptChanges();
}

17 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 May 2013, 05:09 AM
Hi,

Please try the following code snippet.

C#:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    //your code...
    
    GridEditableItem editItem = (GridEditableItem)e.Item;
    RadComboBox comboObjectName = (RadComboBox)editItem["ObjectName"].Controls[0];
    string Object = comboObjectName.SelectedValue;
    RadComboBox comboCounterName = (RadComboBox)editItem["CounterName"].Controls[0];
    string Counter = comboCounterName.SelectedValue;
}

Thanks,
Princy.
0
Darryl
Top achievements
Rank 1
answered on 03 May 2013, 12:32 PM
Hi Princy, thanks for your assistance.

I tried the code, to found that my editItem is throwing an error: editItem.KeyValues through an exception of System.ArgumentOutOfRangeException.

That may have been the origin to my problem...
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 May 2013, 12:55 PM
Hello,

In insert mode your DataKeyValue is always null.

This error is raised because you are trying to get DataKeyvalues in insert mode.

Thanks,
Jayesh Goyani
0
Darryl
Top achievements
Rank 1
answered on 03 May 2013, 12:58 PM
Hello Jayesh,

I understand that it will be null, makes sense until its set. In edit mode I should be okay to use the DataKeyValue. But in Insert, how do I resolve this problem? The example: http://www.telerik.com/help/aspnet-ajax/grid-inserting-values-inplace-and-editforms.html does it the same way, so I am having troubles finding a better way to do it.

Thanks!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 May 2013, 04:14 AM
Hello,

can you please provide code from where this error raised?

Thanks,
Jayesh Goyani
0
Darryl
Top achievements
Rank 1
answered on 05 May 2013, 07:17 PM
Hello Jayesh,

Its the first line that errors in the code-behind:GridEditableItem editedItem = e.Item as GridEditableItem;

As per your note below, I am getting the error because I am getting DataKeyValues at insert, which no data key exists at that moment. But the insert samples provided use this exact solution....so I am at a loss for a solution.

Thanks for your help!
0
Darryl
Top achievements
Rank 1
answered on 15 May 2013, 01:00 PM
Anyone have a suggestion? I am still at a loss on this issue.

Thanks.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 May 2013, 05:43 AM
Hello,

Please try with below code snippet.

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridDataInsertItem editedItem = e.Item as GridDataInsertItem;
}


Let me know if any concern.

Can you please submit your whole grid related code?

Thanks,
Jayesh Goyani
0
Darryl
Top achievements
Rank 1
answered on 16 May 2013, 12:15 PM
Hello Jayesh,

Thanks for looking into this...

I have used the following code:
GridDataItem insertedItem = (GridDataItem)e.Item;
string ON = (insertedItem["ObjectName"].Controls[0] as RadComboBox).SelectedValue;
string CN = (insertedItem["CounterName"].Controls[0] as RadComboBox).SelectedValue;

But oddly, my ObjectName combobox, is the wrong value. It is the original value that the database loaded, and not the value selected. my CounterName is correct, and it does retrieve as appropriate.

Code behind:

private DataTable GridSource
{
    get
    {
        Object obj = this.ViewState["_gds"];
        if (obj != null)
        {
            return (DataTable)obj;
        }
        else
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("ParamID");
            dt.Columns.Add("ObjectName");
            dt.Columns.Add("CounterName");
            ds.Tables.Add(dt);
 
            dt.NewRow();
            DataRow dr;
            dr = dt.NewRow();
            dr[0] = "0";
            dr[1] = "Memory";
            dr[2] = "Pages/sec";
            dt.Rows.Add(dr);
 
            dt.NewRow();
            DataRow dr2;
            dr2 = dt.NewRow();
            dr2[0] = "1";
            dr2[1] = "Memory";
            dr2[2] = "Available MBytes";
            dt.Rows.Add(dr2);
 
            //dt.NewRow();
            //DataRow dr3;
            //dr3 = dt.NewRow();
            //dr3[0] = "2";
            //dr3[1] = "LogicalDisk";
            //dr3[2] = "Free Megabytes";
            //dt.Rows.Add(dr3);
 
            this.ViewState["_gds"] = dt;
            return dt;
        }
    }
}
 
protected void Page_Load(object sender, EventArgs e)
{
    //required due to RadPanel
    RadGrid MyGrid = (RadGrid)(RadPanelBar1.FindItemByValue("PanelItem1").FindControl("RadGrid1"));
    MyGrid.ItemDataBound += RadGrid1_ItemDataBound;
 
    //use w/o RadPanel
    //RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
 
}
 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    //required due to RadPanel
    RadGrid MyGrid = (RadGrid)(RadPanelBar1.FindItemByValue("PanelItem1").FindControl("RadGrid1"));
    MyGrid.DataSource = this.GridSource;
 
    //use w/o RadPanel
    //RadGrid1.DataSource = this.GridSource;
}
 
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem editedItem = e.Item as GridEditableItem;
    GridEditManager editMan = editedItem.EditManager;
    foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
    {
        if (column is IGridEditableColumn)
        {
            IGridEditableColumn editableCol = (column as IGridEditableColumn);
            if (editableCol.IsEditable)
            {
                IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
                string editorText = "unknown";
                object editorValue = null;
 
                if (editor is GridTextColumnEditor)
                {
                    editorText = (editor as GridTextColumnEditor).Text;
                    editorValue = (editor as GridTextColumnEditor).Text;
                }
 
                if (editor is GridDropDownColumnEditor)
                {
                    editorText = (editor as GridDropDownColumnEditor).SelectedText;
                    editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                }
 
                DataRow[] changedRows = this.GridSource.Select("ParamID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ParamID"].ToString());
                changedRows[0][column.UniqueName] = editorValue;
                this.GridSource.AcceptChanges();
            }
        }
    }
}
 
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridDataItem insertedItem = (GridDataItem)e.Item;
    string ON = (insertedItem["ObjectName"].Controls[0] as RadComboBox).SelectedValue;
    string CN = (insertedItem["CounterName"].Controls[0] as RadComboBox).SelectedValue;
    Hashtable newValues = new Hashtable();
    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, insertedItem);
    //((GridEditableItem)e.Item).ExtractValuesFromItem(newValues, insertedItem);
 
    //RadComboBox test = (RadComboBox)insertedItem.FindControl("ObjectName");
    //string test2 = test.SelectedValue;
 
    ////ordersTable.Rows.Add(newRow);
    //this.GridSource.AcceptChanges();
 
}
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        RadComboBox combo = (RadComboBox)item["ObjectName"].Controls[0];
        combo.AutoPostBack = true;
        combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
    }
}
 
void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    string sql = "SELECT pcv.CounterName FROM PerformanceDataAllView AS pdv WITH (NOLOCK) INNER JOIN PerformanceCounterView AS pcv WITH (NOLOCK)  ON pdv.PerformanceSourceInternalId = pcv.PerformanceSourceInternalId INNER JOIN BaseManagedEntity AS bme WITH (NOLOCK) ON pcv.ManagedEntityId = bme.BaseManagedEntityId WHERE bme.Path LIKE @Server and pcv.ObjectName IN (@ObjectName) GROUP BY pcv.CounterName";
    SqlDataAdapter adapter = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["DB_OperationsManager"].ConnectionString);
    adapter.SelectCommand.Parameters.AddWithValue("@Server", rtbServerName.Text);
    adapter.SelectCommand.Parameters.AddWithValue("@ObjectName", e.Value);
    DataTable dt = new DataTable();
    int Count = adapter.Fill(dt);
 
    RadComboBox rcbObjectValue = (RadComboBox)sender;
    //GridDataItem editItem = (GridDataItem)rcbObjectValue.NamingContainer;
    //RadComboBox rcbCounterName = (RadComboBox)editItem.FindControl("CounterName");
 
    GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
    RadComboBox rcbCounterName = editedItem["CounterName"].Controls[0] as RadComboBox;
 
    rcbCounterName.ClearSelection();
    rcbCounterName.DataSource = dt;
    rcbCounterName.DataTextField = "CounterName";
    rcbCounterName.DataValueField = "CounterName";
    rcbCounterName.DataBind();
}
0
Darryl
Top achievements
Rank 1
answered on 16 May 2013, 06:56 PM
I believe the problem is linked to the AutoPostBack = true; on the ItemDataBound()
I find that if I turn the postback off, the value (appears to be) kept correctly. But then my 2nd dropdown isn't populated.  At least, that is what I believe is the problem. Your input would be appreciated.

 

 

 

 

0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 May 2013, 12:45 PM
Hello,

SQL Script.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Product](
    [Cust] [varchar](25) NULL,
    [Product] [varchar](20) NULL,
    [QTY] [int] NULL,
    [ObjectName] [nvarchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Product] ([Cust], [Product], [QTY], [ObjectName]) VALUES (N'KATE', N'VEG', 2, N'Memory6')
INSERT [dbo].[Product] ([Cust], [Product], [QTY], [ObjectName]) VALUES (N'KATE', N'SODA', 6, N'Memory1')
INSERT [dbo].[Product] ([Cust], [Product], [QTY], [ObjectName]) VALUES (N'KATE', N'MILK', 1, N'Memory2')
INSERT [dbo].[Product] ([Cust], [Product], [QTY], [ObjectName]) VALUES (N'KATE', N'BEER', 12, N'Memory3')
INSERT [dbo].[Product] ([Cust], [Product], [QTY], [ObjectName]) VALUES (N'FRED', N'MILK', 3, N'Memory4')
INSERT [dbo].[Product] ([Cust], [Product], [QTY], [ObjectName]) VALUES (N'FRED', N'BEER', 24, N'Memory')
INSERT [dbo].[Product] ([Cust], [Product], [QTY], [ObjectName]) VALUES (N'KATE', N'VEG', 3, N'Memory5')

.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Forum.aspx.cs" Inherits="Forum" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowEdit="True" EnableViewState="false"
        AutoGenerateEditColumn="True" OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
        OnInsertCommand="RadGrid1_InsertCommand" OnItemDataBound="RadGrid1_ItemDataBound"
        AutoGenerateColumns="False" ShowStatusBar="True" AllowAutomaticUpdates="False"
        AllowAutomaticInserts="False">
        <ExportSettings>
            <Pdf AllowPrinting="False" />
        </ExportSettings>
        <MasterTableView EditMode="InPlace" CommandItemDisplay="Top" DataKeyNames="ParamID">
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
            </RowIndicatorColumn>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridDropDownColumn HeaderText="Objects" UniqueName="ObjectName" runat="server"
                    DataSourceID="SqlDataSource1" ListValueField="ObjectName" ListTextField="ObjectName"
                    DataField="ObjectName" />
                <telerik:GridDropDownColumn HeaderText="Counters" UniqueName="CounterName" runat="server"
                    ListValueField="CounterName" ListTextField="CounterName" DataField="CounterName" />
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
            <PagerStyle PageSizeControlType="RadComboBox" />
        </MasterTableView>
        <PagerStyle PageSizeControlType="RadComboBox" />
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:JayeshTestConnectionString %>"
        SelectCommand="SELECT [Cust], [Product], [QTY], [ObjectName] FROM [Product]"></asp:SqlDataSource>
    </form>
</body>
</html>


.ASPX.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
 
public partial class Forum : System.Web.UI.Page
{
 
    private DataTable GridSource
    {
        get
        {
            Object obj = this.ViewState["_gds"];
            if (obj != null)
            {
                return (DataTable)obj;
            }
            else
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("ParamID");
                dt.Columns.Add("ObjectName");
                dt.Columns.Add("CounterName");
                ds.Tables.Add(dt);
 
                dt.NewRow();
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "0";
                dr[1] = "Memory";
                dr[2] = "Pages/sec";
                dt.Rows.Add(dr);
 
                dt.NewRow();
                DataRow dr2;
                dr2 = dt.NewRow();
                dr2[0] = "1";
                dr2[1] = "Memory";
                dr2[2] = "Available MBytes";
                dt.Rows.Add(dr2);
 
                //dt.NewRow();
                //DataRow dr3;
                //dr3 = dt.NewRow();
                //dr3[0] = "2";
                //dr3[1] = "LogicalDisk";
                //dr3[2] = "Free Megabytes";
                //dt.Rows.Add(dr3);
 
                this.ViewState["_gds"] = dt;
                return dt;
            }
        }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        //required due to RadPanel
         
        RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
 
        //use w/o RadPanel
        //RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
 
    }
 
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        //required due to RadPanel
 
        RadGrid1.DataSource = this.GridSource;
 
        //use w/o RadPanel
        //RadGrid1.DataSource = this.GridSource;
    }
 
    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridEditManager editMan = editedItem.EditManager;
        foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
        {
            if (column is IGridEditableColumn)
            {
                IGridEditableColumn editableCol = (column as IGridEditableColumn);
                if (editableCol.IsEditable)
                {
                    IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
                    string editorText = "unknown";
                    object editorValue = null;
 
                    if (editor is GridTextColumnEditor)
                    {
                        editorText = (editor as GridTextColumnEditor).Text;
                        editorValue = (editor as GridTextColumnEditor).Text;
                    }
 
                    if (editor is GridDropDownColumnEditor)
                    {
                        editorText = (editor as GridDropDownColumnEditor).SelectedText;
                        editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                    }
 
                    DataRow[] changedRows = this.GridSource.Select("ParamID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ParamID"].ToString());
                    changedRows[0][column.UniqueName] = editorValue;
                    this.GridSource.AcceptChanges();
                }
            }
        }
    }
 
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem insertedItem = (GridDataItem)e.Item;
        string ON = (insertedItem["ObjectName"].Controls[0] as RadComboBox).SelectedValue;
        string CN = (insertedItem["CounterName"].Controls[0] as RadComboBox).SelectedValue;
        Hashtable newValues = new Hashtable();
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, insertedItem);
 
    }
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            RadComboBox combo = (RadComboBox)item["ObjectName"].Controls[0];
            combo.AutoPostBack = true;
            combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
        }
    }
 
    void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
         
 
        RadComboBox rcbObjectValue = (RadComboBox)sender;
         
 
        GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
        RadComboBox rcbCounterName = editedItem["CounterName"].Controls[0] as RadComboBox;
 
        dynamic data = new[] {
            new { ID = 1, CounterName ="Name1"},
            new { ID = 2, CounterName = "Name2"},
            new { ID = 3, CounterName = "Name3"},
             new { ID = 4, CounterName = "Name4"},
            new { ID = 5, CounterName = "Name5"}
        };
 
        rcbCounterName.ClearSelection();
        rcbCounterName.DataSource = data;
        rcbCounterName.DataTextField = "CounterName";
        rcbCounterName.DataValueField = "CounterName";
        rcbCounterName.DataBind();
    }
 
 
 
}

I am not able to reproduce this issue please try with above code snippet.


Thanks,
Jayesh Goyani
0
Darryl
Top achievements
Rank 1
answered on 17 May 2013, 04:15 PM
I ran your test code and it didn't work properly. No errors, same behavior I am experiencing.

The AutoPostBack is resetting the selected value of the first combobox (Objects). Though the second combobox (Counters) is fine, as no postback occurs.

Final behavior should be: 
If I click an existing row and click "Edit":
    Select Objects dropdown and change value of "Memory" to "Memory 3".
    Select Counters and change Counter from "Name1" to "Name2".
    click Update
    Original Objects value "Memory" persisted, and did not update to Memory3 for that Row.

If I click "Add new record", I can select Any object I choose (ill use Memory 4 in this test)
    Counters generates list based on Object value "Memory 4"
    Select "Name5" from Counters
    Click Insert
    DataTable "Gridsource" and RadGrid should have a new row "Memory4 and Name5".

Currently, in your test, neither work correctly. It appears that your combo.AutoPostBack is causing the ObjectName "selected value" to default back to the original value it had in it, on load, before it was changed or defined.
0
Pavlina
Telerik team
answered on 20 May 2013, 01:24 PM
Hello Darryl,

May I ask you to isolate the problem in a sample project which we can test locally, so we can provide a proper solution for your case.

Thank you.

Greetings,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 May 2013, 01:32 PM
Hello,

I have done Changes in above code.
1. EnableViewState="true"
2. You have added ItemDataBound event twice.
3. I have added ItemCreated event
4. Move ItemDataBound event's code in to ItemCreated's Event.
5. Delete ItemDataBound event

Please try with below code snippet.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Forum.aspx.cs" Inherits="Forum" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowEdit="True" EnableViewState="true"
        AutoGenerateEditColumn="True" OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
        OnInsertCommand="RadGrid1_InsertCommand"
        OnItemCreated="RadGrid1_ItemCreated" AutoGenerateColumns="False" ShowStatusBar="True"
        AllowAutomaticUpdates="False" AllowAutomaticInserts="False">
        <ExportSettings>
            <Pdf AllowPrinting="False" />
        </ExportSettings>
        <MasterTableView EditMode="InPlace" CommandItemDisplay="Top" DataKeyNames="ParamID">
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
            </RowIndicatorColumn>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridDropDownColumn HeaderText="Objects" UniqueName="ObjectName" runat="server"
                    DataSourceID="SqlDataSource1" ListValueField="ObjectName" ListTextField="ObjectName"
                    DataField="ObjectName" />
                <telerik:GridDropDownColumn HeaderText="Counters" UniqueName="CounterName" runat="server"
                    ListValueField="CounterName" ListTextField="CounterName" DataField="CounterName" />
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
            <PagerStyle PageSizeControlType="RadComboBox" />
        </MasterTableView>
        <PagerStyle PageSizeControlType="RadComboBox" />
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JayeshTestConnectionString %>"
        SelectCommand="SELECT [Cust], [Product], [QTY], [ObjectName] FROM [Product]">
    </asp:SqlDataSource>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
 
public partial class Forum : System.Web.UI.Page
{
 
    private DataTable GridSource
    {
        get
        {
            Object obj = this.ViewState["_gds"];
            if (obj != null)
            {
                return (DataTable)obj;
            }
            else
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("ParamID");
                dt.Columns.Add("ObjectName");
                dt.Columns.Add("CounterName");
                ds.Tables.Add(dt);
 
                dt.NewRow();
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "0";
                dr[1] = "Memory0";
                dr[2] = "Pages/sec";
                dt.Rows.Add(dr);
 
                dt.NewRow();
                DataRow dr2;
                dr2 = dt.NewRow();
                dr2[0] = "1";
                dr2[1] = "Memory1";
                dr2[2] = "Available MBytes";
                dt.Rows.Add(dr2);
 
                //dt.NewRow();
                //DataRow dr3;
                //dr3 = dt.NewRow();
                //dr3[0] = "2";
                //dr3[1] = "LogicalDisk";
                //dr3[2] = "Free Megabytes";
                //dt.Rows.Add(dr3);
 
                this.ViewState["_gds"] = dt;
                return dt;
            }
        }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        //required due to RadPanel
         
         
 
        RadGrid1.ItemCreated += new GridItemEventHandler(RadGrid1_ItemCreated);
        //use w/o RadPanel
        //RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
 
    }
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            RadComboBox combo = (RadComboBox)item["ObjectName"].Controls[0];
            combo.AutoPostBack = true;
            combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
        }
    }
 
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        //required due to RadPanel
 
        RadGrid1.DataSource = this.GridSource;
 
        //use w/o RadPanel
        //RadGrid1.DataSource = this.GridSource;
    }
 
    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
 
        GridEditManager editMan = editedItem.EditManager;
 
        string ON = (editedItem["ObjectName"].Controls[0] as RadComboBox).SelectedValue;
        string CN = (editedItem["CounterName"].Controls[0] as RadComboBox).SelectedValue;
 
        foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
        {
            if (column is IGridEditableColumn)
            {
                IGridEditableColumn editableCol = (column as IGridEditableColumn);
                if (editableCol.IsEditable)
                {
                    IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
                    string editorText = "unknown";
                    object editorValue = null;
 
                    if (editor is GridTextColumnEditor)
                    {
                        editorText = (editor as GridTextColumnEditor).Text;
                        editorValue = (editor as GridTextColumnEditor).Text;
                    }
 
                    if (editor is GridDropDownColumnEditor)
                    {
                        editorText = (editor as GridDropDownColumnEditor).SelectedText;
                        editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                    }
 
                    DataRow[] changedRows = this.GridSource.Select("ParamID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ParamID"].ToString());
                    changedRows[0][column.UniqueName] = editorValue;
                    this.GridSource.AcceptChanges();
                }
            }
        }
    }
 
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem insertedItem = (GridDataItem)e.Item;
        string ON = (insertedItem["ObjectName"].Controls[0] as RadComboBox).SelectedValue;
        string CN = (insertedItem["CounterName"].Controls[0] as RadComboBox).SelectedValue;
        Hashtable newValues = new Hashtable();
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, insertedItem);
 
    }
 
    
 
    void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
         
 
        GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
        RadComboBox rcbCounterName = editedItem["CounterName"].Controls[0] as RadComboBox;
 
        dynamic data = new[] {
            new { ID = 1, CounterName ="Name1"},
            new { ID = 2, CounterName = "Name2"},
            new { ID = 3, CounterName = "Name3"},
             new { ID = 4, CounterName = "Name4"},
            new { ID = 5, CounterName = "Name5"}
        };
 
         
        rcbCounterName.DataSource = data;
        rcbCounterName.DataTextField = "CounterName";
        rcbCounterName.DataValueField = "CounterName";
        rcbCounterName.DataBind();
    }
 
 
 
}




Thanks,
Jayesh Goyani
0
Darryl
Top achievements
Rank 1
answered on 20 May 2013, 01:33 PM
Hello Pavlina, The sample Jayesh provided is exhibiting the exact behavior that I am running into. If you follow through the steps that I provided in his sample, you will see the behavior where the autopostback resets the "objects" combobox's value.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 May 2013, 01:35 PM
Hello,

Please try with above code snippet and let me know if any concern.

Thanks,
Jayesh Goyani
0
Darryl
Top achievements
Rank 1
answered on 20 May 2013, 02:48 PM
Hello Jayesh,

Those changes fixed my problem, in both your sample code, and in my final page.

The primary changes made that I believe resolved the issue: Enable view state on both the page and the Grid itself and Moving the ItemDataBound event's code in to ItemCreated's Event.

I greatly appreciate your assistance, as this was truly driving me crazy.

Thanks again.
Tags
Grid
Asked by
Darryl
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Darryl
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Pavlina
Telerik team
Share this question
or