Grohl Grohl - 1 year ago 53
C# Question

Access data in datagridview from a class

I've read a lot of topics on this issue but I'm not finding an answer. I'm fairly new to this so please bear with me.

I'm trying to pass values from datagridview to a list. And then in a new class I want to make som methods accessing that list. Trouble is that when I pass the datagridview it returns it without content and values which means I can't do anything with it.

The code under ////TESTING//// works like I want. I create an instance of the specified list and it's counting the amount of rows properly, see screenshot.

public List<vertEl> getVertList = new List<vertEl>();

//Opens the file dialog and assigns file path to Textbox
OpenFileDialog browseButton = new OpenFileDialog();
private void browse_Click(object sender, EventArgs e)
{

browseButton.Filter = "Excel Files |*.xlsx;*.xls;*.xlsm;*.csv";
if (browseButton.ShowDialog() == DialogResult.OK)
{
//SOME CODE TO GET DATA FROM EXCEL AND SOME METHODS TO CALCULATE
//VALUES TO PASS TO THE TAB VERTIKALELEMENTER TAB IN MY DATAGRIDVIEW
//VERTIKALELEMENTER IS vertElementerDgv IN MY CODE

////TESTING////
GetVertElementasList TEST = new GetVertElementasList();
getVertList = TEST.vertList(vertElementerDgv);
MessageBox.Show(getVertList.Count.ToString());


}
else return;
}


I now want to do this in a seperate class and call a method from that class to do the same but when I try that with code underneath I do not get the same count as when I have the code in form1 (public partial class BridgeGeometry). It return count of 0. The method foo() is assigned to the button 1 in the form.

class GetKoord
{
public GetVertElementasList getList = new GetVertElementasList();

BridgGeometry obj = new BridgGeometry();

public void foo()
{
var TEST = getList.vertList(obj.vertElementerDgv);
//var TEST = obj.getVertList;
MessageBox.Show(TEST.Count.ToString());

}

}


I also tried to get the values directly from the datagridview but there's nothing in it when I access it from a class which is not the form1/BridgeGeometry class.

Form - screenshot

Answer Source

Based on the link I managed to get this working. Probably not the best solution, but a working one. I tried to wrap my head around databinding, listbinding etc. but since the class with the input values are a messy one I gave that up for now. The datagriview input values are a little from lists and some from other datagridview.

MSDN-forum: Accessing Form1 controls from a different class

Explanations are given in the link so I'll just provide how I did it in my program.

If my GetKoord class are like this:

    public class GetKoord
    {
    private BridgGeometry bridgeGeometry;

    public GetKoord(BridgGeometry form1)
    {
        bridgeGeometry = form1;
    }

    public List<vertElementerParam> getListvertElementer(List<vertElementerParam> theList)
    {
        //var vertElementerDgv = bridgeGeometry.vertElementerDgv;
        GetVertElementasList getVertElementasList = new GetVertElementasList();
        List<vertElementerParam> MyDgvListList = new List<vertElementerParam>();
        MyDgvListList = getVertElementasList.vertList(bridgeGeometry.vertElementerDgv);

        //MessageBox.Show(MyDgvListList.Count.ToString());

        theList = MyDgvListList;
        return theList;
    }

    }

then I can get the list in Button1_Click like this, check the screenshot in the first post:

    public List<vertElementerParam> getVertList = new List<vertElementerParam>();

    private void button1_Click(object sender, EventArgs e)
    {
    GetKoord getKoord = new GetKoord(this);
    List<vertElementerParam> testList = new List<vertElementerParam>();

    testList = getKoord.getListvertElementer(getVertList);
    MessageBox.Show(testList.Count.ToString());

    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download