Modal/Popup for Add/Edit data

Answered
0
0

I was following the youtube  tutorial to create an application from scratch, the tutorial is not finished yet, it’s two parts, the second part ends after setting up a grid.

The next step is to implement a Popup/Modal editor for the data in the grid (create new row or edit existing row).

I’m wondering how that is done, any help?

 

Best regards,

Ali

  • You must to post comments
Best Answer
0
0

The tutorial in question uses Dapper to access a SQLite database.
Some alternative ways to design this project would be:
– Read the data from a JSON file (The JSON file is your “database”)
– Create the data in C# code, as C# objects
– Use SQL commands to access a SQLite or SQL database (instead of using Dapper, we just write the code to connect to the database ourselves).

Each of these examples would have different code for creating new rows and editing existing rows, based on how it’s accessing the database, and what type of database it is.
The code to create a modal/popup is the same though.

Here is a sample that uses the simplest method- it creates the data in C# code, as C# objects. So this sample has no code for database access. The code just opens the modal and edits the data in the datagridview.

Some relevant bits of code from the sample:

This creates a BindingLIst, binds it to the DataSource of the datagridview, and then fills it with data: Note that data can be added to the BindingList before and after it is bound to the datagridview- it will all show up in the datagridview.

//Page1.cs
BindingList PeopleList = new BindingList(); //create empty BindingList
private void Page1_Load(object sender, System.EventArgs e)
{
PeopleList.Add(new Person() { FirstName = "John", LastName = "Doe", Age = 40 });
dataGridView1.DataSource = PeopleList; //bind the datagridview to the BindingList
PeopleList.Add(new Person() { FirstName = "Jane", LastName = "Doe", Age = 30 });
PeopleList.Add(new Person() { FirstName = "Tom", LastName = "Smith", Age = 21 });
PeopleList.Add(new Person() { FirstName = "Emily", LastName = "Brown", Age = 57 });
PeopleList.Add(new Person() { FirstName = "Susan", LastName = "Green", Age = 46 });
}

This code opens a modal dialog on button click. It also gets data from the modal dialog and uses that data to add a new entry to the table.

//Page1.cs
private void button1_Click(object sender, System.EventArgs e)
{
//dialog will not exist outside of this using statement
//handy because then it doesn't clog up memory and you don't have to remember to dispose of it.
using (var dialog = new ModalPopup())
{
dialog.ShowDialog();
PeopleList.Add(new Person() { FirstName = dialog.FirstName, LastName = dialog.LastName, Age = dialog.Age });
}
}

Here I attached an event handler to the CellEndEdit event. All it does is display a message. If you were connecting to an external database, this would be a good time to write data to the database,
//Page1.cs
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
AlertBox.Show("You edited a value. If we were connected to a database, this would be a great time to save it.");
}

The code for the modal popup is ModalPopup.cs and it looks like this. I created several public fields (FirstName, LastName, Age) to store the data. In the button1_Click event, I show an AlertBox, and assign values to the public variables. I then close the popup.

//ModalPopup.cs
public string FirstName { get; private set; }
public string LastName { get; private set; }
public int Age { get; private set; }
public ModalPopup()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
AlertBox.Show("submitted");
FirstName = textBox1.Text;
LastName = textBox2.Text;
Age = int.Parse(typedTextBox1.Text);
this.Close();
}

Hope this helps!
-Julie

  • You must to post comments
1
0

In addition to Julie’s great answer I have put a video online on Youtube that demonstrates how to edit data in modal dialogs. It also shows how to use a BindingSource.

The video can be found here: https://www.youtube.com/@iceteagroup

  • Ali Badran
    Thanks!! Been waiting for this for a LONG time!
  • You must to post comments
0
0

Hi Ali,

Sorry for the delay. It’s on the TO DO list, unfortunately together with so many other things. My plan to have a new video online before Christmas.

Best

Joe

  • Ali Badran
    Hello, any update?
  • Julie (ITG)
    I posted a sample with a simple Datagridview and modal panel that lets you add new data. Hope this helps!
  • Ali Badran
    Thanks! 1 question, what is the benefit of using the BindingList instead of using a normal list?
  • Julie (ITG)
    It allows you to add entries to the list after the list is bound to the datagridview, and have them show up in the datagridview. Try changing the BindingList in the sample to a regular list- you’ll notice that only the first entry (John Doe) shows up in the datagridview. This is because it is the only thing that was added to the list before we bound it to the datagridview (dataGridView1.DataSource = PeopleList; is when we bound it). Because the rest of the entries were added after it was bound, they don’t appear in the datagridview. Also if you use a regular list, the modal panel which lets you add a new person to the list doesn’t work. Because adding items to the list doesn’t update the datagridview.
  • Ali Badran
    Thanks!!
Showing 3 results
Your Answer

Please first to submit.