1

I am trying to create a WPF application. In my application I want to add a ComboboxCell in 3 row of my dataGrid.

I can do this in C# window application by using the following code:

public partial class Form1 : Form
{
    public Form1()
    {
           InitializeComponent();
           for (int i = 1; i < 13; i++)
           {
                 dataGridView1.Columns.Add("Slot" + i, "Slot " + i);
           }
           for (int i = 0; i < 18; i++) 
           {
                dataGridView1.Rows.Add();
           }GridBaseCells();
    }
    DataGridViewComboBoxCell ModeCell  = new DataGridViewComboBoxCell();
    string[] Modes= { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
    void GridBaseCells()
    {
         ModeCell.Items.AddRange(ModeAr);
         for (int i = 2; i < 14; i++)
         {
              dataGridView1[i, 3] = (DataGridViewComboBoxCell)ModeCell.Clone();
              dataGridView1[i, 3].Value = "C";
         }
    }
}

I am trying to implement the same using WPF. But i am not able to do this(I cant find DataGridViewComboBoxCell).

How can I do this?

(Please help with an example. I am new to WPF, don't have much Idea in binding and all).

Sheridan
  • 68,826
  • 24
  • 143
  • 183
Jake
  • 1,560
  • 2
  • 17
  • 25
  • You don't add cells, you add rows, but if you're doing it properly in the WPF way, then you don't add rows either... you add a new data item and let WPF render the new row for you. Please see the [Data Binding Overview](http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) page from MSDN for help with data `Binding`. – Sheridan Jan 28 '14 at 13:53
  • Are you *sure* that your question is even WPF related? Why does your class extend the `Form` class??? – Sheridan Jan 28 '14 at 13:54
  • The code above is 'C# Windows Form Application'. I am trying to implement the same in C# WPF application. – Jake Jan 28 '14 at 13:58
  • Wow! An essential piece of information like that and you didn't think that it was necessary to put that in your question? – Sheridan Jan 28 '14 at 14:34

1 Answers1

1

Ok, so I hope that you're willing to do some work yourself... you're going to have lots to do. WPF is very different from WinForms, so you can't use your WinForms code for a start. You're going to have to get your head around a very different way of working. In WPF, we work with data elements, not UI elements.

What I mean by that is that we build classes that contain all of the properties required in the UI and implement the INotifyPropertyChanged interface. Then, when we have declared a collection of these classes in the UI, we simply data bind it to the ItemsSource property of a collection control:

<DataGrid ItemsSource="{Binding YourItems}" />

The DataGrid will automatically generate its columns for you. So to answer your question, all we need to do to add a new row to the DataGrid is to add a new item into the collection in code:

YourItems.Add(new YourItemClass());

The INotifyPropertyChanged interface will then take care of updating the UI for you. So,that's your answer in basic form.

Rather than trying to teach all of WPF to you here, I'd prefer to point you to some very useful online resources for you to get a better idea on what is possible and how to do it:

WPF DataGrid Control from WPF Tutorial.net
WPF DataGrid Practical Examples from CodeProject

Sheridan
  • 68,826
  • 24
  • 143
  • 183