473,405 Members | 2,167 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,405 software developers and data experts.

How to change the data of datagridview when changes has made from another form

29
I'm currently developing a windows application.

I have two forms, Form1 and Form2. They are both open.

In Form1, I have two dateTimePickers (start date and end date), two text boxes (no of records and no of days) and two buttons (submit and cancel). In Form2, I have a dataGridView that displays data from a text file.

For example, I set the start date from form1 as May 5, 2010 and end date as May 6, 2010. After i click the submit button, form2 will read the text file and display the data that are >= start date and <= end date.
May 6 '10 #1
11 2353
tlhintoq
3,525 Expert 2GB
Original Poster: How do I get my Form2 to react to something on my Form1?
How do I make my Form1 control something on my Form2?
Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
Events tutorial (including Form to Form which is the same as class to class)
This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.

Bad: Directly accessing controls of one class/form from another.
Expand|Select|Wrap|Line Numbers
  1. bool IsOn = Form2.button1.IsChecked;
Good: Use a property to get such information
Expand|Select|Wrap|Line Numbers
  1. //Form1
  2. bool IsOn = Form2.IsOn;
Expand|Select|Wrap|Line Numbers
  1. //Form 2
  2. public bool IsOn
  3. {
  4.    get { return CheckBox1.Checked; }
  5.    set { CheckBox1.Checked = value; }
  6. }
It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: It won't break references in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden is not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentalize the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

Form2
Expand|Select|Wrap|Line Numbers
  1. public bool IsOn
  2. {
  3.    get { return btnMeaningfulName.Checked; }
  4.    set {
  5.             btnMeaningfulName.Checked = value;
  6.             panelDashboard.Visible = value;
  7.             labelStatus = value == true ? "On" : "Off";
  8.             btnRunNow.Enabled = value;
  9.        }
  10. }
Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.
May 6 '10 #2
mylixes
29
@tlhintoq
I appreciate your reply regarding this matter..
Can you give me the code that can help me solve the problem?

This is what I currently have:

//when Submit button is click is Form1
Expand|Select|Wrap|Line Numbers
  1. private void btnSubmit_Click(object sender, EventArgs e)
  2.         {
  3.             string startDate = "", endDate = "", prevDays = "", prevRecords = "";
  4.  
  5.             if (rdbDate.Checked)
  6.             {
  7.                 startDate = Convert.ToDateTime(dtpStartDate.Text).ToShortDateString();
  8.                 endDate = Convert.ToDateTime(dtpEndDate.Text).ToShortDateString();
  9.                 prevRecords = "";
  10.                 prevDays = "";
  11.             }
  12.             else if (rdbPrevDays.Checked)
  13.             {
  14.                 string[] str;
  15.                 str = txtPrevDays.Text.Split(' ');
  16.                 prevDays = str[0];
  17.  
  18.                 prevRecords = "";
  19.                 startDate = "";
  20.                 endDate = "";
  21.             }
  22.             else
  23.             {
  24.                 string[] str;
  25.                 str = txtPrevRecords.Text.Split(' ');
  26.                 prevRecords = str[0];
  27.  
  28.                 startDate = "";
  29.                 endDate = "";
  30.                 prevDays = "";
  31.  
  32.             }
  33.  
  34.             cashierLoginRecordsFrm = new FrmCashierLoginRecords(startDate, endDate, prevDays, prevRecords, 1);
  35.  
  36.             int i = 0;
  37.  
  38.             Form frm;
  39.             while (Application.OpenForms.Count > 1)
  40.             {
  41.                 if (Application.OpenForms.Count == 2)
  42.                 {
  43.                     break;
  44.                 }
  45.                 else
  46.                 {
  47.                     if (i < Application.OpenForms.Count)
  48.                     {
  49.                         frm = Application.OpenForms[i];
  50.                         if (frm.Name.Equals("FrmCashierLoginRecords"))
  51.                         {
  52.                             Application.OpenForms[i].Close();
  53.                             break;
  54.                         }
  55.  
  56.                         i++;
  57.                     }
  58.                     else
  59.                         break;
  60.  
  61.                 }
  62.             }
  63.  
  64.             cashierLoginRecordsFrm.Show();
  65.  
  66.         }

