سؤال

Working in C# WPF. Ok so I have a datagrid which is bound to an entity framework in my project. The datagrid functions awesome when updating values which are displayed. I push the changes to the database using MyDataEntity.SaveChanges() and everything works perfectly.

The issue comes up when adding a new row. I've set CanUserAddRows="True" in my XAML, so the user is able to add new rows. The problem comes when i have to save the new rows to the database. For some reason, the SaveChanges() call doesn't update the database. What I tried to do was pull all the values of the current row into an IList and then access the individual values so I could call MyDataEntities.Add()' on theRowEditEnding` event, but that made some other problems. Mainly, I cant access the individual values in the IList. when I look at the data in the list, it is in there, i have seen it, i just cant assign it to anything.

So really, my problem is two-fold.

1: How can I save the newly entered row to my database

2: How can I access the values in the IList

2 would be a better solution, because this way i could use the following code to fill additional columns in the database as well as pass data to other methods to get values for specific columns in the database.

private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    IList rows = DataGrid.SelectedItems;
    TableName tbl = new TableName
    {
        Name = rows[1].ToString(),
        Qty = decimal.Parse(rows[4].ToString()),
        Type = rows[6].ToString(),
        TQty = ConvertQtoT(rows[4].ToString(), rows[6].ToString(),
        OnLIst = true
    };

        MyDataEntity.TableNames.Add(tbl);
        MyDataEntity.TableNames.SaveChanges(); 
    }
}
هل كانت مفيدة؟

المحلول

You can save a newly entered row into database from datagrid as follows:

First assign datasource to datagrid:

datagrid.ItemsSource = myview.GetList();

Next select the SelectionChanged Event of Datagrid:

private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  objToAdd = datagrid.SelectedItem as clsName; //Entity Object
}

Next select CellEditEnding Event of Datagrid:

private void datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        try
        {
            FrameworkElement element1 = datagrid.Columns[0].GetCellContent(e.Row);
            if (element1.GetType() == typeof(TextBox))
            {
                var colomn1 = ((TextBox)element1).Text;
                objToAdd.Column1 = Convert.ToInt32(Column1);
            }
            FrameworkElement element2 = datagrid.Columns[1].GetCellContent(e.Row);
            if (element2.GetType() == typeof(TextBox))
            {
                var colomn2 = ((TextBox)element2).Text;
                objToAdd.Column2 = Convert.ToInt32(Column2);
            }
            FrameworkElement element3 = datagrid.Columns[2].GetCellContent(e.Row);
            if (element3.GetType() == typeof(TextBox))
            {
                var colomn3 = ((TextBox)element3).Text;
                objToAdd.Column3 = Convert.ToInt32(Column3);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message); 
        }
    }

Next select RowEditEnding Event of Datagrid:

private void datagrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        try
        {
            var Res = MessageBox.Show("Do you want to Create this new entry", "Confirm", MessageBoxButton.YesNo);
            if (Res == MessageBoxResult.Yes)
            {
                EntityObject.InsertEmployee(objToAdd);
                EntityObject.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message); 
        }
    }

Hope this will help you...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top