0

Im trying to change a Combobox value which is inside a Datagridview. The data that comes into the datagriview comes from a XML file which is placed in a Dataset. So basically I want to get the data set into the DataGridView. The second column should be a ComboBox. I read on the forum I should make a dictionary for the Combobox so I started with that but now how to continue I'm not really sure. Here is some code I have allready. These are the two classes I have in a dedicated class that I made. Now in my

public class ClassMAVLinkCmd
{
    Dictionary<int, MVLCmdMessage> MAVLnkCommand = new Dictionary<int, MVLCmdMessage>()
    {
        {16, new MVLCmdMessage {UAVCANMode = "UAV_CMD", MAVLinkCommand = "MAV_CMD_NAV_WAYPOINT", MAVLinkMsgId = 16} } ,
        {17, new MVLCmdMessage {UAVCANMode = "UAV_CMD", MAVLinkCommand = "MAV_CMD_NAV_LOITER_UNLIM", MAVLinkMsgId = 17} },
        {18, new MVLCmdMessage {UAVCANMode = "UAV_CMD", MAVLinkCommand = "MAV_CMD_NAV_LOITER_TURNS", MAVLinkMsgId = 18} },
    };
}

public class MVLCmdMessage
{
    public string UAVCANMode { get; set; }
    public string MAVLinkCommand { get; set; }
    public int MAVLinkMsgId { get; set; }

}

Now when I press the button to read the file and load it into the DataGridview I have this so far:

private void btnLoad_Click(object sender, EventArgs e)
{

    DataSet dsRoute = new DataSet();

    //Open the file dialog and select a XML file
    if (myOpen.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = myOpen.OpenFile()) != null)
            {
                using (myStream)                               
                dsRoute.ReadXml(myStream);
                strFilename = myOpen.SafeFileName.ToString();
                StatusRouteName.Text = strFilename;

                int i = 0;
                int length = dsRoute.Tables.Count;

                for (i = 0; i < length; i++)
                {
                    DataWayPoints.Rows.Add();
                    int row = WayPointList.Count;
                     DataWayPoints["Waypoint", i].Value = Convert.ToString(i + 1);
                    //DataWayPoints["MAVLnkCmd", i].Value = dsRoute.Tables[i].Rows[0]["MAVLnkCmd"].ToString();
                    //Need to investigate how to set a cmbBox in a DGV!!!!!!!!!!!!!!!!!!!!!!
                    DataWayPoints["P1", i].Value = dsRoute.Tables[i].Rows[0]["P1"].ToString();
                    DataWayPoints["P2", i].Value = dsRoute.Tables[i].Rows[0]["P2"].ToString();
                    DataWayPoints["P3", i].Value = dsRoute.Tables[i].Rows[0]["P3"].ToString();
                    DataWayPoints["P4", i].Value = dsRoute.Tables[i].Rows[0]["P4"].ToString();
                    DataWayPoints["Latitude", i].Value = dsRoute.Tables[i].Rows[0]["Latitude"].ToString();
                    DataWayPoints["Longitude", i].Value = dsRoute.Tables[i].Rows[0]["Longitude"].ToString();
                    DataWayPoints["Altitude", i].Value = dsRoute.Tables[i].Rows[0]["Altitude"].ToString();
                    DataWayPoints["DistanceToNextWpt", i].Value = dsRoute.Tables[i].Rows[0]["DistanceToNextWpt"].ToString();
                    DataWayPoints["Velocity", i].Value = dsRoute.Tables[i].Rows[0]["Velocity"].ToString();
                    }

                lblWayPointTotal.Text = "Waypoint total: " + length.ToString();
                CalcWptDistances();

            }
        }
        catch (Exception )
       {
            MessageBox.Show("Error could not open the selected file");
       }
        UpdateRouteList();
    }           

}

Could anybody explain me what would be the next step, thanks a lot in advance

