2014-09-06 80 views
0

幫助!!!確切的foreach循環訪問ragridview行

我有一個radgridview,我想從radgrid獲取數據到數據表中。在下面的代碼中,我可以獲取數據庫中的列名(第一個foreach循環)。但我根本不知道如何從radgrid獲取行進入數據錶行。

CODE

DataTable dtRecords = new DataTable(); 
    foreach (Telerik.Windows.Controls.GridViewColumn col in OverallExposureGrid.Columns) 
    { 
     DataColumn colString = new DataColumn(col.UniqueName); 
     dtRecords.Columns.Add(colString); 
    } 
    foreach (**---- wat should i put here ---**)//loop thr each row in radgrid 
     { 
      DataRow dr = dtRecords.NewRow(); 
      foreach (Telerik.Windows.Controls.GridViewColumn col in OverallExposureGrid.Columns) //loops through each column in RadGrid 
       dr[col.UniqueName] = row[col.UniqueName].Text;//error 
       dtRecords.Rows.Add(dr); 
    } 

XAML代碼

  <telerik:RadGridView telerik:StyleManager.Theme="Expression_Dark" x:Name="OverallExposureGrid" ItemsSource="{Binding RegionSales, Mode=TwoWay}" ShowGroupPanel="False" IsSynchronizedWithCurrentItem="True" IsReadOnly="True" AutoGenerateColumns="False" Height="444"> 
         <telerik:RadGridView.ColumnGroups> 
          <telerik:GridViewColumnGroup Block.IsHyphenationEnabled="True" Name="commentGroup" Header="Notes" HeaderStyle="{StaticResource GroupStyle}"/> 
          <telerik:GridViewColumnGroup Block.IsHyphenationEnabled="True" Name="BlankGroup" Header="" /> 

         </telerik:RadGridView.ColumnGroups> 
         <telerik:RadGridView.Columns> 
          <telerik:GridViewDataColumn Header="Ticker" UniqueName="Ticker" TextBlock.FontWeight="ExtraBold" ColumnGroupName="BlankGroup" DataMemberBinding="{Binding CompnayTicker, Mode=TwoWay}" TextAlignment="Left"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" TextBlock.FontWeight="ExtraBold" IsVisible="{Binding CompanyNameVisibility, Mode=TwoWay}" Header="Company Name" DataMemberBinding="{Binding CompanyName, Mode=TwoWay}" TextAlignment="Left"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding DomicileCountryVisibility, Mode=TwoWay}" Header="Domicile Country" DataMemberBinding="{Binding DomicileCountry, Mode=TwoWay}" TextAlignment="Left"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding GICS1Visibility, Mode=TwoWay}" Header="GICS Sector Level 1" DataMemberBinding="{Binding GICSL1, Mode=TwoWay}" TextAlignment="Left"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding GICS2Visibility, Mode=TwoWay}" Header="GICS Sector Level 2" DataMemberBinding="{Binding GICSL2, Mode=TwoWay}" TextAlignment="Left"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding MarketCapVisibility, Mode=TwoWay}" Header="Mkt Cap" DataMemberBinding="{Binding MKTCap, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="True" Header="LRY" DataMemberBinding="{Binding LRY, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding EuropeVisibilityFullView}" UniqueName="Europe" Header="Europe" HeaderCellStyle="{StaticResource HeaderStyle}" DataMemberBinding="{Binding EuropeSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding UKVisibilityFullView, Mode=TwoWay}" UniqueName="UK" Header="UK" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.UK, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding EURxUKVisibilityFullView, Mode=TwoWay}" UniqueName="EURxUK" Header="EUR x UK" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.EURxUK, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding FranceVisibilityFullView, Mode=TwoWay}" UniqueName="France" Header="France" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.France, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding GermanyVisibilityFullView, Mode=TwoWay}" UniqueName="Germany" Header="Germany" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.Germany, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding GreeceVisibilityFullView, Mode=TwoWay}" UniqueName="Greece" Header="Greece" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.Greece, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding ItalyVisibilityFullView, Mode=TwoWay}" UniqueName="Italy" Header="Italy" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.Italy, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding IrelandVisibilityFullView, Mode=TwoWay}" UniqueName="Ireland" Header="Ireland" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.Ireland, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding PortugalVisibilityFullView, Mode=TwoWay}" UniqueName="Portugal" Header="Portugal" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.Portugal, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding SpainVisibilityFullView, Mode=TwoWay}" UniqueName="Spain" Header="Spain" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Europe.Spain, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding CEEMEAVisibilityFullView, Mode=TwoWay}" Header="CEEMEA" UniqueName="CEEMEA" HeaderCellStyle="{StaticResource HeaderStyle}" DataMemberBinding="{Binding CEEMEASales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding RussiaVisibilityFullView, Mode=TwoWay}" UniqueName="Russia" Header="Russia" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding CEEMEA.Russia, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding MideastVisibilityFullView, Mode=TwoWay}" UniqueName="Mid East" Header="Mid East" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding CEEMEA.MidEast, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding AfricaVisibilityFullView, Mode=TwoWay}" UniqueName="Africa" Header="Africa" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding CEEMEA.Africa, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding OtherCEEEMAVisibilityFullView, Mode=TwoWay}" UniqueName="Other CEEMEA" Header="Other CEEMEA" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding CEEMEA.OtherCEEMEA, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding NorthAmericaVisibilityFullView, Mode=TwoWay}" UniqueName="North America" Header="North America" HeaderCellStyle="{StaticResource HeaderStyle}" DataMemberBinding="{Binding NorthAmericaSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding LatamVisibilityFullView, Mode=TwoWay}" UniqueName="Latam" Header="Latam" HeaderCellStyle="{StaticResource HeaderStyle}" DataMemberBinding="{Binding LatamSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding MexicoVisibilityFullView, Mode=TwoWay}" UniqueName="Mexico" Header="Mexico" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Latam.Mexico, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding BrazilVisibilityFullView, Mode=TwoWay}" UniqueName="Brazil" Header="Brazil" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Latam.Brazil, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding OtherLatamVisibilityFullView, Mode=TwoWay}" UniqueName="Other Latam" Header="Other Latam" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Latam.OtherLatam, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding JapanVisibilityFullView, Mode=TwoWay}" UniqueName="Japan" Header="Japan" HeaderCellStyle="{StaticResource HeaderStyle}" DataMemberBinding="{Binding JapanSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding AuNzVisibilityFullView, Mode=TwoWay}" UniqueName="Au/NZ" Header="AU/NZ" HeaderCellStyle="{StaticResource HeaderStyle}" DataMemberBinding="{Binding AuNZSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding AsiaVisibilityFullView, Mode=TwoWay}" UniqueName="Asia" Header="Asia" HeaderCellStyle="{StaticResource HeaderStyle}" DataMemberBinding="{Binding AsiaSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding ChinaVisibilityFullView, Mode=TwoWay}" UniqueName="China" Header="China" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Asia.China, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding IndiaVisibilityFullView, Mode=TwoWay}" UniqueName="India" Header="India" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Asia.India, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding KoreaVisibilityFullView, Mode=TwoWay}" UniqueName="Korea" Header="Korea" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Asia.Korea, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding OtherAsiaVisibilityFullView, Mode=TwoWay}" UniqueName="Other Asia" Header="Other Asia" HeaderCellStyle="{StaticResource HeaderStyleCountries}" DataMemberBinding="{Binding Asia.OtherAsia, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 

          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding OtherVisibilityFullView, Mode=TwoWay}" UniqueName="Other" Header="Other" DataMemberBinding="{Binding OtherSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" IsVisible="{Binding MixedVisibilityFullView, Mode=TwoWay}" UniqueName="Mixed" Header="Mixed" DataMemberBinding="{Binding MixedSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <!--<telerik:GridViewDataColumn ColumnGroupName="BlankGroup" Header="Total" DataMemberBinding="{Binding TotalSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/>--> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" Header="Developed" DataMemberBinding="{Binding DevelopedSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn ColumnGroupName="BlankGroup" Header="Emerging" DataMemberBinding="{Binding EmergingSales, Converter={StaticResource formatCellConverter}, Mode=TwoWay}" TextAlignment="Right"/> 
          <telerik:GridViewDataColumn Header="Other" ColumnGroupName="commentGroup" DataMemberBinding="{Binding OtherNotes, Mode=TwoWay}" MaxWidth="180" TextWrapping="Wrap" Width="200"/> 

          <telerik:GridViewDataColumn Header="Mixed" ColumnGroupName="commentGroup" DataMemberBinding="{Binding MixedNotes, Mode=TwoWay}" MaxWidth="180" TextWrapping="Wrap" Width="200"/> 

         </telerik:RadGridView.Columns> 
        </telerik:RadGridView> 

我不希望從項目源的數據,該數據到數據表是從目前的數據形式radgrid控件。這是一個新手....

回答

0

將DataLoaded事件應用到您的RadGridView。您將在ItemsContaiserGenerator中獲得已排序的項目。

您可以通過使用

List<class> variable= new List<class>(); 

for (i = 0; i < count; i++) 
     { 
      (class)row = (class)sender.Items[i]; 
      { 
       variable.Add(row); 
      } 
     } 
得到它的項目