繁体   English   中英

更新后如何刷新 c# dataGridView?

[英]How can I refresh c# dataGridView after update ?

当我单击任何行时,我有一个 dataGridView,打开一个表单以更新行数据,但在结束更新后,更新表单关闭,但 dataGridView 数据未更新

我怎样才能做到这一点?

BindingSource是不使用第 3 方 ORM 的唯一方法,起初它可能看起来很啰嗦,但BindingSource上的一种更新方法的好处非常有用。

如果您的来源是例如用户字符串列表

List<string> users = GetUsers();
BindingSource source = new BindingSource();
source.DataSource = users;
dataGridView1.DataSource = source;

然后,当您完成编辑时,只需更新您的数据 object,无论是DataTable还是用户字符串列表,如此处以及BindingSource上的ResetBindings

users = GetUsers(); //Update your data object
source.ResetBindings(false);

将您的 DatagridView 重新绑定到源。

DataGridView dg1 = new DataGridView();
dg1.DataSource = src1;

// Update Data in src1

dg1.DataSource = null;
dg1.DataSource = src1;

我知道我迟到了,但希望这可以帮助那些对 Class 绑定做同样事情的人

var newEntry = new MyClassObject();

var bindingSource = dataGridView.DataSource as BindingSource;
var myClassObjects = bindingSource.DataSource as List<MyClassObject>;
myClassObjects.Add(newEntry);
bindingSource.DataSource = myClassObjects;

dataGridView.DataSource = null;
dataGridView.DataSource = bindingSource;
dataGridView.Update();
dataGridView.Refresh();

我知道这是一个老话题,但我突然找到了最好的方法,它不需要取消数据源并重新分配它。 只需使用 BindingList 而不是 List。

例如:

//declare your list
private BindingList<myclass> mMyList = new BindingList<myclass>();

//then bind it to your datagrid, i usually do it on the Load event
private void Form1_Load(object sender, EventArgs e)
{
    _dgMyDatagrig.DataSource = mMyList;
}

//start populating your list 
private void addItem(mycclass item)
{
    mMylist.add(item);

//the datagrid will show automatically the new added/updated items, no need to do anything else

}

我使用 DataGridView 的Invalidate() function。 但是,这将刷新整个 DataGridView。 如果要刷新特定行,请使用dgv.InvalidateRow(rowIndex) 如果要刷新特定单元格,可以使用dgv.InvalidateCell(columnIndex, rowIndex) 这当然是假设您使用的是绑定源或数据源。

我不知道这是否真的解决了......但是通过查看所有其他答案,似乎没有什么很清楚。 我发现这样做的最好方法是放置相同的代码,该代码用于将您的datagridview填充到一个方法中并将其传递给您的表单的datagridview ,如下所示:

public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
{ }

该方法中的代码与最初用于填充datagirdview的代码完全相同,只是datagridview名称更改为您在方法中调用的任何名称。

现在这个方法在你的父表单中被调用。

子窗体通过.ShowDialog()启动,然后调用该方法,以便在子窗体关闭后立即调用它......如下所示:

ChildForm.ShowDialog();

ConnectAndPopulateDataGridView(dataGridView1);

您可以使用 DataGridView 刷新方法。 但是......在很多情况下,您必须从运行在与运行 DataGridView 的线程不同的线程上的方法刷新 DataGridView。 为此,您应该实现以下方法并调用它,而不是直接键入 DataGridView.Refresh():

    private void RefreshGridView()
    {
        if (dataGridView1.InvokeRequired)
        {
            dataGridView1.Invoke((MethodInvoker)delegate ()
            {
                RefreshGridView();
            });
        }
        else
            dataGridView1.Refresh();
    }  

您只需要重新定义DataSource 因此,如果您有例如包含 a、b、i c 的 DataGridView 的 DataSource:

DataGridView.DataSource = a, b, c

突然你更新了 DataSource,所以你只有 a 和 b,你需要重新定义你的 DataSource:

DataGridView.DataSource = a, b

希望这个对你有帮助。

谢谢你。

您可以使用 SqlDataAdapter 更新 DataGridView

     using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn))
            {
               DataTable dt = new DataTable();
                 ad.Fill(dt);
               dataGridView1.DataSource = dt;
            }
        }

暂无
暂无

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

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