Dean Larbi
Dean Larbi

Reputation: 109

Populating DataGridView from dataset created in a separate form

I have a dataset that is being created inside of a windows form we'll call form1. I want this dataset to populate a DataGridView that exists in a separate form we'll call form2.

I've tried setting up the datasource like so, but it doesn't work because the context is incorrect.

newShipmentGrid.DataSource = dataToWatch;

Specifically, the error I get is "The name newShipmentGrid does not exist in the current context". This is a Windows page form application.

Upvotes: 0

Views: 1096

Answers (2)

deadEddie
deadEddie

Reputation: 242

Here is a very simple implementation from some old code of mine for a Windows Forms Application. The constructor takes a DataSet from the calling procedure (another form) as the sole parameter, sets a DataSet member to the value of the parameter, and fills the data set with the method 'FillDataGrid'. Hope this helps you out.

public partial class HistForm : Form
{
    DataSet data;

    public HistForm(DataSet ds)
    {
        data = ds;
        InitializeComponent();
        FillDataGrid();            
    }

    private void FillDataGrid()
    {
        dataGridView1.DataSource = data.Tables[0];
    }
}

The calling method for this form:

private void btnHist_Click(object sender, EventArgs e)
    {
        DataSet tempDataSet = new DataSet();
        tempDataSet = userData;
        tempDataSet.Tables[0].Columns.RemoveAt(1); //remove columns 0 and 1 for display purposes
        tempDataSet.Tables[0].Columns.RemoveAt(0);
        HistForm hForm = new HistForm(tempDataSet);
        hForm.Show();
    }

Upvotes: 1

briskovich
briskovich

Reputation: 690

You would have to pass the variables into the second form and then populate the grid in the page load event.

Here is a good link to begin with:

http://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.100).aspx

Upvotes: 0

Related Questions