2

I am using inplace editing on a RadGrid that is built using a class file. Everything is working well except I am having an issue the SelectedIndexChanged event not firing when the grid is in edit mode. Any thoughts?

private void RadGrid_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        try
        {
            if ((e.Item as GridDataItem) == null) { return; }
            ((RadNumericTextBox) (e.Item as GridDataItem)["Percentage"].Controls[0]).Width = Unit.Pixel(75);
            ((TextBox) (e.Item as GridDataItem)["Code"].Controls[0]).Width = Unit.Pixel(75);

            RadComboBox _participantList = (e.Item as GridEditableItem)["ID"].Controls[0] as RadComboBox;
            if (null == _participantList) { return; }

            _participantList.Width = Unit.Pixel(120);
            _participantList.DataValueField = "ID";
            _participantList.DataTextField = "ID";
            _participantList.AutoPostBack = true;
            _participantList.DataSource = MAASBaseInterface.ParticipantAPI.GetParticipants();
            _participantList.DataBind();
            _participantList.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(_participantList_SelectedIndexChanged);

            if (!(e.Item.DataItem is GridInsertionObject))
                _participantList.SelectedValue = ((Participant) (e.Item.DataItem)).ID.ToString();
            if (e.Item.DataItem is GridInsertionObject)
                _participantList.EmptyMessage = "-- Select --";
        }
        catch (Exception ex)
        {

            string _ex = ex.Message;
        }
    }
} 

void _participantList_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    //first reference the edited grid item through the NamingContainer attribute
    GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
    int _selectedValue = Convert.ToInt32((editedItem["ID"].Controls[0] as RadComboBox).SelectedValue);
    ParticipantList _participants = MAASBaseInterface.ParticipantAPI.GetParticipants();
    Participant _participant = _participants.Where(a => a.ID == _selectedValue) as Participant;
    RadTextBox _code = editedItem["Code"].Controls[0] as RadTextBox;
    _code.ReadOnly = false;
    _code.Text = _participant.Code;
}
Tim
  • 1,249
  • 5
  • 28
  • 54

2 Answers2

1

You need a button that has the CommandName="Select" set. Without that the event doesn't trigger. Could that be the problem?

This link gives more detail

EDIT:

The problem might be that the dropdown list is dynamically added to the grid so that the event needs to be added each time the row is bound. In my experience the radGrid and the GridView works in the same way with respect to the event model so this SO answer might sort you out. Good luck - my initial thoughs were that this couldn't be don't but there may be a way forward

Community
  • 1
  • 1
Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
  • 1
    Thanks Tim, but I am not looking for the selectedindexchanged event of the radgrid. I want the selectedindexchanged of the radcombobox. I know I don't need a select button to have that event fire, I have seen the sample that telerik is using for that demo but they have defined the radcomboboxes in the .aspx page and not dynamically as I have done. I am overlooking something but what I don't know. – Tim Dec 22 '11 at 03:44
  • @Tim I dont think you are overlooing anything. I my experience the radGrid and GridViews work in similar ways with respect to the event model. This link http://forums.asp.net/t/1313185.aspx/1 details someones struggles. If i were you I would try to redesign the page to take out the selectedindexchanged and get things working through a command button and the onitemcommand event. It's the only way I have successfully passed commands out of these type of grids. Sorry if this isn't what you were looking for – Crab Bucket Dec 22 '11 at 09:57
  • @Tim Actually i've foud a possible solution which I've included as an edit on the question – Crab Bucket Dec 22 '11 at 10:04
  • Hey Tim. Thanks for all your valuable input. It did provoke some thought but alas non of it solved the problem. I did find the issue and you may not believe what it is. See my answer above. – Tim Dec 22 '11 at 15:47
1

The problem was that I was only setting the Value property of the RadComboBox and not the Text property. Even though text value was showing in the RadComboBox in edit mode apparently it was displaying the Value property. As soon as it was set it started posting back just like it was supposed to do.

                if (!(e.Item.DataItem is GridInsertionObject))
                {
                    _participantList.SelectedValue =
                        ((ReinsuranceAgreementParticipant) (e.Item.DataItem)).LegacyReinsurerID.ToString();
                    // I added this line
                    _participantList.Text = ((ReinsuranceAgreementParticipant)(e.Item.DataItem)).LegacyReinsurerID.ToString();
                }
                if (e.Item.DataItem is GridInsertionObject)
                    _participantList.EmptyMessage = "Select Reinsurer";
Tim
  • 1,249
  • 5
  • 28
  • 54