簡體   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