简体   繁体   English

插入/更新后立即在datagridview中显示数据

[英]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. 我知道它在SOF中已经被问过很多次了,但是我向您保证,我尝试了诸如.refresh,.update,在插入后调用该方法之类的一切。更改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. 我正在使用存储过程在datagridview上显示数据,也可以进行插入,更新等操作。我希望有人能够帮助我指出我做错了什么或错过了什么。 Thank you 谢谢

Here's my class for displaying data on my datagridview 这是我的课程,用于在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 这是我的用户控件,我的控件如datagridview,button等

 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 这是一个基于ID PS从datagridview获取数据的表单:Btn_Update向我的用户控件显示新表单

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. 我使用了您发布的代码,并设法使GridView刷新而不必重新加载。

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

Display.Display_Customer(CustomersList);

Or 要么

Display.Display_Customer(_view.CustomersList);

With the above changed, I managed to refresh the DataGridView upon update. 通过上述更改,我设法在更新时刷新了DataGridView

Update to enable DataGridView Search: 更新以启用DataGridView搜索:

Since BindingSourse is used as the DataSource, you can utilize the BindingSourse.Filter to enable the search. 由于BindingSourse用作数据源,因此可以利用BindingSourse.Filter启用搜索。 Update the Search textbox as below; 如下更新搜索文本框;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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