//Form2
Expand|Select|Wrap|Line Numbers
  1. private void FrmCashierLoginRecords_Load(object sender, EventArgs e)
  2.         {
  3.             Common.desiredLowerLocation.X = Common.panelLowerLocX;
  4.             Common.desiredLowerLocation.Y = Common.panelLowerLocY;
  5.  
  6.             this.Location = new Point(Common.desiredLowerLocation.X, Common.desiredLowerLocation.Y);
  7.  
  8.             int i = 0, x = 0;
  9.             string path = ".\\CashierRecord.txt";
  10.  
  11.  
  12.             string str = "";
  13.             string[] tempStr;
  14.             string[] result;
  15.             string[] splitTempStr;
  16.             string[] cashierCardNo = new string[10];
  17.             string[] date = new string[10];
  18.             string[] status = new string[10];
  19.             DateTime dateFromFile;
  20.  
  21.             dataGridView1.Refresh();
  22.             dataGridView1.Rows.Add(9);
  23.             dataGridView1["Column2", 0].ValueType = System.Type.GetType("System.Date");
  24.  
  25.             if (File.Exists(path))
  26.             {
  27.                 tempStr = File.ReadAllLines(path);
  28.  
  29.                 if (Common.isCashierLoginTransactionHistory)
  30.                 {
  31.                     result = new string[10];
  32.  
  33.                     if (tempStr.Length < 10)
  34.                     {
  35.                         i = 0;
  36.                         do
  37.                         {
  38.                             if (i == tempStr.Length)
  39.                                 break;
  40.  
  41.                             if (!tempStr[i].StartsWith(";"))
  42.                             {
  43.                                 result[i - 1] = tempStr[i];
  44.  
  45.                             }
  46.                             i++;
  47.  
  48.                         }
  49.                         while (i <= tempStr.Length);
  50.  
  51.                     }
  52.                     else
  53.                     {
  54.                         Array.Copy(tempStr, tempStr.Length - 10, result, 0, 10);
  55.                     }
  56.  
  57.                     Array.Reverse(result);
  58.  
  59.  
  60.                     for (i = 0; i < 10; i++)
  61.                     {
  62.                         if (result[i] != null)
  63.                         {
  64.                             if (!result[i].Equals(""))
  65.                             {
  66.                                 splitTempStr = result[i].Split('\t');
  67.  
  68.                                 cashierCardNo[i] = splitTempStr[0];
  69.                                 date[i] = splitTempStr[2];
  70.                                 status[i] = splitTempStr[4];
  71.  
  72.                                 dataGridView1["Column1", i - x].Value = cashierCardNo[i];
  73.                                 dataGridView1["Column2", i - x].Value = date[i];
  74.                                 dataGridView1["Column3", i - x].Value = status[i];
  75.                             }
  76.                             else
  77.                             {
  78.                                 x++;
  79.                             }
  80.                         }
  81.                         else
  82.                         {
  83.                             x++;
  84.                         }
  85.                     }
  86.                 }
  87.                 else
  88.                 {
  89.                     string[] splitDate;
  90.                     if (!dateStart.Equals("") && !dateEnd.Equals(""))
  91.                     {
  92.                         result = new string[10];
  93.                         x = 0;
  94.  
  95.                         for (i = 0; i < tempStr.Length; i++)
  96.                         {
  97.                             if (!tempStr[i].Equals(""))
  98.                             {
  99.                                 if (!tempStr[i].StartsWith(";"))
  100.                                 {
  101.                                     if (x < result.Length)
  102.                                     {
  103.                                         splitTempStr = tempStr[i].Split('\t');
  104.                                         cashierCardNo[x] = splitTempStr[0];
  105.                                         date[x] = splitTempStr[2];
  106.                                         status[x] = splitTempStr[4];
  107.  
  108.                                         splitDate = date[x].Split(' ');
  109.  
  110.                                         string[] str1 = splitDate[0].Split('/');
  111.                                         string temp = "";
  112.                                         temp = str1[0];
  113.                                         str1[0] = str1[1];
  114.                                         str1[1] = temp;
  115.                                         string dt = str1[0] + "/" + str1[1] + "/" + str1[2];
  116.                                         dateFromFile = Convert.ToDateTime(dt);
  117.  
  118.                                         if (DateTime.Parse(dateFromFile.ToShortDateString()) >= DateTime.Parse(dateStart) && DateTime.Parse(dateFromFile.ToShortDateString()) <= DateTime.Parse(dateEnd))
  119.                                         {
  120.                                             result[x] = tempStr[i];
  121.  
  122.                                             x++;
  123.  
  124.                                         }
  125.                                         else
  126.                                         {
  127.                                             continue;
  128.                                         }
  129.                                     }
  130.                                     else
  131.                                     {
  132.                                         break;
  133.                                     }
  134.                                 }
  135.                             }
  136.                         }
  137.  
  138.                         Array.Reverse(result);
  139.  
  140.                         for (i = 0; i < result.Length; i++)
  141.                         {
  142.                             if (result[i] != null)
  143.                             {
  144.                                 if (!result[i].Equals(""))
  145.                                 {
  146.                                     splitTempStr = result[i].Split('\t');
  147.  
  148.                                     cashierCardNo[i] = splitTempStr[0];
  149.                                     date[i] = splitTempStr[2];
  150.                                     status[i] = splitTempStr[4];
  151.  
  152.                                     dataGridView1["Column1", i].Value = cashierCardNo[i];
  153.                                     dataGridView1["Column2", i].Value = date[i];
  154.                                     dataGridView1["Column3", i].Value = status[i];
  155.                                 }
  156.                                 else
  157.                                 {
  158.                                     dataGridView1["Column1", i].Value = "";
  159.                                     dataGridView1["Column2", i].Value = "";
  160.                                     dataGridView1["Column3", i].Value = "";
  161.                                 }
  162.                             }
  163.                         }
  164.                     }
  165.                     else if (!previousRecords.Equals(""))
  166.                     {
  167.                         result = new string[tempStr.Length];
  168.                         x = 0;
  169.  
  170.                         for (i = 0; i < tempStr.Length; i++)
  171.                         {
  172.                             if (!tempStr[i].StartsWith(";"))
  173.                             {
  174.                                 result[x] = tempStr[i];
  175.                                 x++;
  176.                             }
  177.                         }
  178.  
  179.                         Array.Reverse(result);
  180.  
  181.                         x = 0;
  182.  
  183.                         for (i = 0; i < int.Parse(previousRecords); i++)
  184.                         {
  185.                             if (result[i] != null)
  186.                             {
  187.                                 if (!result[i].Equals(""))
  188.                                 {
  189.                                     splitTempStr = result[i].Split('\t');
  190.                                     cashierCardNo[i] = splitTempStr[0];
  191.                                     date[i] = splitTempStr[2];
  192.                                     status[i] = splitTempStr[4];
  193.  
  194.                                     dataGridView1["Column1", x].Value = cashierCardNo[i];
  195.                                     dataGridView1["Column2", x].Value = date[i];
  196.                                     dataGridView1["Column3", x].Value = status[i];
  197.  
  198.                                     x++;
  199.                                 }
  200.                             }
  201.                             else
  202.                             {
  203.  
  204.                             }
  205.  
  206.                         }
  207.  
  208.                     }
  209.                     else if (!previousDays.Equals(""))
  210.                     {
  211.  
  212.                     }
  213.                     else
  214.                     {
  215.  
  216.                     }
  217.  
  218.                     dataGridView1.Refresh();
  219.                 }
  220.             }
  221.  
  222.         }