ElectricRay81
  • 121
  • 11
  • Storing data in a dictionary makes sense if you later will use the key to find the values. Not sure what your data are; but you won't be able to even access the MAVLnkCommand as it is a private field. Did you perhaps see [this post](https://stackoverflow.com/questions/27201551/datagridview-comboboxcolumn-different-values-for-each-row/27202221#27202221)? Here the state names are key to List holding cities in that state which then get loaded inot the combocells' items.. – TaW Jun 15 '18 at 13:49
  • sorry for the late response i was travelling, I want the class MAVLnkCommand to be accesible thru the whole program so I should make it public static. I have seen the post that youre mentioning but I don't understand it completely. I tryy to do the same thing basically, only for me I only need thos commands like "MAV_CMD_NAV_WAYPOINT" available in the combobox. I showed only 3 commands but there are a lot just to save unnecessary code I wrote in the example just 3. And these commands should appear in the 2nd column. – ElectricRay81 Jun 16 '18 at 07:16
  • No need to show more data, but it will help to understand the data structure better. Do you really have it spread over many tables and want to create a row from each table? (The normal way is to have a table with rows and fields filling a dgv and its rows and cells..) – TaW Jun 16 '18 at 07:39
  • My table is the Dataset dsRoute, which is a XML file, so I have just one table. The second column of this table should be this Combobox. Because in the table I want to be able to adjust commands and values. I had those commands set up in the properties window of the DGV combo. But when I load the file I got errors. The saving works but I got errors when I load the XML file. This error was because of the combobox I found out during debugging. So than I searched here on the forum and found the same thread as you mentioned and thought lets try that. – ElectricRay81 Jun 16 '18 at 07:54
  • BTW there are 131 different command like MAV_CMD_NAV_WAYPOINT and all I would like to select with a combo. But in row 1 there will be a different command as in row 2 and row etc. I want to use this to make a flightplan for my drone. – ElectricRay81 Jun 16 '18 at 07:59
  • _I have just one table_ But why: `int length = dsRoute.Tables.Count; for (i = 0; i < length; i++)..` ?? A DataSet is can (but doesn't need to) hold several DataTables. - Also: _when I load the file I got errors_ : What errors do you get? – TaW Jun 16 '18 at 08:16
  • Eureka! I think I fixed it very easy. – ElectricRay81 Jun 16 '18 at 08:59

1 Answers1

0

If you create the items in the properties window and just use the correct code it works fine. I was thinking to complicated.

for (i = 0; i < length+1; i++)
{
    DataWayPoints.Rows.Add();

    DataWayPoints["Waypoint", i].Value = Convert.ToString(i + 1);
    DataGridViewComboBoxCell CommandCell = (DataGridViewComboBoxCell)(DataWayPoints["MAVLnkCmd", i]);
    CommandCell.Value = dsRoute.Tables[i].Rows[0]["MAVLnkCmd"].ToString();
    DataWayPoints["P1", i].Value = dsRoute.Tables[i].Rows[0]["P1"].ToString();
    DataWayPoints["P2", i].Value = dsRoute.Tables[i].Rows[0]["P2"].ToString();
    DataWayPoints["P3", i].Value = dsRoute.Tables[i].Rows[0]["P3"].ToString();
    DataWayPoints["P4", i].Value = dsRoute.Tables[i].Rows[0]["P4"].ToString();
    DataWayPoints["Latitude", i].Value = dsRoute.Tables[i].Rows[0]["Latitude"].ToString();
    DataWayPoints["Longitude", i].Value = dsRoute.Tables[i].Rows[0]["Longitude"].ToString();
    DataWayPoints["Altitude", i].Value = dsRoute.Tables[i].Rows[0]["Altitude"].ToString();
    DataWayPoints["DistanceToNextWpt", i].Value = dsRoute.Tables[i].Rows[0]["DistanceToNextWpt"].ToString();
    DataWayPoints["Velocity", i].Value = dsRoute.Tables[i].Rows[0]["Velocity"].ToString();
}

The trick is in using the DataGridViewComboBoxCell and asign this, I did it like this:

DataGridViewComboBoxCell CommandCell = (DataGridViewComboBoxCell)(DataWayPoints["MAVLnkCmd", i]);
CommandCell.Value = dsRoute.Tables[i].Rows[0]["MAVLnkCmd"].ToString();

Now when I read my file it shows me the correct values, I just got an other error, It tells me that my "input matrix is longer as the amount of columns in this table" Hmm weird cause I count everywhere 11 columns but this a different problem. I think my question has been solved.

ElectricRay81
  • 121
  • 11
  • If it works for you fine. Just a note: That other question was all about having __different items__ for each row and keeping the items lists updated to the right cities whever the state field changes.. – TaW Jun 16 '18 at 09:24
  • 1
    Yes I found out that this question was not the same a my issue. Than by reading the code over and over I finally saw what I had to do. Thanks for your help TaW! So now struggling to find my last error. it tells me that I have more than the table hmmmm. These Exceptions are somethimes very criptic lol – ElectricRay81 Jun 16 '18 at 09:31