在Datagridview

时间:2018-06-15 09:46:49

标签: c# datagridview datagridcomboboxcolumn

我试图更改Datagridview中的Combobox值。进入datagriview的数据来自放置在数据集中的XML文件。所以基本上我想把数据集放到DataGridView中。第二列应该是ComboBox。我在论坛上看到我应该为Combobox制作一本字典,所以我从那开始,但现在如何继续我不太确定。这是我已经准备好的一些代码。这些是我在一个专门的课程中所拥有的两个课程。现在在我的

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; }

}

现在,当我按下按钮读取文件并将其加载到DataGridview中时,到目前为止我有这个:

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();
    }           

}

有人可以向我解释下一步会是什么,非常感谢提前

1 个答案:

答案 0 :(得分:0)

如果您在属性窗口中创建项目并只使用正确的代码,它可以正常工作。我想要复杂化。

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();
}

诀窍在于使用DataGridViewComboBoxCell并将其设置为这样,我这样做了:

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

现在,当我读取我的文件时,它显示了正确的值,我只是另外一个错误,它告诉我,我的输入矩阵比这个表中的列数更长&#34;嗯奇怪的原因我到处都是11列,但这是一个不同的问题。我想我的问题已经解决了。