additional question:
is it possible if I will pass the data from Form1 to Form2 without closing and reopen the Form2 just to reflect the changes in the datagrid?
May 7 '10 #3
tlhintoq
3,525 Expert 2GB
It helps the volunteers here tremendously when you post the actual code that is causing you problems. Not the entire application. Just the parts you have written that are causing the problem.

1 - Copy the code from Visual Studio



2 - In your question thread, click the [code] tags button. Its the one that looks like # symbol


Code tags have magically appeared in your post with the cursor right between them.

[code]|[/code]

Just paste and you should see this
[code] public void BrokenMethod()
{
// I don't understand why this doesn't work
int Yogi = "Bear";
}[/code]

Which will look like this
Expand|Select|Wrap|Line Numbers
  1.         public void BrokenMethod()
  2.         {
  3.             // I don't understand why this doesn't work
  4.             int Yogi = "Bear";
  5.         }
More on tags. They're cool. Check'em out.
May 7 '10 #4
tlhintoq
3,525 Expert 2GB
I appreciate your reply regarding this matter..
Can you give me the code that can help me solve the problem?
So in other words you didn't even try to do it yourself. You just posted your entire application thus far and hope someone else will code it for you. No. That's not going to happen.

If it's not important enough to you, to invest some of your own time to make an effort an *learn* ... then it has less importance to me. Let me be clear... It's not that I, or anyone here doesn't want to help you. We would love to help you. But I for one am not going to write it for you. Helping means you make your best effort, and show what you have done in an effort to accomplish and learn... and someone hear will help you clean up issues.

Can anybody send me code to [...]
The Bytes volunteers are not here to write your code for you. This is not a free homework service.
Bytes is very much a "Give me a fish I eat for a day. Teach me to fish I eat for a lifetime" kind of place. Just giving you the code doesn't help you learn near as effectively as good old-fashioned trial and error.

Please research your problem before posting your question.
A great place start your research is the MSDN library. This library is a bunch of articles and documentation provided by Microsoft about anything to do with .NET development. I recommend that you bookmark the resource for your future reference.

After you research, do some experimenting. Then if your trials aren't doing what you expect, post the code and relevant messages/errors and we'll see what we can do to point you in the right direction for making it work.


May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your look book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books.

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tip, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Building an application Part 1
Building an application part 2
May 7 '10 #5
mylixes
29
@tlhintoq
oh sorry bout that..
I just want to post the actual code i have for better understanding.

