M
Mark Yorkovich
Guest
I am building a data-driven WinForms application in C# using a PostgreSQL database. Currently I am populating my two DataGridViews as follows.
I have a class in which I plan on doing all of my database-related work - get data from the database, add update and delete data in the database tables, etc. That class currently looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Npgsql;
namespace Test_XRef_Tool.Xref_Test
{
public class Db
{
private string connString = String.Format("Server = localhost; Port = 5432; Database = dvdrental; User Id = userid; Password = password;");
// Used to retrieve data for population of controls (DGVs, CBOs, etc)
public DataTable GetData(string selectQuery)
{
NpgsqlConnection conn = new NpgsqlConnection(connString);
DataSet ds = new DataSet();
try
{
conn.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
conn.Close();
da.Fill(ds);
return ds.Tables[0];
}
}
}
}
And on my Form1 class I'm populating two DGVs. The code to populate them looks like this:
Db categoriesData = new Db();
dgvCategories.DataSource = categoriesData.GetData("SELECT * FROM actor");
Db defaultsData = new Db();
dgvDefaults.DataSource = defaultsData.GetData("SELECT * FROM film");
This all works fine to populate the DGVs, but I realized that during user-editing of the DGVs and various other operations I plan on doing in the program, I need access to the DGV's datasource - prior to any changes made to the DGV cells.
How do I do that? My thought is that I need to create some sort of object or variable on the Form1 class that will hold the current state of the DGV's data, but I can't figure out how to do that - if that's even the right way to do this.
Continue reading...
I have a class in which I plan on doing all of my database-related work - get data from the database, add update and delete data in the database tables, etc. That class currently looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Npgsql;
namespace Test_XRef_Tool.Xref_Test
{
public class Db
{
private string connString = String.Format("Server = localhost; Port = 5432; Database = dvdrental; User Id = userid; Password = password;");
// Used to retrieve data for population of controls (DGVs, CBOs, etc)
public DataTable GetData(string selectQuery)
{
NpgsqlConnection conn = new NpgsqlConnection(connString);
DataSet ds = new DataSet();
try
{
conn.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
conn.Close();
da.Fill(ds);
return ds.Tables[0];
}
}
}
}
And on my Form1 class I'm populating two DGVs. The code to populate them looks like this:
Db categoriesData = new Db();
dgvCategories.DataSource = categoriesData.GetData("SELECT * FROM actor");
Db defaultsData = new Db();
dgvDefaults.DataSource = defaultsData.GetData("SELECT * FROM film");
This all works fine to populate the DGVs, but I realized that during user-editing of the DGVs and various other operations I plan on doing in the program, I need access to the DGV's datasource - prior to any changes made to the DGV cells.
How do I do that? My thought is that I need to create some sort of object or variable on the Form1 class that will hold the current state of the DGV's data, but I can't figure out how to do that - if that's even the right way to do this.
Continue reading...