0

我在向数据网格中的组合框添加文本时遇到问题。我有问题评论说这是错误的。如果有人能告诉我正确的方法,谢谢。

注意:所有其他信息都是正确的。

这是代码。

            XmlNode node = doc.SelectSingleNode("/MovieData");
            foreach (XmlNode movie in node.SelectNodes("Movie"))
            {
                if (movie != null)
                {
                    DataGridViewRow row = (DataGridViewRow)movieListDataViewBox.Rows[0].Clone();
                    row.Cells[0].Value = movie["Name"].InnerText;
                    row.Cells[1].Value = movie["Rating"].InnerText;
                    row.Cells[2].Value = movie["Disk"].InnerText;
                    row.Cells[3].Value = movie["LengthHr"].InnerText + " Hr. " + movie["LengthMin"].InnerText + " Min.";
                    // clear the combobox here ?
                    foreach(XmlNode type in movie["Type"])
                    {
                        row.Cells[4].Value = type.InnerText; // This is wrong here
                    }
                    row.Cells[5].Value = movie["SeriesType"].InnerText;
                    row.Cells[6].Value = movie["Location"].InnerText;
                    row.Cells[7].Value = movie["Owner"].InnerText;
                    row.Cells[8].Value = movie["Date"].InnerText;
                    row.Cells[9].Value = movie["Time"].InnerText;
                    movieListDataViewBox.Rows.Add(row);
                }
            }

编辑:这是 xml 文件的外观。

<Movie>
    <Name>Death Race</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Blu-Ray</Disk>
    <Owner>N/A</Owner>
    <Location>Basement</Location>
    <SeriesType>Movie Series</SeriesType>
    <LengthHr>1</LengthHr>
    <LengthMin>51</LengthMin>
    <Time>9 : 44 : 23 PM</Time>
    <Date>10/16/2013</Date>
  </Movie>
  <Movie>
    <Name>Death Race 2</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Combo</Disk>
    <Owner>N/A</Owner>
    <Location>Basement</Location>
    <SeriesType>Movie Series</SeriesType>
    <LengthHr>1</LengthHr>
    <LengthMin>41</LengthMin>
    <Time>9 : 52 : 34 PM</Time>
    <Date>10/16/2013</Date>
  </Movie>
4

1 回答 1

0

这是我使用的有效解决方案。

            XmlDocument doc = new XmlDocument();
            doc.Load(movieListXML);
            XmlNode node = doc.SelectSingleNode("/MovieData");
            foreach (XmlNode movie in node.SelectNodes("Movie"))
            {
                if (movie != null)
                {
                    DataGridViewRow row = (DataGridViewRow)movieListDataViewBox.Rows[0].Clone();
                    row.Cells[0].Value = movie["Name"].InnerText;
                    row.Cells[1].Value = movie["Rating"].InnerText;
                    row.Cells[2].Value = movie["Disk"].InnerText;
                    row.Cells[3].Value = movie["LengthHr"].InnerText + " Hr. " + movie["LengthMin"].InnerText + " Min.";
                    var cb = row.Cells[4] as DataGridViewComboBoxCell; // this is for a combo box item in datagrid
                    cb.Items.Clear(); // this is for a combo box item in datagrid
                    XmlNodeList nodeList = movie.ChildNodes;
                    string str = "";
                    foreach(XmlNode nl in nodeList)
                    {
                        if ((nl.Name == "Type"))
                        {
                            cb.Items.Add(nl.InnerText); // this is for a combo box 
                        }
                    }
                    row.Cells[4].Value = str;

                    row.Cells[5].Value = movie["SeriesType"].InnerText;
                    row.Cells[6].Value = movie["Location"].InnerText;
                    row.Cells[7].Value = movie["Owner"].InnerText;
                    row.Cells[8].Value = movie["Date"].InnerText;
                    row.Cells[9].Value = movie["Time"].InnerText;
                    row.Cells[10].Value = "Pictures";
                    movieListDataViewBox.Rows.Add(row);
                }
            }
于 2013-10-17T17:03:20.977 回答