user3524628
user3524628

Reputation: 137

How to transfer auto generated grid to grid with individual columns

Actually I am using the auto generated grid function like this:

<DataGrid ItemsSource="{Binding UserDataObject}" Width="786" />

Now I'd like to specify the individual columns like

<DataGrid>
    <Column Header="ID" />
    <Column Header="Username" />
    <Column Header="Role">--Show Role As Selector--</Column>
</DataGrid>

How does this work in WPF?

Explanations:

My UserDataObject is defined as

ObservableCollection<User> mUserDataObject = new ObservableCollection<User>();

public ObservableCollection<User> UserDataObject
    {
        get
        {
            return mUserDataObject;
        }
    }

    //User user = new User("ID", "Username", "Password", "Role");

    public class User : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // Attributes
        private int? _ID;
        private string _Username;
        private string _Password;
        private string _Role;

        // Constructors
        public User()
        {
        }

        public User(int pID, string pUsername, string pPassword, string pRole)
        {
            this.ID = pID;
            this.Username = pUsername;
            this.Password = pPassword;
            this.Role = pRole;
        }

        // Getter and Setter
        public int? ID
        {
            get { return _ID; }
            set { _ID = value; OnPropertyChanged("ID"); }
        }

        public string Username
        {
            get { return _Username; }
            set { _Username = value; OnPropertyChanged("Username"); }
        }

        public string Password
        {
            get { return _Password; }
            set { _Password = value; OnPropertyChanged("Password"); }
        }

        public string Role
        {
            get { return _Role; }
            set { _Role = value; OnPropertyChanged("Role"); }
        }

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

Additionally, how can I add an event listener that registers changes in one cell?

Upvotes: 0

Views: 200

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81283

You have to set AutoGenerateColumns to False on dataGrid and provide your own list of columns.

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding UserDataObject}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
    <DataGridTextColumn Header="Username" Binding="{Binding Username}"/>
    <DataGridComboBoxColumn Header="Role" ItemsSource="{Binding Role}"/>
  </DataGrid.Columns>
</DataGrid>

Use DataGridTextColumn if want to allow user to edit the property and DataGridComboBoxColumn if want user to select value from only permissible values for the property.


how can I add an event listener that registers changes in one cell?

Since you have proper bindings, you don't have to worry about handling it in code behind. Move your code to property setters to which cell is binded to.

However, in case you are interested you can hook CellEditEnding event.

Upvotes: 1

Related Questions