Quick note: I have no idea what they've done with these forums, but for some reason, I can't find the way to put this directly into the C# forums only.
One note:
The initial button (1) should return a select. The second button (2) should update whatever rows in the gridview were updated. Based on coding in C#, at some point on updating a reader will need to be executed, because C# will need to read the data entered in the gridview and update the values in the database based on the entered values in the gridview. My guess is that the pseudo code would look something like:
Variables declared for appropriate columns in the gridview (something like string companyname = dataGridView1.ChangedColumn[1].Convert.ToString(); - as a gridview is an array)
Data Reader opened to read the new values entered in the gridview
Database connection opened
Update command passes changed values in the gridview into the database based on an id field (the WHERE clause).
Database connection closed
Tools: C#, Windows Form Application
I have a simple Windows form application that returns data from a table in a DataGridView (see the below code):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Form1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = "integrated security=SSPI;data source=SERVER001;persist security info=False;initial catalog=Elephants";
connect.Open();
SqlCommand two = new SqlCommand("SELECT Name, Date FROM Table", connect);
SqlDataAdapter adapt = new SqlDataAdapter(two);
adapt.SelectCommand = two;
DataTable datatab = new DataTable();
adapt.Fill(datatab);
BindingSource bind = new BindingSource();
bind.DataSource = datatab;
DataGridView1.DataSource = bind;
adapt.Update(datatab);
connect.Close();
}The user pushes a button, and a data table is returned, all of which returns the appropriate data. Now DataGridView1 allows users to add information (or alter information) to the table that was returned, but these data are not inputted into the database, which is the next step I'm trying to complete (inserting and updating). I can find a ton of information about the above with windows forms (selecting data only), but nothing, using windows forms about updating or deleting where the full code is shown to ascertain what I'm missing.
private void button2_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = "integrated security=SSPI;data source=SERVER001;persist security info=False;initial catalog=Elephants";
connect.Open();
// originally, tried changing the SQL command to an update statement from the button1, and it didn't update the items, nor recognize where the updated values were supposed to come from.
connect.Close();
}Note: this is NOT an ASP application and I don't want or need ASP code, which there are tons of examples for on the internet (it seems every article assumes a user is building an ASP application). This is only for a Windows forms. Where is a good resource that shows a step-by-step process of how to update and insert data in a DataGridView table, where users press a button, and values that they inputted into the DataGridview1 table will be inserted (or updated) in the database?
Note: this YouTube video here, http://www.youtube.com/watch?v=Sm5mxkytfWk, shows how to do some of the above code, which is the original reference (though I'm using MS SQL Server).







