0

I'm creating CRUD app, and basically what I want to do, is to create datagrid where each row have its own button to decrease the value of one cell (which is taking data from SQL by Entity Framework). Like code behind work with data base is working or i will fix this later, but my problem is exactly the way of showing the button in data grid.

Its shown like this: System.Windows.Controls.Button. I don't know how to change this.

I was looking for this problem for about 3 hours and I was unable to find a satisfactory answer that would help me.

Part of class responsible for data-handling

    public Button SubstractByOne = new Button();

    public LearnDayAdvenced(LearningDays learnDay)
    {
        SubstractByOne.Name = "substract";            
        SubstractByOne.Click += new RoutedEventHandler(Button_click);
    }    

    protected void Button_click(object sender, EventArgs e)
    {
        SubstractTime();
    }

And the code responsible for displaying and creating data. (I know that I should refactor this but in case of problem its like this)

ICollection<LearnDayAdvenced> learnDayAdvenceds = new List<LearnDayAdvenced>();

foreach (var learningDay in learningDaysAsSource)
{
    LearnDayAdvenced advencedLearnDay = new LearnDayAdvenced(learningDay);
    learnDayAdvenceds.Add(advencedLearnDay);
}

DataTable learningDataTable = new DataTable("Learn Days");
learningDataTable.Columns.Add(new DataColumn("Subject name", typeof(string)));
learningDataTable.Columns.Add(new DataColumn("time left", typeof(double)));
learningDataTable.Columns.Add(new DataColumn("Substract by 1", typeof(Button)));

foreach (var learnDay in learnDayAdvenceds)
{
    learningDataTable.Rows.Add(learnDay.LearnDay.subjectName, learnDay.LearnDay.learningTimeAmount, learnDay.SubstractByOne);
}

this.DataGrid.ItemsSource = learningDataTable.AsDataView();
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
cebilon123
  • 551
  • 1
  • 4
  • 7
  • Your question isn't clear. Do you want a button per row or button per cell? If it's the former, does that mean clicking this button will decrease the value of all cells in that row? – Sach May 22 '19 at 20:40
  • I want to have one button in each row of "Substract by 1" column. Basically what i m getting is system.windows.controls.button in each row. Clicking the button is decreasing value of one cell and inside of table in data base. – cebilon123 May 22 '19 at 21:07
  • Could you post your `Datagrid` definition? – FF- May 22 '19 at 22:27
  • I xaml its only like **""** with name and width parameter. Basically column definition is from **DataTable** in code above. (Maybe i should define the look of DataGrid somewhere? ) – cebilon123 May 23 '19 at 07:11

1 Answers1

0

See here : Add button column to datatable.

Rather use a template to assign the button to each column. You can exclude it from your datatable.

Adding a Button to a WPF DataGrid

RandomCoder
  • 134
  • 9