Please just help me out with this.

The program has no error.
I just want some help how to make it more easier and reliable.
May 7 '10 #6
tlhintoq
3,525 Expert 2GB
The program has no error.
Can you give me the code that can help me solve the problem?
Which is it? You have an error or you don't?

I just want some help how to make it more easier and reliable.
Title: How to change the data of datagridview when changes has made from another form
Again, which is it? You are trying to make it more reliable, or you want to know how to make form1 talk to form2?

Please define exactly what the nature of your problem is, what code you have written that is causing the problem, and what help you would like.
May 7 '10 #7
mylixes
29
@tlhintoq
I don't have any error in my code.
I just want to know how to reflect the changes I made from Form1 to Form2 datagridView without closing and re-opening the Form2.
May 7 '10 #8
tlhintoq
3,525 Expert 2GB
An example of communication from form1 to form2 has already been given.

The two tutorial links show this in even greater detail.
Build the two tutorial projects and you will learn.
May 7 '10 #9
mylixes
29
@tlhintoq
I read the link but in this code,

Expand|Select|Wrap|Line Numbers
  1.  private void RaiseFeedback(string p)
  2.          {
  3.              EventHandler<Saint.TextArgs> handler = Feedback;
  4.              if (handler != null)
  5.              {
  6.                  handler(null, new TextArgs(p));
  7.              }
  8.          }
where should i get the "Saint" (EventHandler<Saint.TextArgs> handler = Feedback;)?
May 7 '10 #10
mylixes
29
@mylixes
Oh sorry. I got it!
May 7 '10 #11
tlhintoq
3,525 Expert 2GB
where should i get the "Saint" (EventHandler<Saint.TextArgs> handler = Feedback;)?
It is an example of how you could write your own event arguments. It represents whatever namespace you create as part of your project.

Notice if you make a new project named "widget" that the namespace for the project is 'widget'. Everyhing you make is wiithin that namespace. If you make a new class named "gadget" then a static property in that class named "thingie" you would reference it as 'widget.gadget.thingie'

This same is done in that example. It represents a custom event argument called 'TextArgs' with a project namespace of 'Saint'.

In your real project you would have a different namespace and you would make a different custom event Argument that matches your need.

If you were passing information about a retail product as an example you might make an argument of ProductArgs that contained properties of SKU, price and quantity.

Take a look at other event arts in the .NET framework and you will see how they contain different properties. KeyPressArgs contain a KeyCode property used to get the actual key pressed. ExceptionArg have a Message property.

For your project you will have to decide what data should be grouped together logically to make classes and args that get passed around via events.

Notice that in the tutorial we make the TextArgs class. That is the same one as Saint.TextArgs. It would just be inside your project's namespace instead.

Thank you for pointing out the confusing reference in my example. I have taken it out of the example in the hopes it doesn't confuse others. You can remove it from your project as well, if you are building the tutorial.
May 7 '10 #12

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

Similar topics

3
by: RBirney | last post by:
When i load a form (frmcontract) in the program i write i can load a contract (cbocontract) as well as many other things, also in this form i can load another form to deal with expenditure...
4
by: YYZ | last post by:
Just wondering if there is a good way to do this that won't take me forever and a day. My form has many textboxes and comboboxes and radio buttons and checkboxes on it. I want to know if a user...
1
by: Felipe Roucher | last post by:
Hello, I 've a Windows Form and a "DataGridView" with the "DataSource" binding with a "BindingSource" and a "BindingNavigator", in the other hand in the same Form, there's multiples textbox's...
11
by: Kevin | last post by:
I've been searching forever for examples of saving data changes in a DataGridView. There's all kinds of examples, but none really show how to save changes. Someone please help me. I have a...
6
by: lucyh3h | last post by:
Hi, In one of my pages, I use javascript (AJAX) to populate one mulitple select field based on user's click event on another control. I noticed that when I navigate back to this page by clicking...
5
by: vovan | last post by:
I have set of controls (Textboxes, checkboxes etc) along with the Grid on Windows Form. I use BindingSource to populate both Grid and the set of Controls. User selects the record in the grid and...
0
by: mirandacascade | last post by:
Using Access 2003. For this application when one enters data and clicks a command button, code gets run that does the following: For 5 different combo box controls: - create a querydef -...
1
by: SePp | last post by:
Hi all. I want to refresh a Datagrid from another form. (in C# ) For Example I have a Datagrid in that I want to edit data. The user has the ability to press a button to change these data. A...
2
by: Hitman | last post by:
Hi Everyone, I have a little bit of problem with my database. I have a textbox ( the name of the textbox is location) on one form and 1 command button. I also made another form with 1 textbox (the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.