简体   繁体   English

更新后如何刷新 c# dataGridView?

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

I have a dataGridView when I click on any row a form is opened to update the row data, but after ending updates the updating form is closed but the dataGridView data is not updated当我单击任何行时,我有一个 dataGridView,打开一个表单以更新行数据,但在结束更新后,更新表单关闭,但 dataGridView 数据未更新

How can i do that?我怎样才能做到这一点?

BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful. BindingSource是不使用第 3 方 ORM 的唯一方法,起初它可能看起来很啰嗦,但BindingSource上的一种更新方法的好处非常有用。

If your source is say for example a list of user strings如果您的来源是例如用户字符串列表

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

then when your done editing just update your data object whether that be a DataTable or List of user strings like here and ResetBindings on the BindingSource ;然后,当您完成编辑时,只需更新您的数据 object,无论是DataTable还是用户字符串列表,如此处以及BindingSource上的ResetBindings

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

Rebind your DatagridView to the source.将您的 DatagridView 重新绑定到源。

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

// Update Data in src1

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

I know i am late to the party but hope this helps someone who will do the same with Class binding我知道我迟到了,但希望这可以帮助那些对 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();

I know thats an old topic but i suddenly found the best way of doing it and it does not require nullifying the datasource and reassigning it.我知道这是一个老话题,但我突然找到了最好的方法,它不需要取消数据源并重新分配它。 Just use a BindingList instead of a List.只需使用 BindingList 而不是 List。

for example:例如:

//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

}

I use the DataGridView's Invalidate() function.我使用 DataGridView 的Invalidate() function。 However, that will refresh the entire DataGridView.但是,这将刷新整个 DataGridView。 If you want to refresh a particular row, you use dgv.InvalidateRow(rowIndex) .如果要刷新特定行,请使用dgv.InvalidateRow(rowIndex) If you want to refresh a particular cell, you can use dgv.InvalidateCell(columnIndex, rowIndex) .如果要刷新特定单元格,可以使用dgv.InvalidateCell(columnIndex, rowIndex) This is of course assuming you're using a binding source or data source.这当然是假设您使用的是绑定源或数据源。

I don't know if this has really been solved or not... but by looking at all the other answers, nothing seems quite clear.我不知道这是否真的解决了......但是通过查看所有其他答案,似乎没有什么很清楚。 The best way I found to do this is to put the same code, that was used to populate your datagridview into a method and pass it your form's datagridview , as so:我发现这样做的最好方法是放置相同的代码,该代码用于将您的datagridview填充到一个方法中并将其传递给您的表单的datagridview ,如下所示:

public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
{ }

The code within the method is the exact same as the code used to populate the datagirdview originally, except for the datagridview name changing to whatever you called it in your method.该方法中的代码与最初用于填充datagirdview的代码完全相同,只是datagridview名称更改为您在方法中调用的任何名称。

Now this method is called in your parent form.现在这个方法在你的父表单中被调用。

The child form is launched via a .ShowDialog() then the method is called after so that it is called right after the child for is closed... as so:子窗体通过.ShowDialog()启动,然后调用该方法,以便在子窗体关闭后立即调用它......如下所示:

ChildForm.ShowDialog();

ConnectAndPopulateDataGridView(dataGridView1);

You can use the DataGridView refresh method.您可以使用 DataGridView 刷新方法。 But... in a lot of cases you have to refresh the DataGridView from methods running on a different thread than the one where the DataGridView is running.但是......在很多情况下,您必须从运行在与运行 DataGridView 的线程不同的线程上的方法刷新 DataGridView。 In order to do that you should implement the following method and call it rather than directly typing DataGridView.Refresh():为此,您应该实现以下方法并调用它,而不是直接键入 DataGridView.Refresh():

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

You just need to redefine the DataSource .您只需要重新定义DataSource So if you have for example DataGridView's DataSource that contains a, b, i c:因此,如果您有例如包含 a、b、i c 的 DataGridView 的 DataSource:

DataGridView.DataSource = a, b, c

And suddenly you update the DataSource so you have just a and b, you would need to redefine your DataSource:突然你更新了 DataSource,所以你只有 a 和 b,你需要重新定义你的 DataSource:

DataGridView.DataSource = a, b

I hope you find this useful.希望这个对你有帮助。

Thank you.谢谢你。

You can use SqlDataAdapter to update the DataGridView您可以使用 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