7 Replies - 11250 Views - Last Post: 13 April 2011 - 08:05 AM Rate Topic: -----

#1 newToC#   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-April 11

add data on datagridview from another form

Posted 13 April 2011 - 01:27 AM

string iName;
            string temp_num, temp_lname, temp_fname;

            frmCreateGroup fCreateGroup = (frmCreateGroup)Application.OpenForms["frmCreateGroup"];
            DataGridView dgv = (DataGridView)fCreateGroup.Controls["dgvNewMember"];

            foreach (object sName in lbNewContact.Items)
            {
                iName = (string)sName;
                temp_num = iName.Substring(0, iName.IndexOf(" "));
                temp_lname = iName.Substring(iName.IndexOf(" ") + 1, iName.IndexOf(",") - (iName.IndexOf(" ") + 1));
                temp_fname = iName.Substring(iName.IndexOf(",") + 2);

                string[] row = new string[] { temp_num, temp_fname, temp_lname };
                dgv.Rows.Add(row);
            }
            this.Close();


Hi all,

I paste the code I made. I encounter an error on this line "dgv.Rows.Add(row);". The error is "object reference not set to an instance of an object". My objective is to add data on the datagridview from another form when I close the current form.

Any help here. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: add data on datagridview from another form

#2 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: add data on datagridview from another form

Posted 13 April 2011 - 05:49 AM

More likely than not, this is null:

DataGridView dgv = (DataGridView)fCreateGroup.Controls["dgvNewMember"];


Put a breakpoint and check what value 'dgv' is holding on the line that break. If it's null it cannot add a row to nothing, see?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: add data on datagridview from another form

Posted 13 April 2011 - 06:08 AM

We have tutorials on cross-class/form communication.

Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes
Was This Post Helpful? 0
  • +
  • -

#4 newToC#   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-April 11

Re: add data on datagridview from another form

Posted 13 April 2011 - 06:21 AM

View PostSergio Tapia, on 13 April 2011 - 05:49 AM, said:

More likely than not, this is null:

DataGridView dgv = (DataGridView)fCreateGroup.Controls["dgvNewMember"];


Put a breakpoint and check what value 'dgv' is holding on the line that break. If it's null it cannot add a row to nothing, see?

Hi,

Thanks for the response. Yes you are right the value is null but I already used this kind of codes and it is working like this:
frmBlockList fBlocklist = (frmBlockList)Application.OpenForms["frmBlockList"];
            DataGridView dgv = (DataGridView)fBlocklist.Controls["dgView"];
            dgv.DataSource = record;
            dgv.DataMember = "block_list";
            dgv.Refresh();


any advise how can I solve this error?

Thanks again

This post has been edited by Curtis Rutland: 13 April 2011 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: add data on datagridview from another form

Posted 13 April 2011 - 06:51 AM

Are you sure the control in the other form is named, dgvNewMember?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: add data on datagridview from another form

Posted 13 April 2011 - 07:12 AM

Quote

frmBlockList fBlocklist = (frmBlockList)Application.OpenForms["frmBlockList"];
            DataGridView dgv = (DataGridView)fBlocklist.Controls["dgView"];
            dgv.DataSource = record;
            dgv.DataMember = "block_list";
            dgv.Refresh();



I will point out that a lot of work is being done to directly search for and access GUI controls on another form. It is a lot of work to make a fragile and tightly bound relationship. Both of which fly in direct opposition to good programming where every module should be ignorant of the others. This almost looks like an effort is being made to avoid object oriented principals and event-driven coding.

This code is all about the second form trying to reach into the first form and take the information it wants.

If anything changes anywhere the entire house of cards comes crashing down. If someone just changes the control's name... Or changes the access to private like it should be...

I would highly recommend abandoning this course. It is a horrible direction to take, and it is a programming pattern/habit that you don't want to get into.

Take a look at the tutorials as they show you how to do this sort of thing in a proper 'black box' scheme using events to drive your program.
Was This Post Helpful? 1
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: add data on datagridview from another form

Posted 13 April 2011 - 07:23 AM

Yeah, the third one he linked I wrote for this exact type of situation.
Was This Post Helpful? 1
  • +
  • -

#8 newToC#   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-April 11

Re: add data on datagridview from another form

Posted 13 April 2011 - 08:05 AM

View PostSergio Tapia, on 13 April 2011 - 06:51 AM, said:

Are you sure the control in the other form is named, dgvNewMember?

Yes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1