visual studio
visual studio

Reputation: 19

How to send Datagridview Values to Main form

I would like to send my Datagridview values to my Combobox in my mainform.

This is my code in my Datagridview form :

private void EquipmentList_Load(object sender, EventArgs e)
{
    DataTable.RowCount = 30;

    //Beam Description default value
    DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
}

Below code is what i've used but nothing happened :

        private void MainForm_Load(object sender, EventArgs e)
    {
        this.KeyPreview = true;


        EquipmentList ELform = new EquipmentList();

        //Beam Combo Box default values
        var BCBi1 = ELform.DataTable.Rows[0].Cells[1].Value.ToString();


        this.BeamCB.Items.Add(BCBi1);


    }

When i run it i'm getting this error:

error when running

An advance thank you for all those who will help me.

Upvotes: 1

Views: 46

Answers (2)

Freek W.
Freek W.

Reputation: 406

To the question "So is there any possible to load that values wihtout clicking/showing my equipmentlist form." Absolutely. Start by building a class for your data:

public class gridviewdata
{
   public string content { get; set; }
   public int row { get; set; }
   public int cell { get; set; }

   public gridviewdata(string content, int row, int cell)
   {
       this.content = content;
       this.row = row;
       this.cell = cell;
   }
}

add a mediator class:

public static class mediator
{
   public static List<gridviewdata> gridviewdatalist;
}

then add this code to your ELform load event:

private void EquipmentList_Load(object sender, EventArgs e)
{
    DataTable.RowCount = 30;
    //Beam Description default value
    DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
    mediator.gridviewdatalist.Add(new gridviewdata("8.000m x 0.400m x 0.550m", 0, 1);

}

then add a new method identifying your specific row to your MainForm:

private gridviewdata selectdata(int row, int cell)
{
   foreach(gridviewdata data in mediator.gridviewdatalist)
   {
      if(data.row == row && data.cell == cell)
       {
          return data;
       }
   }
   return null;
 }

And finally call that code in your MainForm:

private void MainForm_Load(object sender, EventArgs e)
{
    this.KeyPreview = true;

    //and call it there
    var BCBi1 = selectdata(0, 1);


    this.BeamCB.Items.Add(new Item(BCDi1, 1));    //Add value and index


}

Hope this helps.

Upvotes: 0

ASh
ASh

Reputation: 35680

DataTable.Rows[0].Cells[1].Value is assigned in EquipmentList form when it is loaded.

EquipmentList ELform = new EquipmentList(); - here form is created but not loaded (you need to invoke Show or ShowDialog for that).

I suggest to declare publicly accessible default value (and not rely on existance of DataTable object with 1 row, which is a low-level implementation detail)

public const string DefaultSize = "8.000m x 0.400m x 0.550m";

private void EquipmentList_Load(object sender, EventArgs e)
{
    DataTable.RowCount = 30;

    DataTable.Rows[0].Cells[1].Value = DefaultSize;
}

and then

private void MainForm_Load(object sender, EventArgs e)
{
    this.KeyPreview = true;

    this.BeamCB.Items.Add(EquipmentList.DefaultSize);
}

Upvotes: 1

Related Questions