SelectedIndexChanged throws Error

Hello,
I'm using MultiColumnComboBox control.
When I change the SelectedIndex, I need to fill a TextBox with description of that Index.

So, first, I'm filling my Control like this:

private void LoadTI()
        {
            var query = (from ta in ctx.tblTipoIs.Where(s => s.Ativo == true)
                         select new
                         {
                             ta.TipoIncID,
                             ta.Codigo,
                             ta.Descricao
                         }).OrderBy(s => s.Codigo);
            CmbTipoI.DataSource = query.ToList();
            CmbTipoI.DisplayMember = "Codigo";
            CmbTipoI.ValueMember = "TipoIncID";
            CmbTipoI.SelectedIndex = -1;
            CmbTipoI.Text = "";
        }

This is called on Form Load.

Than, I'm using this code that I got from your Helping Documentation:

private void CmbTipoI_SelectedIndexChanged(object sender, EventArgs e)
        {
                ComboBoxBaseDataBound c = CmbTipoI as ComboBoxBaseDataBound;
                if (c.SelectedIndex > 0) // Changed since c.SelectedIndex is starting 0 on Form Load
                {
                DataRowView drv = c.Items[c.SelectedIndex] as DataRowView; // Here's the error: "Object not set to an instance of an Object --> drv is null
                LblTI.Text = query.Descricao;
                }

When I throw a breakpoint in this event, the c.SelectedIndex is > 0, but drv is null!

So, I can't change Index and fill LblTI with Descrição of Selected Index.

Any help?

EDIT: The only way I get my LblTI.Text filled is querying like this...

private void CmbTipoI_SelectedIndexChanged(object sender, EventArgs e)
        {
                ComboBoxBaseDataBound c = CmbTipoI as ComboBoxBaseDataBound;
                if (c.SelectedIndex > 0)
                {
                var query = (from lc in ctx.tblTipoIs.Where(s => s.TipoIncID == c.SelectedIndex)
                             select lc).Single();
                LblTI.Text = query.Descricao;
                }
            else
            {
                LblTI.Text = "";
            }
        }

3 Replies

SK Senthil Kumaran Rajan Syncfusion Team August 6, 2018 04:00 PM UTC

Hi Carlos, 
 
Thank you for contacting Syncfusion support. 
 
From the provided code snippet we could find that you have bind the DataSource from list. In the documentation link we have provided the solution to retrieve the item from DataTable. But if you are using the list, then you have to get the item from the list not from DataRowView. Please make use of the below code example. 
 
Code Example[C#] 
 
ComboBoxBaseDataBound c = multiColumnComboBox1 as ComboBoxBaseDataBound; 
if (c.SelectedIndex != -1 && (c.Items[0] is Students)) 
{ 
     string displayText = (c.Items[c.SelectedIndex] as Students).LastName; 
     if (displayText != null) 
     { 
        c.Text = displayText; 
     } 
} 
 
 
We have prepared the sample for your reference and this can be downloaded from the below location. 
 
 
Regards, 
Senthil 



CF Carlos Ferreira August 7, 2018 12:23 PM UTC

Thanks for your answer.

I've tried to use it in a datagrid filter using comboboxes and one of it is a multiplecombobox. However, I'm getting a NULL Exception and the selected value is simply ignored.

Her's what I'm doing:

My Class:

public class Stuff
{
     public int ID{get; set;}
     public string Conc {get; set;}
     public string StuffName{get;set;}
}

My ToList query:

public IEnumerable<Stuff> LoadData()
        {
            var ctx = new DbContext();
            List<Stuff> list = new List<Stuff>();
            var query = (from p in ctx.tblStuffs.AsQueryable() 
                              join t in ctx.tblConcs on p.ConcID equals t.ConcID
                              select new 
                                   {
                                        ID = p.StuffID,
                                        StuffName = p.StuffName,
                                        Conc = t.ConcName
                                   }).FirstOrDefault();
          ComboBoxBaseDataBound conc = CmbConc as ComboBoxBaseDataBound;
            if (conc.SelectedIndex != -1 && (conc.Items[0] is tblConc))
            {
                string displayText = (conc.Items[conc.SelectedIndex] as tblConc).ConcName; // In here, "as tblConc" is getting Null
                query = query.Where(s => s.Conc== displayText);
            }
     return query.ToList();
}

The CmbConc is same structure as first example in my Post.

But, When I filter my CmbConc, I get all values in Database and not filter by CmbConc SelectedValue.

Any help on this?

Thanks


SK Senthil Kumaran Rajan Syncfusion Team August 8, 2018 04:00 PM UTC

Hi Carlos, 
 
Thank you for your update. 
 
From the provided code example we could find that you have bind the list as DataSource for MultiColumnComboBox, In code example could you please let us know the “tblConc” type and conc.Items[conc.SelectedIndex] type?  
We hope as the “tblConc” type and “conc.Items[conc.SelectedIndex]” type gets mismatched, so that the value is return as null. If possible, could you please share us the simple sample that reproduce the reported behavior? It will be helpful for us to analyze and provide the solution at the earliest. 
 
Regards, 
Senthil 


Loader.
Up arrow icon