.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

WPF ComboBox in DataGrid

Posted By:Diptimaya Patra       Posted Date: April 17, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

WPF ComboBox in DataGrid . This article will describe the DataGrid in Edit mode while the edit cell requires a list of data.
 

Introduction

This article will describe the DataGrid in Edit mode while the edit cell requires a list of data. We are going to achieve that using the ComboBox.

Creating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as DatePickerDGWPF.

ComboBoxWPF1.gif

Here is the basic idea of our sample application; we would have sample data with one property that can be selected from a list of values displayed in ComboBox.

When the particular cell will be edit mode, we would be able to select from the list displayed in the ComboBox.

So let's have a class and have sample data.
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            
            List ticketsList = new List 
            {
                new TicketInfo{ Subject="IE Not Working", Status="Open", RaisedBy="User 1"},
                new TicketInfo{ Subject="TFS Not Working", Status="Assigned", RaisedBy="User 3"},
                new TicketInfo{ Subject="VS 2008 Not Working", Status="Open", RaisedBy="User 9"},
                new TicketInfo{ Subject="SQL SERVER Not Working", Status="Open", RaisedBy="User 2"},
                new TicketInfo{ Subject="Portal Not Working", Status="Closed", RaisedBy="User 12"},
                new TicketInfo{ Subject="Not able to log in to portal", Status="Open", RaisedBy="User 4"},
                new TicketInfo{ Subject="WIFI not Working", Status="Open", RaisedBy="User 7"}
            };
            dgData.ItemsSource = ticketsList;
        }
    }

    public class StatusList : List
    {
        public StatusList()
        {
            this.Add("Assigned");
            this.Add("Closed");
            this.Add("In Progress");
            this.Add("Open");
            this.Add("Resolved");
        }
    }


Now we will add a DataGrid to the Window and design the Columns inside of it.

ComboBoxWPF3.gif

Code view of the above image

        
            
                
                
                    
                        
                            
                        
                    
                    
                    
                        
                            
                        
                    
                    
                
                
            
        


We need to set the ItemSource Property; for that I have created a Class that would generate the list and in XAML I have accessed it and assigned it. The following figures will clarify what I have done:

public class StatusList : List
{
     public StatusList()
     {
            this.Add("Assigned");
            this.Add("Closed");
            this.Add("In Progress");
            this.Add("Open");
            this.Add("Resolved");
     }
}

...

    

...

ComboBoxWPF6.gif

Code view of above image

     
            
     

It seems we are done with the design of our DataGrid columns.

Run the application and try to edit the column "Status", you would get Dropdownlist control to handle the Status List.

ComboBoxWPF7.gif

And when in Edit mode:

ComboBoxWPF8.gif

After Updating:

ComboBoxWPF9.gif

Hope this article helps.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend