How to display the selected row information in WinForms MultiColumnComboBox?
Display the selected the row
MultiColumnComboBox control doesn’t have a direct option to retrieve selected row information, and we can only retrieve the value of the DisplayMember column. Therefore, we need to follow the steps below to achieve this requirement.
Step 1: Populate the MultiColumnComboBox values from the DataTable. The following code demonstrates the same.
C#
// Datasource DataTable
DataTable dt = new DataTable("DataSource");
dt.Columns.Add("S.No");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Occupation");
dt.Columns.Add("Place");
// Create a Data Set
DataSet ds = new DataSet();
ds.Tables.Add(dt);
dt.Rows.Add(new string[] { "1", "Alex", "Zen", "Doctor", "Italy" });
dt.Rows.Add(new string[] { "2", "Bob", "Harry", "Staff", "London" });
dt.Rows.Add(new string[] { "3", "Alice", "Peter", "Developer", "US" });
dt.Rows.Add(new string[] { "4", "Adlensha", "Fdo", "Staff", "Italy" });
dt.Rows.Add(new string[] { "5", "Brick", "John", "Nurse", "North America" });
dt.Rows.Add(new string[] { "6", "Collen", "Geo", "Developer", "India" });
this.multiColumnComboBox1.DataSource = dt;
this.multiColumnComboBox1.DisplayMember = "Place";
Step 2: Create a class to represent the selected row information of the MultiColumnComboBox control. The following code demonstrates the same.
C#
/// <summary>
/// This represents the Selection Row information in MultiColumnComboBox
/// </summary>
public class SelectedRowInformation
{
public SelectedRowInformation(string serialNumber, string firstName, string lastName, string occupation, string place)
{
SerialNumber = serialNumber;
FirstName = firstName;
LastName = lastName;
Occupation = occupation;
Place = place;
}
/// <summary>
/// Holds serial number
/// </summary>
private string m_serialNumber;
/// <summary>
/// Gets/Sets the Serial Number
/// </summary>
public string SerialNumber
{
get { return m_serialNumber; }
set { m_serialNumber = value; }
}
/// <summary>
/// Holds the First Name
/// </summary>
private string m_FirstName;
/// <summary>
/// Gets/Sets the First Name
/// </summary>
public string FirstName
{
get { return m_FirstName; }
set { m_FirstName = value; }
}
/// <summary>
/// Holds the Last Name
/// </summary>
private string m_LastName;
/// <summary>
/// Holds the Last Name
/// </summary>
public string LastName
{
get { return m_LastName; }
set { m_LastName = value; }
}
/// <summary>
/// Holds the Occupation
/// </summary>
private string m_Occupation;
/// <summary>
/// Gets/Sets the Occupation
/// </summary>
public string Occupation
{
get { return m_Occupation; }
set { m_Occupation = value; }
}
/// <summary>
/// Holds the Place
/// </summary>
private string m_Place;
/// <summary>
/// Gets/Sets the Place
/// </summary>
public string Place
{
get { return m_Place; }
set { m_Place = value; }
}
}
Step 3: Create a generic collection property with the type of the above Class to hold the selected row information.
C#
//Collection to hold Selected Row information List<SelectedRowInformation> SelectedRowsCollection = new List<SelectedRowInformation>();
Step 4: The SelectedIndexChanged event will raise, when user selects the dropdown items and in here, we can add selected row information into SelectedRowsCollection. The following code demonstrates the same.
C#
this.multiColumnComboBox1.SelectedIndexChanged+=new EventHandler(multiColumnComboBox1_SelectedIndexChanged);
// Here we are adding selected row information for all rows in MultiColumnComboBox
if (this.multiColumnComboBox1.SelectedItem != null && this.multiColumnComboBox1.SelectedItem is DataRowView)
{
DataRowView selectedRow = this.multiColumnComboBox1.SelectedItem as DataRowView;
this.SelectedRowsCollection.Add(new SelectedRowInformation(selectedRow.Row["S.No"].ToString(), selectedRow.Row["FirstName"].ToString(),selectedRow.Row["LastName"].ToString(), selectedRow.Row["Occupation"].ToString(), selectedRow.Row["Place"].ToString()));
}
Step 5: Now you can retrieve the saved selected row information from SelectedRowsCollection. The following code demonstrates the same.
C#
if (this.SelectedRowsCollection.Count > 0)
{
MessageBox.Show(this, "S.no : " + this.SelectedRowsCollection[this.SelectedRowsCollection.Count - 1].SerialNumber + Environment.NewLine
+ "FirstName : " + this.SelectedRowsCollection[this.SelectedRowsCollection.Count - 1].FirstName + Environment.NewLine
+ "LastName : " + this.SelectedRowsCollection[this.SelectedRowsCollection.Count - 1].LastName + Environment.NewLine
+ "Occupation : " + this.SelectedRowsCollection[this.SelectedRowsCollection.Count - 1].Occupation + Environment.NewLine
+ "Place : " + this.SelectedRowsCollection[this.SelectedRowsCollection.Count - 1].Place + Environment.NewLine, "SelectedRowInformation");
}
else
{
MessageBox.Show("Select any rows in MultiColumnComboBox", "No rows selected");
}
Screenshot

Figure 1: Select a row in MultiColumnComboBox.

Figure 2: If click the Button, then it will show the selected row information.