Results 1 to 3 of 3

Thread: [RESOLVED] Pass Datarow between forms

  1. #1

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Resolved [RESOLVED] Pass Datarow between forms

    Here is the happy ending:

    I have form A. It has a DataviewGrid that gets filled with some data.

    I double-click one of the rows and Form B opens.

    Each textbox on Form B displays one cell of data from DataViewGrid row selected on Form A.

    I make my edits, and when I'm happy, I close Form B. the edits I made are now relfected on the DataViewGrid row that I had double-clicked on Form A.


    I'm using a datatable (ReportsData) to hold the data on Form A (MainFrm).
    The DataGridView (ReportDataView) uses ReportsData as its Datasource.

    To me, it seems the smart thing to do would be to pass the datarow corresponding to the row in the DataViewGrid to Form B, rather than a whole bunch of strings in a whole bunch of properties on Form B
    So, I do this on Form A. NOTE: The 3 is so I can test the functionality. I will need to figure out how to determine which row was clicked and put that number here. But that isn't at issue at the moment):
    Code:
            private void ReportDataView_RowHeaderMouseDoubleClick
                (object sender, DataGridViewCellMouseEventArgs e)
            {
             
                EditRecord EditForm = new EditRecord();             
    
                EditForm.DataToEdit = ReportsData.Rows[3];           
                
                EditForm.ShowDialog();
            }
    I set up a property in EditForm (aka Form B) to accept the datarow:

    Code:
            public DataRow DataToEdit
            {   
                
            get    { return dataToEdit; }  
            set    { dataToEdit = value; }  
                                   
            }
    Now I write the column data to my textboxes (Form B):
    Code:
            public EditRecord()
            {
                InitializeComponent();
    
                ReportNumber.Text = System.Convert.ToString(dataToEdit["ReportNumber"]);
            }
    But NOPE. The dataToEdit datarow is NULL. I know I need to declare the datarow as NEW somewhere, so the errorlist tells me. But where/how?
    The Form B datarow has to have the identical columns as Form A, I assumed, unwisely it seems, that passing it would automagiacally cause this to happen.
    Now, also, when I'm done with this edit, the datarow has to go back to Form A because Form B is going into the trash.

    So, any ideas how to pull this off?

    Thanks!

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Pass Datarow between forms

    Your function EditRecord is an initializer. It is called first. It is called when you make a NEW of that class.

    Inside that function, you're attempting to read a row from dataToEdit but that row doesn't exist yet.

    Instead, you should just change the declaration of the initializer to this:

    public EditRecord(DataRow Data){
    InitializeComponent();
    DataToEdit=Data;
    //Now you can access the row
    }
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: Pass Datarow between forms

    Yep. That does it!

    For the sake of the next person who needs this answered:
    Code:
            private void ApplyChanges_Click(object sender, EventArgs e)
            {
                dataToEdit["ReportNumber"] = ReportNumber.Text;
            }
    so Form A gets the data back. the Datarow was obviously passed by reference.

    Thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width