Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two forms form1 and form2.

Form1 contains text boxes, radiobuttons,listboxes and edit toolstripbutton and form2 contains DataGridView.

My question is I want to display selected row data of DatagridView in respective text boxes as I clicked edit toolstrip button.

it is getting error as (object reference not set to an instance of an object)

this is form1 code
C#
---------------------
public static string Gender, Division, Genre, Event, Scoreeve, Relay,Note,Selectmode;
public static int Rounds, Scoreteam, timeprecison, Advancement;

public frmAddEditEvents()
       {
           if (Selectmode == "update")
               fillRecord();
           if (Selectmode == "insert")
           InitializeComponent();
       }

public void  fillRecord()
       {
           try
           {


               rdbMale.Text = Gender;
               cmbDivison.Text = Division;
               rdbRun.Text   = Genre;
               cmbEvent.Text = Event;
               chkScore.Text = Scoreeve;
               chkRelay.Text = Relay;
               nudRounds.Value = Rounds;
               nudScores.Value = Scoreteam;
               nudTimeprecision.Value = timeprecison;
               txtEventnote.Text = Note;
               numericUpDown1.Value = Advancement;
               btnSave.Text = "update";


           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());

           }

       }


this is my code form2
--------------------------
C#
private void tsbtnEdit_Click(object sender, EventArgs e)
       {



           if (grdEvents.SelectedRows.Count > 0)
           {


             frmAddEditEvents.Selectmode = "update";
             frmAddEditEvents.Gender = Convert.ToString(grdEvents.SelectedRows[0].Cells[1].Value);
             frmAddEditEvents.Division = Convert.ToString(grdEvents.SelectedRows[0].Cells[2].Value);
             frmAddEditEvents.Genre = Convert.ToString(grdEvents.SelectedRows[0].Cells[3].Value);
             frmAddEditEvents.Event = Convert.ToString(grdEvents.SelectedRows[0].Cells[4].Value);
             frmAddEditEvents.Scoreeve = Convert.ToString(grdEvents.SelectedRows[0].Cells[5].Value);
             frmAddEditEvents.Relay = Convert.ToString(grdEvents.SelectedRows[0].Cells[6].Value);
             frmAddEditEvents.Rounds = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[7].Value);
             frmAddEditEvents.Scoreteam = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[8].Value);
             frmAddEditEvents.timeprecison = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[9].Value);
             frmAddEditEvents.Note = Convert.ToString(grdEvents.SelectedRows[0].Cells[10].Value);
             frmAddEditEvents.Advancement = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[11].Value);

             frmAddEditEvents AEE = new frmAddEditEvents();
               AEE.ShowDialog();

           }
           else
           {
               MessageBox.Show("Please select any record to update");
           }
Posted
Updated 5-Jul-12 0:55am
v2

1 solution

i think this will help you
C#
if (grdEvents.SelectedRows.Count > 0)
            {

frmAddEditEvents AEE = new frmAddEditEvents(); //moved this line on the top
              frmAddEditEvents.Selectmode = "update";
              frmAddEditEvents.Gender = Convert.ToString(grdEvents.SelectedRows[0].Cells[1].Value);
              frmAddEditEvents.Division = Convert.ToString(grdEvents.SelectedRows[0].Cells[2].Value);
              frmAddEditEvents.Genre = Convert.ToString(grdEvents.SelectedRows[0].Cells[3].Value);
              frmAddEditEvents.Event = Convert.ToString(grdEvents.SelectedRows[0].Cells[4].Value);
              frmAddEditEvents.Scoreeve = Convert.ToString(grdEvents.SelectedRows[0].Cells[5].Value);
              frmAddEditEvents.Relay = Convert.ToString(grdEvents.SelectedRows[0].Cells[6].Value);
              frmAddEditEvents.Rounds = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[7].Value);
              frmAddEditEvents.Scoreteam = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[8].Value);
              frmAddEditEvents.timeprecison = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[9].Value);
              frmAddEditEvents.Note = Convert.ToString(grdEvents.SelectedRows[0].Cells[10].Value);
              frmAddEditEvents.Advancement = Convert.ToInt16(grdEvents.SelectedRows[0].Cells[11].Value);

              
                AEE.ShowDialog();

            }
            else
            {
                MessageBox.Show("Please select any record to update");
            }




Please go through these link this will help you

Passing Data Between Forms[^]

and

http://stackoverflow.com/questions/7886544/passing-a-value-from-one-form-to-another-form[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900