Multi Column ComboBox in C# WPF


For my working purpose I wanna try to make a multiple column Combox Box.So i prepare myself to make this.But i fetched too many problem for this and Finally i have gotten this solution.

At first write this code in XAML file to create MultiColumn ComboBox.

<ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="1,0,0,232" Name="cmbWHTypeId" Width="175" TabIndex="0" VerticalAlignment="Bottom">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding WhtID}" Width="60"/>
                <TextBlock Text="|" Width="10"/>
                <TextBlock Text="{Binding WhtName}" Width="100" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

 

Then Write the following Code in CS file of  UI

cmbWHTypeId.ItemsSource = objBWhTypes.GetAllWhTypes();

Here GetAllWhTypes() is a method which returns a list collection.

public List<EWareHouseTypes>GetAllWhTypes()
{
List<EWareHouseTypes> liWhTypes = new List<EWareHouseTypes>();

try
{
IEBSDataContext objDataContext = new IEBSDataContext();

var b = from WAREHOUSE_TYPES in objDataContext.SP_WarehouseType_GetAll() select WAREHOUSE_TYPES;
foreach (var item in b)
{
EWareHouseTypes objEWhTypes = new EWareHouseTypes();
objEWhTypes.WhtID = item.WT_ID;
objEWhTypes.WhtName = item.WT_NAME;
liWhTypes.Add(objEWhTypes);
}
}
catch (Exception e)
{

throw e;
}
return liWhTypes;
}

EWareHouseTypes is a class and it contains two property id and name.

GET VALUE FROM COMBO BOX

EWareHouseTypes aWHType = (EWareHouseTypes)cmbWHTypeId.SelectedItem;
MessageBox.Show(aWHType.WhtID);

Manually SET INDEX for a Particular ID

string typeID="WHT01"

for (int i = 0; i < cmbWHTypeId.Items.Count; i++)
{
EWareHouseTypes aWHType = (EWareHouseTypes)cmbWHTypeId.Items[i];
if (aWHType.WhtID == typeID)
{
cmbWHTypeId.SelectedIndex = i;
break;
}
}

If you have any query about this topic please leave a comment.

About these ads

8 Responses to “Multi Column ComboBox in C# WPF”

  1. Towhid Says:

    It is very helpful. It solved my problem.

  2. Nabab Says:

    Nice blog. I benefited to much from this blog. I appreciate u for your nice initiative. go ahead. Thanks Refat.

  3. atik sarker Says:

    really nice…….

  4. Md. Manjurul Hoque Jony Says:

    thanks

  5. Noel Farrugia Says:

    Thanks alot it really helped ;)

  6. ahmad Says:

    good idea
    thanks for share

  7. Igor Says:

    thanks!

  8. azar bhaladar Says:

    HI
    Is it possible to show the column header in this.
    I want to display header for each column present in combobox.

    can u please help me.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: