I can not see the new item in Datagridview without rebooting

I added an entry to the base db, and after adding the entry I do datagridview.Refresh (); and I don’t see the newly added record.

If I stop and run the application there. What am I doing or not doing? Notes: button1 and datagridview are in different forms. I made the datagridview modifiers public. This project is ado.net project

public class CustomerService { public List<Customers> ShowAll() { List<Customers> customers = new List<Customers>(); SqlConnection conn = new SqlConnection("data source=.; database=custer; user id=sa; password=*****"); SqlCommand cmd = new SqlCommand(" select * from Customers ", conn.Open()); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Customer customer = new Customer () { CustomerID = dr.GetInt32(0), CustomerName = dr.GetString(1), CustomerSurname = dr.GetString(2), }; customers.Add(customer); } conn.Close(); return customers; } } private void button1_Click(object sender, EventArgs e) { CustomerService service = new CustomerService(); if (txtCustomerName.Text != "" || txtCustomerSurname.Text != "") { customerservice.customerAdd(txtCustomerName.Text, txtCustomerSurname.Text);//this rows is other method .I am using for adding new customer MessageBox.Show("Customer Added"); Form1.dataGridView1.DataSource = service.ShowAll(); Form1.dataGridView1.Refresh(); } else { //…………… } } 
+4
source share
12 answers

After adding data to the database, your application does not know anything about the added data. You need to load this data into memory. Get all the data that you want to display from the database, and bind it explicitly to the user interface after performing database operations.

EDIT

Can you check what ShowAll () returns during debugging? Does it return the data you need?

If this is a WPF application, look here . The problem here is to tell your user interface component that the underlying data source has been changed, so the user interface component reflects the changes made to the data.

Again, if this is a WPF application, you can define your data as an ObservableCollection and associate the user interface with this collection. ObservableCollection automatically requests the user interface to update when data changes.

Rica ederim.)

+3
source

The usual way to do this is to reset the DataSource DataGridView .

Try this code (with the correct code to ensure the correct data source):

 Form1.dataGridView1.DataSource = typeof(List); Form1.dataGridView1.DataSource = service.ShowAll(); 

Calling .Refresh() does not work, because it only forces you to redraw, but the code that draws the grid is not aware of the changes.

Also provide a link to WPF that can help you:

Why is the DataGrid not updated when the ItemsSource changes?

+2
source

Try calling EndEdit in a datagridview:

 this.dataGridView1.EndEdit(); 

Second, update the grid view:

 this.dataGridView1.Refresh(); 

And if that still doesn't work, try calling Refresh on the containing control

 ParentControl.Refresh() 

As a result, this will cause a redraw, which may be needed.

+1
source

What is the contents of customerervice.customerAdd? Perhaps it does not close the connection correctly / does not clear the data in db, and this only happens when your application is closed (all memory is located, and all connections are closed / reset).

In addition, I suggest using the BindingSource with which the grid is bound and changing its data source - it has an event to automatically notify the grid if its data source has been changed and which will cause it to update.

+1
source

As you said, these are different forms, I believe that a pointer to Form1 does not indicate the desired form. you must pass the pointer of this form using this to this form.

When you create form 2, define as follows:

 Form2 = new Form2(); Form2.form1 = this; 

Then your code should work.

+1
source

It seems you are using List as a data source. I found that using a shared list is suitable for read-only data, but to do any updates you need something with more power. I do not know about WPF, but in winForms I had great success with the IBindingList interface and the general BindingList collection. The general BindingList collection implements the IBindingList interface for you. I would read MSDN articles on both of them. I pretty much stopped using the IBindingList interface, but it is still great for implementation.

http://msdn.microsoft.com/en-us/library/system.componentmodel.ibindinglist.aspx

http://msdn.microsoft.com/en-us/library/ms132679.aspx

+1
source

The answer is that the gridview is connected to the BindingList<Customers> , and not to the List<Customers> . Hope this solves your problem ...

+1
source

If you change the list to a BindingList, you will have success. I put together a sample that just had a DataGridView and a couple of buttons on the form.

Button

button1 generates some fake data and assigns a data source. button2 adds another Client to the main list.

The DataGridView is updated when the underlying BindingList changes.

code:

 public class Customer { public int Id { get; set; } public string Name { get; set; } public string SurName { get; set; } } public partial class Form1 : Form { BindingList<Customer> customers = new BindingList<Customer>(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 10; ++i) { customers.Add(new Customer { Id = i, Name = "Name" + i, SurName = "Surname" + i }); } dataGridView1.DataSource = customers; } private void button2_Click(object sender, EventArgs e) { customers.Add(new Customer { Id = 22, Name = "Newname", SurName = "Newsurname" }); } } 

Now the reason is that BindingList<T> implements IBindingList , by the way, and this interface has, among other things, an event called ListChanged that occurs when a list or something in the list changes.

+1
source

If you are not required to use the client list strictly as a data source for a datagridview, then here is a much better solution using datatable. You will receive an updated list of clients and datagridview after insertion

 public class CustomerService { public DataTable ShowCustomers() { string cns = "data source=.; database=custer; user id=sa; password=*****"; SqlConnection conn = new SqlConnection(cns); SqlDataAdapter da = new SqDataAdapter("select * from Customers", conn); DataTable dt = new DataTable(); da.Fill(dt); return dt; } } private void button1_Click(object sender, EventArgs e) { CustomerService service = new CustomerService(); if (txtCustomerName.Text != "" || txtCustomerSurname.Text != "") { customerservice.customerAdd(txtCustomerName.Text,txtCustomerSurname.Text); MessageBox.Show("Customer Added"); DataTable dt = service.ShowCustomers(); Form1.dataGridView1.DataSource = dt; //If you also need customer list. Provide the DataTable and get list List<Customers> customers = new List<Customers>(); for (int i=0;i<dt.Rows.Count;i++) { Customer customer = new Customer(); customer.CustomerID = Convert.ToInt32(dt.Rows[i][0]); customer.CustomerName = dt.Rows[i][1].ToString(); customer.CustomerSurname = dt.Rows[i][2].ToString(); customers.Add(customer); } } } 
+1
source

You need to bind data only once, not on each new record. That all data binding to the control, that all changes will be automatically reflected.

  • Make customers member of the CustomerService class data and modify your code accordingly:

     class CustomerService { List<Customers> customers; ... } 
  • Next, the binding code must be executed once, possibly when the first record is added to the List<Customers> .

     BindingSource binding = new BindingSource(); binding.DataSource = customers; dataGridView1.DataSource = binding; 

If you did it right, every time any record is deleted or added to the client collection, it should be automatically reflected in the DataGridView control.

+1
source

I think you must have used the update panel and could not update it

+1
source

I think this will solve your problem, check this out ........

 DataGrid.CommitEdit(); DataGrid.Items.Refresh(); 

Note

The CommitEdit () method fires the RowEditEnding event, this is an infinite loop . WPF does not allow updating the view while editing it, as an endless loop can occur. However, after editing, you can update the view. Try to remove the RowEditEnding (If Initialzed) event handler and update for Items; Then add an event handler:

+1
source

All Articles