বুধবার, ৩০ নভেম্বর, ২০১১

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.

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন