将DGV中的行复制到列表中

时间:2012-10-31 18:43:40

标签: c# .net winforms list datagridview

这样我就可以从datagridview中作为列表进行访问。我在这里遇到的问题..让我们说如果我在DGV中有6 rows,当它更新到列表时会显示36rows。 我该如何解决这个问题?

for (int i = 0; i < testDsrcConfigs .Count; i++ )
            {
                Console.WriteLine(testDsrcConfigs [i]);
            }

从for循环中,我可以看到6行。 是否可以使用for loop ??我怎样才能适应来回访问会员?请帮帮我..!

List<SdrcConfig> testDsrcConfigs = new List<SdrcConfig>();


foreach (GridViewRowInfo dr in RadGridView.Rows)
    {
        //Create a TrafficLane list 
        List<TrafficLane> covTrafLane = new List<TrafficLane>();

        if (dr.Cells["bct0"].Value != "XXXX")
            {
                covTrafLane.Add(new TrafficLane(dr.Cells["bct0"].Value.ToString()));
            }
        if (dr.Cells["bct1"].Value != "XXXX")
            {
                covTrafLane.Add(new TrafficLane(dr.Cells["bct1"].Value.ToString()));
            }
        if (dr.Cells["bct2"].Value != "XXXX")
            {
                covTrafLane.Add(new TrafficLane(dr.Cells["bct2"].Value.ToString()));
            }
        if (dr.Cells["bct3"].Value != "XXXX")
            {
                covTrafLane.Add(new TrafficLane(dr.Cells["bct3"].Value.ToString()));
            }

            //Create RseDevicePosition
            DevicePosition devPos;
            int ctx;
            Int32.TryParse(dr.Cells["bx"].Value.ToString(), out ctx);
            int cty;
            Int32.TryParse(dr.Cells["by"].Value.ToString(), out cty);
            int ctz;
            Int32.TryParse(dr.Cells["bz"].Value.ToString(), out ctz);
            int bazu;
            Int32.TryParse(dr.Cells["bazim"].Value.ToString(), out bazu);
            int bele;
            Int32.TryParse(dr.Cells["belev"].Value.ToString(), out bele);
            int bti;
            Int32.TryParse(dr.Cells["btilt"].Value.ToString(), out bti);


            ushort devnum = UInt16.Parse(dr.Cells["bdevnum"].Value.ToString());

            devPos = new DevicePosition(
                new ValueWithUnit<int>(ctx, "mm"),
                new ValueWithUnit<int>(cty, "mm"),
                new ValueWithUnit<int>(ctz, "mm"),
                new ValueWithUnit<int>(bazu, "tenthOfDegree"),
                new ValueWithUnit<int>(bele, "tenthOfDegree"),
                new ValueWithUnit<int>(bti, "tenthOfDegree"));


            ((MyConfig.
            .ObuTransactionSystemConfig as DsrcTransactionSystemConfig).
                 DsrcSystemConfig as MultiLaneDsrcSystemConfig).DsrcConfigs.ForEach(
                     dsrcBeacon => testDsrcConfigs.Add(
                         new SdrcConfig(
                             dr.Cells["bid"].Value.ToString(), 
                             dr.Cells["bdesc"].Value.ToString(),        
                             covTrafLane,
                             devPos, devnum,
                             dsrcBeacon.Settings)));
    }

1 个答案:

答案 0 :(得分:1)

进行以下更改:

((MyConfig.
        .ObuTransactionSystemConfig as DsrcTransactionSystemConfig).
             DsrcSystemConfig as MultiLaneDsrcSystemConfig).DsrcConfigs.ForEach(
                 dsrcBeacon => testDsrcConfigs.Add(
                     new SdrcConfig(
                         dr.Cells["bid"].Value.ToString(), 
                         dr.Cells["bdesc"].Value.ToString(),        
                         covTrafLane,
                         devPos, devnum,
                         dsrcBeacon.Settings)));

testDsrcConfigs.Add(
                     new SdrcConfig(
                         dr.Cells["bid"].Value.ToString(), 
                         dr.Cells["bdesc"].Value.ToString(),        
                         covTrafLane,
                         devPos, devnum,
                         ((MyConfig.
    .ObuTransactionSystemConfig as DsrcTransactionSystemConfig).
         DsrcSystemConfig as MultiLaneDsrcSystemConfig).DsrcConfigs[0].Settings);