简体   繁体   中英

Show immediately data in datagridview After the Insertion/Updating

I know it's been asked many times here in SOF but i assure you i tried everything like .refresh, .update, call the method after the insertion etc. but still nothing happen i need to refresh my user control again just to see the new data change in datagridview. I'm using stored procedure to display data on my datagridview also for inserting, updating etc. I hope someone would be able to help me to pin point what i do wrong or what i've missed. Thank you

Here's my class for displaying data on my datagridview

public static class Display 
{
    public static void Display_Customer(DataTable dt, DataGridView dgv)
    {     
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
        {
            using (var cmd = new SqlCommand("usp_GetCustomers", con))
            {               
                con.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                using (var sda = new SqlDataAdapter(cmd))
                {
                    sda.Fill(dt);
                    var bsource = new BindingSource();
                    bsource.DataSource = dt;
                    dgv.DataSource = bsource;
                    sda.Update(dt);
                }
                con.Close();    
            }
        }
    } 
}

Here's my usercontrol where my controls like datagridview, button etc

 public partial class ManageCustomer : UserControl
{
    public ManageCustomer()
    {
        InitializeComponent();
    }
    public DataTable dt = new DataTable();
    private void ManageCustomer_Load(object sender, EventArgs e)
    {
        Display.Display_Customer(dt, CustomersList);   
        Customization._DGVWidth(CustomersList);
    }
    private void CustomersList_SelectionChanged(object sender, EventArgs e)
    {
        Register.CustomerSelection(CustomersList, lbl_id, lbl_name, lbl_gender, 
        lbl_contact,lbl_email, lbl_address, PreviewImage);      
    }
    private void Btn_Update_Click(object sender, EventArgs e)
    {
        var uc = new UpdateCustomer(this).ShowDialog();
    }
    private void CustomersList_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = CustomersList.Rows[e.RowIndex];
            Variables.ID = row.Cells["Customer ID"].Value.ToString();
        }
    }

}

Here's a form that get's the data from datagridview based on ID PS: Btn_Update show new form to my usercontrol

public partial class UpdateCustomer : Form
{
    ManageCustomer _view;
    public UpdateCustomer(ManageCustomer view)
    {
        InitializeComponent();
        UpdateC.CustomerInformation(Variables.ID, lbl_path, PreviewImage, txt_name, txt_contact, txt_email, txt_address);
        this._view = view;
    }
    private void btn_update_Click(object sender, EventArgs e)
    {
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
        {
            UpdateC._Update(lbl_path.Text, txt_name.Text, txt_contact.Text, txt_email.Text, txt_address.Text);
            Display.Display_Customer(_view.dt, _view.CustomersList);
        }
    }
}

Finally, Here's my stored proc

USE [SalesInventory]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_GetCustomers]

AS
BEGIN
SET NOCOUNT ON; 
                SELECT   CustomerID as 'Customer ID', Images, Full_Name, Gender, Contact_Number as 'Contact Number', Email, Home_Address as 'Address'
                FROM Customer_List      
END

I played around with the code you have posted and managed to get the GridView refreshed without having to reload.

Please change the Display class as below

    public static void Display_Customer3(DataGridView dgv)
    {
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
        {
            using (var cmd = new SqlCommand("usp_GetCustomers", con))
            {
                con.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                using (var sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();     // Initiate the datatable here to avoid getting duplicate data during 'sda.Fill(dt)'
                    sda.Fill(dt);
                    var bsource = new BindingSource();
                    bsource.DataSource = dt;
                    dgv.DataSource = bsource;
                    //sda.Update(dt);
                }
                con.Close();
            }
        }
    }

Update rest of the code where Display.Display_Customer is referenced to appropriately.

Display.Display_Customer(CustomersList);

Or

Display.Display_Customer(_view.CustomersList);

With the above changed, I managed to refresh the DataGridView upon update.

Update to enable DataGridView Search:

Since BindingSourse is used as the DataSource, you can utilize the BindingSourse.Filter to enable the search. Update the Search textbox as below;

BindingSource dgBS = (BindingSource)CustomersList.DataSource;
dgBS.Filter = string.Format("FileName LIKE '%{0}%'", txtFilter_FileName.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM