Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,132 members, 7,825,552 topics. Date: Sunday, 12 May 2024 at 05:37 PM

Help On C# Multiple Form Application - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help On C# Multiple Form Application (2012 Views)

Help on C Programming / Pls I Need Ebook On C+ And Matlab / I Need Help On C++ Any Guru Here (2) (3) (4)

(1) (Reply) (Go Down)

Help On C# Multiple Form Application by enzo01(m): 3:21am On Jun 22, 2011
Hello fellow nairalanders i'm having this problem i cerated a form and filled it with datagridview, i now want to add new record to the datagridview by creating another form (Form2), that i'll use to type in this information. I did that successfully but the probelm am having is that after typing the information and want to save it it'll generate an error message. Please i need a little help here. I'll really apprici8 if i can get a respond.
Re: Help On C# Multiple Form Application by Gomez(m): 12:14pm On Jun 22, 2011
Hi
I will love to help you but you have to provide a more usefull information about the error.
What is the error message?
can you post the code snippet that is causing the error?

With these anybody can help out.

Cheers.

peter
Re: Help On C# Multiple Form Application by enzo01(m): 9:03pm On Jun 22, 2011
Yeah thanks i'll upload the source code. thanks
Re: Help On C# Multiple Form Application by enzo01(m): 6:39pm On Jun 23, 2011
Yeah these are the codes the first one is the form1 with the datagridview. then the second one is the form2, where i want to add new records so it can be added in form1 datagridview. the error am gettins is "Object reference is not set as an instansce of an object. thanks.

Re: Help On C# Multiple Form Application by Gomez(m): 8:16pm On Jun 23, 2011
Hey
I have looked at your code and for sure I can see a couple of places where you might need to rework.
Observation:

You are making a call to property of controls in form1 that has not be instantiated hence the error that is giving to you
"Object reference is not set as an instansce of an object"

string changegirdview = Form1.dview.Text;
Form1.dview.DataSource = dsl;

These are from Form2.
Given that Form1 is not a static class you have to device a means to get reference to that form, there are a couple of ways to do this.
I will make an assumption that you want the that is added in form2 to show in form1 without creating a new form1 object.


1.To do this you have to Create a public method that you want to call from form1
2. Add a second constructor to the form2 such that expects an object of type Form.
3. Pass the form1 object to form2 when you are creating the form2 instance.


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}



public void GetNewData(DataSet dsl)
{
//do ur binding here
//work with the dataset here
}

private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "";
}

private void button1_Click(object sender, EventArgs e)
{
//Here you are passing this current instance into the new form2 you are creating so that form2 will have access to the public properties and methods of form1
Form2 f = new Form2(this);
f.Show();
}
}
4. Work with form1 public methods and properties from form2
/////////////////////////////////////////////////////////////////////////////


public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

//Second constructor that accepts and instance of a form., this is what you use when creating an instance of the form2 from form1
public Form2(Form f)
{
InitializeComponent();
_form1 = f;
}
Form _form1;

private void button1_Click(object sender, EventArgs e)
{
//Here you cast the incoming object to the type of form1, so that u can use intelliscence and design time checking
Form1 frm = (Form1)_form1;

DataSet ds= new DataSet();
//You call the method from form1 and it will reflect in the form that has the gridvew.
frm.GetNewData(ds);
this.Hide();



}
}

Hope that helps!!!!!!!!!!
Re: Help On C# Multiple Form Application by enzo01(m): 12:25am On Jun 24, 2011
Thanx man i'm goin to try this method and giv u a feedback. Thanks once again.
Re: Help On C# Multiple Form Application by enzo01(m): 9:20am On Jun 24, 2011
Hello gomez! Am a bit confused on form2, the public Form2{ initializeComponents are of two types. Do i hv to put in the two public Form2 in the code? And where would i put in the code to add new records?. Thanks
Re: Help On C# Multiple Form Application by Gomez(m): 8:15pm On Jun 26, 2011
I will rewrite the code that you posted, I guess looking at it will help you.
Yeah you can have multiple constructors for a given class.
Yeah you can have the two of them.

Thanks
Re: Help On C# Multiple Form Application by Gomez(m): 8:05pm On Jun 28, 2011
I have uploaded a document containing the actual code and a re-factored version.
It sure will help you out.

Re: Help On C# Multiple Form Application by enzo01(m): 5:23am On Jun 30, 2011
Many tanx to you gomez. I'll be looking foward to that. Tanx once again.
Re: Help On C# Multiple Form Application by enzo01(m): 5:30am On Jun 30, 2011
Many tanx to you gomez. I'll be looking foward to that. Tanx once again.
Re: Help On C# Multiple Form Application by Gomez(m): 10:10am On Jul 01, 2011
I have done that already is in my previous post.
It has an attachment with the same name that you used for urs.

Thanks

(1) (Reply)

Best Programing Schools In Nigeria / Any Blockchain Developer... / Whats the Basic Things To Know Before Learning Coding

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 17
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.