如何更新winforms中的数据网格视图? [英] how to update data grid view in winforms?

查看:125
本文介绍了如何更新winforms中的数据网格视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的winforms中有一个页面,我想在表格上显示所有的数据,这样用户就可以看到表格中的信息,但是在winform上。我以前没有这样做,所以需要你们的一些指导,我希望它一直(添加新的记录)更新和可编辑,所以如果需要,可以删除一行。我所拥有的表格是预订,我可以在表格中添加数据的形式是frmBooking,所以我希望在程序运行时预订数据存储在datagridview中,以便用户可以查看其预订。感谢我的代码是:

  using System; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;

命名空间项目
{
public partial class frmViewBookings:Form
{
public frmViewBookings()
{
InitializeComponent() ;
}

private void btnClose_Click(object sender,EventArgs e)
{
Form3 mainpage = new Form3();
mainpage.Show();
this.Close();
}

private void frmViewBookings_Load(object sender,EventArgs e)
{
// TODO:这行代码将数据加载到usersDataSet1.Booking表中。您可以根据需要移动或移除它。
this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);

}
}
}


解决方案

您可以使用将datagridview绑定到源的datagridview的DataSource属性。例如。数据表。



无论何时编辑datatable,datagridview都会更新。



您还可以通过添加行和列手动修改datagridview,但这更有用。



编辑:如果希望更改更新到datagridview,您需要修改作为绑定源的datatable。


I have a page in my winforms, where i want to display all the data on a table so the user can see the information in the table but on the winforms. i have not done this before so need some guidance from you people, i want it to consistently (when new records are added) update and be editable so a row can be deleted if it needs to. the table i have is "booking" and the form on which i can add data to this table is "frmBooking" so i want booking data made whilst the program is run to be stored in the datagridview so the user can view their bookings made. thanks the code i have is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project
{
    public partial class frmViewBookings : Form
    {
        public frmViewBookings()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Form3 mainpage = new Form3();
            mainpage.Show();
            this.Close();
        }

        private void frmViewBookings_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
            this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);

        }
    }
}

解决方案

You can use the DataSource property of the datagridview which binds the datagridview to a source. eg. A DataTable.

Whenever the datatable is edited the datagridview is updated.

You could also modifiy the datagridview manually by adding rows and columns but this is a bit more work.

Edit: If you wish your changes to update to the datagridview you need to modify the datatable which is the binding source.

这篇关于如何更新winforms中的数据网格视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