473,412 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

DataGridView update. Win forms With form1 and form2.

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.

Expand|Select|Wrap|Line Numbers
  1. namespace Lager_Status
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.  
  6.         private Form2 NewForm2;
  7.  
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.             dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
  12.             dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
  13.             dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(dataGridView1_RowsRemoved);
  14.             dataGridView1.CellEnter += new DataGridViewCellEventHandler(dataGridView1_CellEnter);
  15.             dataGridView1.RowEnter += new DataGridViewCellEventHandler(dataGridView1_RowEnter);
  16.             dataGridView1.ShowEditingIcon = true;
  17. EventHandler(dataGridView1_GotFocus);
  18.             dataGridView1.GotFocus += new EventHandler(Form1_GotFocus);
  19.  
  20.             lagerTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
  21.             lagerTableAdapter.Adapter.AcceptChangesDuringFill = true;
  22.  
  23.         }
  24.  
  25.         void Form1_GotFocus(object sender, EventArgs e)
  26.         {
  27.  
  28.                 DataTable changes = visData.Tables["Lager"].GetChanges();
  29.         }
  30.  
  31.         void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
  32.         {
  33.             Update();
  34.         }
  35.  
  36.         void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
  37.         {
  38.             Update();
  39.         }
  40.  
  41.         void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
  42.         {
  43.             Update();
  44.         }
  45.  
  46.         void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  47.         {
  48.             Update();
  49.         }
  50.  
  51.         void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  52.         {
  53.             Update();
  54.         }
  55.  
  56.         private void Form1_Load(object sender, EventArgs e)
  57.         {
  58.             // TODO: This line of code loads data into the 'visData.Lager' table. You can move, or remove it, as needed.
  59.             this.lagerTableAdapter.Fill(this.visData.Lager);
  60.             this.dataGridView1.Sort(vareNummerDataGridViewTextBoxColumn, ListSortDirection.Ascending);
  61.         }
  62.         private void Update()
  63.         {
  64.             try
  65.             {
  66.                 lagerTableAdapter.Update(visData);
  67.             }
  68.             catch
  69.             {
  70.                 MessageBox.Show("Feil verdi i feltet", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  71.             }
  72.         }
  73.  
  74.         private void btnAvbryt_Click(object sender, EventArgs e)
  75.         {
  76.             Environment.Exit(0);
  77.         }
  78.  
  79.  
  80.         private void btnSalg_Click(object sender, EventArgs e)
  81.         {
  82.  
  83.             NewForm2 = new Form2();
  84.             NewForm2.Show(this);
  85.             this.Hide();
  86.         }
  87.  
  88.  
  89.         private void btnSok_Click(object sender, EventArgs e)
  90.         {
  91.             if (txtSok.Text != "")
  92.             {
  93.                 //dataGridView1.Visible = false;
  94.                 string connString = Properties.Settings.Default.LagerConnectionString;
  95.  
  96.                 //create the database query
  97.                 string query = "SELECT * FROM Lager WHERE (VareNummer LIKE '%" + txtSok.Text + "%' OR Beskrivelse LIKE '%" + txtSok.Text + "%' OR Lokasjon LIKE '%" + txtSok.Text + "%') ORDER BY VareNummer ASC";
  98.  
  99.                 //create an OleDbDataAdapter to execute the query
  100.                 SqlDataAdapter dAdapter = new SqlDataAdapter(query, connString);
  101.  
  102.                 //create a command builder
  103.                 SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);
  104.  
  105.                 //create a DataTable to hold the query results
  106.                 DataTable dTable = new DataTable();
  107.  
  108.                 //fill the DataTable
  109.                 dAdapter.Fill(dTable);
  110.  
  111.                 //BindingSource to sync DataTable and DataGridView
  112.                 BindingSource bSource = new BindingSource();
  113.  
  114.                 //set the BindingSource DataSource
  115.                 bSource.DataSource = dTable;
  116.  
  117.                 //set the DataGridView DataSource
  118.                 dataGridView1.DataSource = bSource;
  119.                 dAdapter.Update(dTable);
  120.             }
  121.         }
  122.     }
  123. }


Form2:

