Question for you:
I am using Visual Studio 2015 to make a Windows Form Application. The goal is just to be able to edit a simple table in a netezza database using a data grid viewer control.
I put the data grid viewer control on my form, created a data source that points to the table, and then pointed the data grid view to that table. It works fine when I run it and displays the 10 or so rows of data perfectly.
Is there an easy way to update the data from the datagridview? I see there are editing/locking options but even with all of them set appropriately, if I edit a value in the cell of the grid and hit enter, it does not go back and update the database. Do I need to manually code for this? I wasn't sure if the grid has a way to do it automatically. In the code I checked all the methods for the tableadapter and didn't find a .Update or anything like that.
some advice would be greatly appreciated. Thank you!
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;
}
}
}