【发布时间】:2017-06-26 05:54:06
【问题描述】:
给你的问题:
我正在使用 Visual Studio 2015 制作 Windows 窗体应用程序。目标只是能够使用数据网格查看器控件编辑 netezza 数据库中的简单表。
我将数据网格查看器控件放在我的表单上,创建了一个指向表的数据源,然后将数据网格视图指向该表。当我运行它并完美显示大约 10 行数据时,它运行良好。
有没有一种简单的方法可以从 datagridview 更新数据?我看到有编辑/锁定选项,但即使所有这些选项都设置得当,如果我在网格的单元格中编辑一个值并按 Enter,它不会返回并更新数据库。我需要为此手动编码吗?我不确定网格是否有办法自动完成。在代码中,我检查了 tableadapter 的所有方法,但没有找到 .Update 或类似的东西。
我们将不胜感激一些建议。谢谢!
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 SBTForceClose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadGrid()
{
dataGridView1.DataSource = dataSet1.LKP_SBT_FORCE_CLOSE;
dataGridView1.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
LoadGrid();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dataSet1.LKP_SBT_FORCE_CLOSE' table. You can move, or remove it, as needed.
this.lKP_SBT_FORCE_CLOSETableAdapter.Fill(this.dataSet1.LKP_SBT_FORCE_CLOSE);
}
private DataRow LastDataRow = null;
private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
if (LastDataRow.RowState == DataRowState.Modified)
{
this.lKP_SBT_FORCE_CLOSETableAdapter.Update(this.LastDataRow);
}
}
}
private void lKPSBTFORCECLOSEBindingSource_PositionChanged(object sender, EventArgs e)
{
BindingSource thisBindingSource = (BindingSource)sender;
DataRow ThisDataRow = ((DataRowView)thisBindingSource.Current).Row;
if (ThisDataRow == LastDataRow)
{
throw new ApplicationException();
}
UpdateRowToDatabase();
LastDataRow = ThisDataRow;
}
}
}
【问题讨论】:
标签: c# database winforms visual-studio-2015