geeqy
geeqy

Reputation: 85

DataGridView.DataSource returns empty DataTable when called from other form

I have DataGridView named dGV.

Firstly I set its DataSourceusing an empty DataTable which has 300 rows and 50 columns so users can input their data in the DataGridView.

When I tried to call the dGV from other form after inputing the data it always returned that empty DataTable. Here's my code :

MainForm :

public MainForm()
{
   InitializeComponent();
   dGV.DataSource = GetTable();
}

public DataTable GetTable()
{
    DataTable table = new DataTable();
    for(int i = 1; i < 51; i++)
    {
        table.Columns.Add("V"+i);
    }
    for (int i = 1; i < 301; i++)
    {
        table.Rows.Add();
    }     
    return table;
}

public DataTable GetData()
{
    DataTable dt = dGV.DataSource as DataTable;
    return dt;
}

Form2 :

private void NDokButton_Click(object sender, EventArgs e)
{
    MainForm mf = new MainForm();
    DataTable dt = mf.GetData();
    var n = dt.Rows[0].ItemArray[0];
    String a = n.ToString();
    MessageBox.Show(a);
}

It shows an empty MessageBox.

Upvotes: 2

Views: 627

Answers (3)

geeqy
geeqy

Reputation: 85

I added new function in the Form2

 public DataTable dat = new DataTable();
 public DataTable GetFromMainForm(DataTable dt) {
            this.dat = dt;
            return dat;
        }

        private DataTable GetFromMainForm() {
            return dat;
        }


private void NDokButton_Click(object sender, EventArgs e)
        {
            DataTable dt = GetFromMainForm();
            var n = dt.Rows[0].ItemArray[0];
            String a = n.ToString();
            MessageBox.Show(a);

        }

In the MainFormi pass the DataTable to Form2

Form2 f = new Form2();
f.GetFromMainForm(dt);

Upvotes: 1

Paul Sasik
Paul Sasik

Reputation: 81429

That is because your second form creates an entirely new form, one in which the data grid is never populated.

Instead of creating a new format you need to pass a reference to the first for or its data grid to accomplish what you want.

Remember that new means new. Just because you create a new instance of an object does not mean that it will have anything to do with the first object (except for statics which would be a bad idea here).

Create a public property of type For in your 2nd form and set the value when you're creating the 2nd form, which I assume is happening in the first form. You will basically do something like this:

secondForm.RefToFirst = this;

Upvotes: 1

Zer0
Zer0

Reputation: 7354

You're creating a new instance of MainForm when you click the button. That will fill the DataGridView with a bunch of empty data. Then you grab an empty cell and display it in a text box.

Pass a reference to the existing MainForm to the second form.

private void NDokButton_Click(object sender, EventArgs e)
{
    //You're creating a new main form here.  You need an instance of the existing form.
    MainForm mf = new MainForm();
    DataTable dt = mf.GetData();
    var n = dt.Rows[0].ItemArray[0];
    String a = n.ToString();
    MessageBox.Show(a);
}

Upvotes: 2

Related Questions