Expand|Select|Wrap|Line Numbers
  1. namespace Lager_Status
  2. {
  3.     public partial class Form2 : Form
  4.     {
  5.         private Form1 NewForm1;
  6.  
  7.         public Form2()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.  
  12.         private void Form2_Load(object sender, EventArgs e)
  13.         {
  14.             // TODO: This line of code loads data into the 'lager._Lager' table. You can move, or remove it, as needed.
  15.             this.lagerTableAdapter3.Fill(this.lager._Lager);
  16.             // TODO: This line of code loads data into the 'lagerDataSet.Lager' table. You can move, or remove it, as needed.
  17.             this.lagerTableAdapter2.Fill(this.lagerDataSet.Lager);
  18.             // TODO: This line of code loads data into the 'salg.Lager' table. You can move, or remove it, as needed.
  19.             this.lagerTableAdapter1.Fill(this.salg.Lager);
  20.             // TODO: This line of code loads data into the 'visData.Lager' table. You can move, or remove it, as needed.
  21.             this.lagerTableAdapter.Fill(this.visData.Lager);
  22.  
  23.         }
  24.  
  25.         private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  26.         {
  27.             //Convert the parent form to accurate type and use special functions.
  28.  
  29.             Form1 parent = this.Owner as Form1;
  30.  
  31.             if (parent != null)
  32.  
  33.                 if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  34.                 {
  35.                     e.Cancel = true;
  36.                 }
  37.                 else
  38.                 {
  39.                     this.Owner.Show();
  40.                 }
  41.             return;
  42.         }
  43.  
  44.         private void ddlVareNummer_SelectedIndexChanged(object sender, EventArgs e)
  45.         {
  46.             if (ddlVareNummer.SelectedValue != null)
  47.             {
  48.                 using (SqlScalar helper2 = new SqlScalar())
  49.                 using (SqlScalar helper = new SqlScalar())
  50.                 {
  51.                     string info = helper2.Execute("SELECT Antall FROM Lager WHERE VareNummer = '" + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
  52.                     {
  53.  
  54.                         if (info != null)
  55.                         {
  56.                             lblAntall.Visible = true;
  57.                             lblAntall.Text = "Du har nå " + " " + info + " " + " igjen på lager!";
  58.                             txtAntallSolgt.Text = "";
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.  
  65.         private void btnSelg_Click(object sender, EventArgs e)
  66.         {
  67.             lblAntall.Text = "";
  68.             lblAntall.Visible = false;
  69.             if (ddlVareNummer.SelectedValue != null)
  70.             {
  71.                 using (SqlScalar helper2 = new SqlScalar())
  72.                 using (SqlScalar helper = new SqlScalar())
  73.                 using (SqlNonQuery helper3 = new SqlNonQuery())
  74.                 {
  75.                     try
  76.                     {
  77.                         string info = helper2.Execute("SELECT Antall FROM Lager WHERE VareNummer = '" + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
  78.                         //string info2 = helper.Execute("SELECT Beskrivelse FROM Lager WHERE VareNummer = '" 
  79.                         //  + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
  80.                         {
  81.                             if (info != null)
  82.                             {
  83.                                 string salg = helper3.Execute("UPDATE Lager SET Antall = " + int.Parse(info) + " - " + int.Parse(txtAntallSolgt.Text) + " WHERE VareNummer = '" + ddlVareNummer.SelectedValue + "'").ToString();
  84.                                 //"UPDATE Lager SET Antall = txtAntallSolgt - (-1) WHERE VareNummer = '" + ddlVareNummer.SelectedValue + "'");
  85.                                 //"UPDATE Users SET FeilPassExpire = '" + DateTime.Now + "'+ 24H where UserName = '" + txtUserName.Text + "'");
  86.                                 //lbAntall.Text = info;
  87.                                 string info2 = helper.Execute("SELECT Antall FROM Lager WHERE VareNummer = '" + ddlVareNummer.SelectedValue.ToString() + "'").ToString();
  88.                                 lblAntall.Visible = true;
  89.                                 lblAntall.Text = "Det er da " + " " + info2 + " " + " igjen i varebeholdningen din.";
  90.                                 txtAntallSolgt.Text = "";
  91.                             }
  92.                             else
  93.                             {
  94.                                 return;
  95.                             }
  96.                         }
  97.                     }
  98.                     catch
  99.                     {
  100.                         MessageBox.Show
  101.                             ("Sorry but something went wrong. Try look at what you typed again", "Error",
  102.                             MessageBoxButtons.OK, MessageBoxIcon.Error);
  103.  
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.  
  109.         public void btnAvbryt_Click(object sender, EventArgs e)
  110.         {
  111.             this.Close();
  112.         }
  113.  
  114.     }
  115. }
Jan 19 '10 #1
0 2303

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bob | last post by:
Declaring a module level form object in multiple froms within a project to show another project form causes a stack overflow and other assorted errors. Can anyone help?
6
by: Ayende Rahien | last post by:
Excetremely annoying problem, I've an application with a long startup time. So I created another form with my logo in it to as a splash screen. The splash screen is run from another thread and is...
2
by: AbdSol | last post by:
How to code…….. Form1 DataGridView Form2 DataGridView the selected rows data to be sent to Form1 and Form2 to exit. // Form1 DataGridViewRowCollection rows = this.mGrid.Rows;...
5
by: Jason Huang | last post by:
Hi, In Global.cs, I have public string myString="". Another 2 forms are Form1.cs, Form2.cs. How do I use the Global.cs' myString to store a string in Form1 and then pass to Form2? Thanks for...
8
by: Narayan | last post by:
Hi Has anyone actually tried out the code in the link given below. It doesn't work for me. \\\ Dim f2 as Form2 f2 = New Form2() f2.Show() Me.Close()
3
by: DotNetNewbie | last post by:
I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days. I am having trouble getting one of the exercises to work at the end of day 4. Exercises: 1. Create a new...
0
by: AFB | last post by:
Hi: I have a form called "Form1" and another called "Form2". I have a panel in Form1 and I add to the panel the Form2. The code is something like... Dim FormuGestorProyectos As From2...
7
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
1
by: =?Utf-8?B?RGF0YUdyaWRWaWV3IGFkZCByb3cgZHluYW1pY2Fs | last post by:
Hi, I am working on the windows application. I created a form Form1 to list records. I used datagridview to show data and bound it to a dataadapter. User can click Update button in Form1 to open...
4
by: BD | last post by:
I have an MDI application with a datagridview on a childform1 that will open childform2 with a double-click event on childform1. Childform2 works on a record and upon closing I want the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.