精确的foreach循环访问ragridview行

时间:2014-09-06 06:59:34

标签: c# datatable radgridview

帮助!!!

我有一个radgridview,我想从radgrid获取数据到数据表中。在下面的代码中,我能够获得数据中的列名(第一个foreach循环)。但我根本不知道如何从radgrid获取行到数据表行。

代码

  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 CODE

            <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 。这是一个新手......

1 个答案:

答案 0 :(得分: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);
           }
       }