Hi everyone.
I hope someone can help me sort this out.
My problem is that I need to update my DataGridView on Form2.closing.
I need both the Form1 code and Form2 code. So I hope someone want's to make a little example for me. I'm a newbie so I don't know much yet but learning. ;-)
If it's something that you wonder about please ask and I'll answer.
Form1 Win app code.
Form2:
I hope someone can help me sort this out.
My problem is that I need to update my DataGridView on Form2.closing.
I need both the Form1 code and Form2 code. So I hope someone want's to make a little example for me. I'm a newbie so I don't know much yet but learning. ;-)
If it's something that you wonder about please ask and I'll answer.
Form1 Win app code.
Code:
namespace Lager_Status
{
public partial class Form1 : Form
{
private Form2 NewForm2;
public Form1()
{
InitializeComponent();
dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(dataGridView1_RowsRemoved);
dataGridView1.CellEnter += new DataGridViewCellEventHandler(dataGridView1_CellEnter);
dataGridView1.RowEnter += new DataGridViewCellEventHandler(dataGridView1_RowEnter);
dataGridView1.ShowEditingIcon = true;
EventHandler(dataGridView1_GotFocus);
dataGridView1.GotFocus += new EventHandler(Form1_GotFocus);
lagerTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
lagerTableAdapter.Adapter.AcceptChangesDuringFill = true;
}
void Form1_GotFocus(object sender, EventArgs e)
{
DataTable changes = visData.Tables["Lager"].GetChanges();
}
void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
Update();
}
void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
Update();
}
void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
Update();
}
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
Update();
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
Update();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'visData.Lager' table. You can move, or remove it, as needed.
this.lagerTableAdapter.Fill(this.visData.Lager);
this.dataGridView1.Sort(vareNummerDataGridViewTextBoxColumn, ListSortDirection.Ascending);
}
private void Update()
{
try
{
lagerTableAdapter.Update(visData);
}
catch
{
MessageBox.Show("Feil verdi i feltet", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnAvbryt_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnSalg_Click(object sender, EventArgs e)
{
NewForm2 = new Form2();
NewForm2.Show(this);
this.Hide();
}
private void btnSok_Click(object sender, EventArgs e)
{
if (txtSok.Text != "")
{
//dataGridView1.Visible = false;
string connString = Properties.Settings.Default.LagerConnectionString;
//create the database query
string query = "SELECT * FROM Lager WHERE (VareNummer LIKE '%" + txtSok.Text + "%' OR Beskrivelse LIKE '%" + txtSok.Text + "%' OR Lokasjon LIKE '%" + txtSok.Text + "%') ORDER BY VareNummer ASC";
//create an OleDbDataAdapter to execute the query
SqlDataAdapter dAdapter = new SqlDataAdapter(query, connString);
//create a command builder
SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//fill the DataTable
dAdapter.Fill(dTable);
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dataGridView1.DataSource = bSource;
dAdapter.Update(dTable);
}
}
}
}
Form2:
Code:
namespace Lager_Status
{
public partial class Form2 : Form
{
private Form1 NewForm1;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'lager._Lager' table. You can move, or remove it, as needed.
this.lagerTableAdapter3.Fill(this.lager._Lager);
// TODO: This line of code loads data into the 'lagerDataSet.Lager' table. You can move, or remove it, as needed.
this.lagerTableAdapter2.Fill(this.lagerDataSet.Lager);
// TODO: This line of code loads data into the 'salg.Lager' table. You can move, or remove it, as needed.
this.lagerTableAdapter1.Fill(this.salg.Lager);
// TODO: This line of code loads data into the 'visData.Lager' table. You can move, or remove it, as needed.
this.lagerTableAdapter.Fill(this.visData.Lager);
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//Convert the parent form to accurate type and use special functions.
Form1 parent = this.Owner as Form1;
if (parent != null)
if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
else
{
this.Owner.Show();
}
return;
}
private void ddlVareNummer_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlVareNummer.SelectedValue != null)
{
using (SqlScalar helper2 = new SqlScalar())
using (SqlScalar helper = new SqlScalar())
{
string info = helper2.Execute("SELECT Antall FROM Lager WHERE VareNummer = '" + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
{
if (info != null)
{
lblAntall.Visible = true;
lblAntall.Text = "Du har nå " + " " + info + " " + " igjen på lager!";
txtAntallSolgt.Text = "";
}
}
}
}
}
private void btnSelg_Click(object sender, EventArgs e)
{
lblAntall.Text = "";
lblAntall.Visible = false;
if (ddlVareNummer.SelectedValue != null)
{
using (SqlScalar helper2 = new SqlScalar())
using (SqlScalar helper = new SqlScalar())
using (SqlNonQuery helper3 = new SqlNonQuery())
{
try
{
string info = helper2.Execute("SELECT Antall FROM Lager WHERE VareNummer = '" + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
//string info2 = helper.Execute("SELECT Beskrivelse FROM Lager WHERE VareNummer = '"
// + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
{
if (info != null)
{
string salg = helper3.Execute("UPDATE Lager SET Antall = " + int.Parse(info) + " - " + int.Parse(txtAntallSolgt.Text) + " WHERE VareNummer = '" + ddlVareNummer.SelectedValue + "'").ToString();
//"UPDATE Lager SET Antall = txtAntallSolgt - (-1) WHERE VareNummer = '" + ddlVareNummer.SelectedValue + "'");
//"UPDATE Users SET FeilPassExpire = '" + DateTime.Now + "'+ 24H where UserName = '" + txtUserName.Text + "'");
//lbAntall.Text = info;
string info2 = helper.Execute("SELECT Antall FROM Lager WHERE VareNummer = '" + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
lblAntall.Visible = true;
lblAntall.Text = "Det er da " + " " + info2 + " " + " igjen i varebeholdningen din.";
txtAntallSolgt.Text = "";
}
else
{
return;
}
}
}
catch
{
MessageBox.Show
("Sorry but something went wrong. Try look at what you typed again", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
public void btnAvbryt_Click(object sender, EventArgs e)
{
this.Close();
}
}
}