I use the entitiy framwork code first method, How can I display the last riff number in the database by clicking the Add text box?, my textbox code is key (int).

Majeed Purseyedmahdi 41 Reputation points
2021-07-07T07:17:02.987+00:00

112464-question-pic.jpg112434-question-pic-2.jpg112428-calss-bank.jpg112420-frm-bank.jpg112473-frm-bank-2.jpg112474-frm-bank-3.jpg112429-frm-bank-4.jpg112430-frm-bank-5.jpg112481-frm-bank-6.jpg

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
748 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,994 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,431 Reputation points
    2021-07-08T11:22:46.993+00:00

    Here is an example that has a table as follows

    113013-countrytable.png

    Form interface
    112940-figure1.png

    Using EF Core 5 and a custom BindingList which allows the DataGridView to be sortable (not needed for your question but nice and if you want it I can post the source)

    112969-window1.png

    using System;  
    using System.Data;  
    using System.Linq;  
    using System.Windows.Forms;  
    using WindowsFormsApp1.Classes;  
    using WindowsFormsApp1.Data;  
    using WindowsFormsApp1.Models;  
      
    namespace WindowsFormsApp1  
    {  
        /// <summary>  
        /// C# 9  
        /// </summary>  
        public partial class Form1 : Form  
        {  
            private readonly BindingSource _bindingSource = new ();  
              
            /// <summary>  
            /// Custom BindingList to allow the DataGridView to be sortable  
            /// </summary>  
            private SortableBindingList<Country> _countryBindingList;  
            public Form1()  
            {  
                InitializeComponent();  
                  
                Shown += OnShown;  
            }  
      
            private void OnShown(object? sender, EventArgs e)  
            {  
                using var context = new Context();  
                  
                _countryBindingList = new SortableBindingList<Country>(context.Country.ToList());  
                _bindingSource.DataSource = _countryBindingList;  
                  
                dataGridView1.DataSource = _bindingSource;  
            }  
      
            private void LastIdButton_Click(object sender, EventArgs e)  
            {  
                using var context = new Context();  
                  
                /*  
                 * If no record, add one with a default value for a country name,  
                 * in reality this would need input from the user e.g. a ComboBox  
                 * selection  
                 */  
                if (!context.Country.Any())  
                {  
                    Country newCountry = new Country() {Name = "TODO"};  
                    context.Country.Add(newCountry);  
                    context.SaveChanges();  
                    LastIdTextBox.Text = newCountry.Id.ToString();  
                    NextIdTextBox.Text = (newCountry.Id + 1).ToString();  
                }  
                /*  
                 * We have at least one record, get the last record  
                 * From the last record get the key, id and display  
                 * the current last id and another text box the last id  
                 * plus one.  
                 */  
                else  
                {  
                    Country country = context.Country.OrderBy(countryItem => countryItem.Id).LastOrDefault();  
                    LastIdTextBox.Text = country.Id.ToString();  
                    NextIdTextBox.Text = (country.Id + 1).ToString();  
                }  
            }  
        }  
    }  
      
    

    In regards to the custom BindingList, to get the current record in the DataGridView

    var currentCountry = _countryBindingList[_bindingSource.Position];  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.