繁体   English   中英

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

[英]Show immediately data in datagridview After the Insertion/Updating

我知道它在SOF中已经被问过很多次了,但是我向您保证,我尝试了诸如.refresh,.update,在插入后调用该方法之类的一切。更改datagridview。 我正在使用存储过程在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();    
            }
        }
    } 
}

这是我的用户控件,我的控件如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();
        }
    }

}

这是一个基于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);
        }
    }
}

最后,这是我存储的过程

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

我使用了您发布的代码,并设法使GridView刷新而不必重新加载。

请如下更改显示类别

    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();
            }
        }
    }

更新其余代码,以适当地引用Display.Display_Customer

Display.Display_Customer(CustomersList);

要么

Display.Display_Customer(_view.CustomersList);

通过上述更改,我设法在更新时刷新了DataGridView

更新以启用DataGridView搜索:

由于BindingSourse用作数据源,因此可以利用BindingSourse.Filter启用搜索。 如下更新搜索文本框;

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