2013-10-16 34 views
0

我在將數據添加到數據網格中的組合框時出現問題。 我有問題,評論說這是錯誤的。如果有人能告訴我正確的方式來做到這一點,謝謝。DataGridView組合框索引有誤

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

這是代碼。

  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> 
+0

您是否試圖將類型的值添加到組合框? Forth列是否爲組合列?我從代碼中無法確定。我發現代碼有缺陷,但是如果沒有這些信息,我不能幫你。 – WozzeC

+0

是的。是。抱歉,我應該解釋第四個是組合框單元。它有什麼缺陷? – deathismyfriend

+0

每個「爲每種類型」覆蓋單元格值。如果沒有其他人,我會在明天發佈修復程序。 – WozzeC

回答

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