0

I'm working with the WPF datagrid for the first time and want to get a list of names which are read out of a datatable in the first column an a button in the second. Everything works fine, the first column is filled with the names an I've added a second column (DataGridTemplateColumn) to the DataGrid. My problem I'm working on for hours is, how to add the buttons to all cells of the second column. I've tried many answers, I found here but nothing works. The DataView object has no Rows-Member, just Columns and Items and using indexes doesn't work. How can I set the Content of a cell to my button?

What I did so far:

conn.Open();
da_read.Fill(ds);
conn.Close();
dt = ds.Tables[0];

view.ItemsSource = dt.DefaultView;
for (int n = 1; n <= 6; n++) view.Columns[n].Visibility = Visibility.Hidden;
imgCol = new DataGridTemplateColumn();
view.Columns.Add(imgCol);

Button myButton = new Button();
myButton.Template = (ControlTemplate)this.FindResource("ButtonRed");

I open my DB connection, read the data, close it and get the first DataTable out of it. Then I set the DataTable as source for the DataGrid and leave only the first column visible. Then I create an new DataGridTemplateColumn and add it do the DataGrid as second column. Finally I tried to get a button in the first cell of the new column, so I created a button. But now I don't know how to get it into the cell. I found some examples in the forum but they didn't work.

view.Rows does not exist.

view.Columns exists but in the Columns[1] object I can't find a method to access the cell.

view.Columns[1][0] does not work.

view.Items[1][0] does not work.

  • Please post your code, and explain what you have tried – Sinnich Jan 05 '18 at 10:29
  • I've added some details to the question. – CptGreenwood Jan 05 '18 at 12:17
  • try to use the approach shown in [this post](https://stackoverflow.com/questions/40358111/datagrid-shows-path-of-image-instead-of-image-itself) – ASh Jan 05 '18 at 13:52
  • I don't think this topic matches my problem. In that post the DataTable is manipulated, not the DataGrid and I don't want to put an Image into the cells but a Button control (class Button). – CptGreenwood Jan 06 '18 at 11:29

0 Answers0