Click here to Skip to main content
Licence CPOL
First Posted 9 Apr 2007
Views 16,792
Downloads 615
Bookmarked 18 times

Conditional combobox column for DataGrid

By | 9 Apr 2007 | Article
This is a class which will allow you to show comboboxes on a DataGrid based on some condition(s).

Screenshot - ConditionalCombobox.jpg

Introduction

There are times when you need to provide a user interface which is capable of taking input both in freeform (name for example) and in restricted form (combobox, e.g., Sex). This control solves this in an easy way.

Background

I ran into one such requirement. I searched over the internet and all the articles or controls where talking about making the whole column a combobox. So I decided to tweak one such code to take care of my need.

Using the Code

First of all, you need to add the GridColumnStyle of the DatagridConditionalComboboxColumn type in the table style of the DataGrid. The constructor of DatagridConditionalComboboxColumn takes a ComboValueChanged delegate as an input. This delegate is called whenever the user does SelectedIndex changes for the combobox.

Then you need to listen to the StatedEditing event which is raised by the DatagridConditionalComboboxColumn whenever a user enters the cell for editing. Here you can check for the conditions, and based on it, you can set the ShowCombobox property to either true or false. If you set it to true, you can the add items to the combobox.

Once you do all this... you are done. Look at the attached source code of the example.

//
// A sample of adding DatagridConditionalComboboxColumn to the datagrid 
// table style
DataGridTableStyle tableStyle = new DataGridTableStyle();

DataGridTextBoxColumn nameColumn = new DataGridTextBoxColumn();
nameColumn.MappingName = "Name";
nameColumn.HeaderText = "Name";
nameColumn.ReadOnly = true;

tableStyle.GridColumnStyles.Add(nameColumn);

ComboValueChanged comboValueChangedDelegate = 
          new ComboValueChanged(comboValueChanged);

DatagridConditionalComboboxColumn valueColumn = 
      new DatagridConditionalComboboxColumn(comboValueChangedDelegate);
valueColumn.StartedEditing +=new EventHandler(valueColumn_StartedEditing);
valueColumn.MappingName = "Value";
valueColumn.HeaderText = "Value";
tableStyle.GridColumnStyles.Add(valueColumn);

this.dataGrid1.DataSource = populateDatatable();

this.dataGrid1.TableStyles.Clear();
this.dataGrid1.TableStyles.Add(tableStyle);

Points of Interest

None..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

pravk

Web Developer

India India

Member

I am a Software developer specializing in finance domain. Currently located in Mumbai, India and working with Morgan Stanley.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 9 Apr 2007
Article Copyright 2007 by pravk
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid