2013-05-12 280 views
-2

我試圖從datagridview中計算最大值。 這裏是我的全部代碼:計算最小值和最大值C#

namespace WindowsFormsApplication2 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public class HRData 
    { 
     public int? HeartRate 
     { 
      get; 
      set; 
     } 
     public int? Speed 
     { 
      get; 
      set; 
     } 
     public int? Power 
     { 
      get; 
      set; 
     } 
     public int? Altitude 
     { 
      get; 
      set; 
     } 

     public override string ToString() 
     { 
      return String.Format("Heart rate={0}, Speed={1}, Power={2}, Altitude={3}", HeartRate, Speed, Power, Altitude); 
     } 
    } 





    public static class HRDataReader 
    { 
     static private int? ConvertValue(string[] values, int index) 
     { 
      if (index >= values.Length) 
       return null; 
      int value; 
      if (int.TryParse(values[index], out value)) 
       return value; 
      return null; 
     } 

     static public IList<HRData> Read(string fileName) 
     { 
      if (string.IsNullOrEmpty(fileName)) 
       throw new ArgumentNullException("fileName"); 
      using (StreamReader sr = new StreamReader(fileName)) 
      { 
       string line; 

       // First: Skip to the correct section. 
       while ((line = sr.ReadLine()) != null) 
        if (line == "[HRData]") 
         break; 

       // Now: Read the HRData 
       List<HRData> data = new List<HRData>(); 
       while ((line = sr.ReadLine()) != null) 
       { 
        if (line.StartsWith("[") && line.EndsWith("]")) 
         break; 
        line = line.Trim().Replace("\t", " "); // Remove all tabs. 
        while (line.Contains(" ")) // Remove all duplicate spaces. 
         line = line.Replace(" ", " "); 
        string[] values = line.Split(' '); // Split the line up. 
        data.Add(new HRData 
        { 
         HeartRate = ConvertValue(values, 0), 
         Speed = ConvertValue(values, 1), 
         Power = ConvertValue(values, 2), 
         Altitude = ConvertValue(values, 3) 
        }); 
       } 
       return data; 
      } 
     } 
    } 


    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click_1(object sender, EventArgs e) 
    { 
     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      dataGridView1.Rows.Clear(); 
      dataGridView1.Columns.Clear(); 

      IList<HRData> data = HRDataReader.Read(openFileDialog1.FileName); 

      dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "HeartRate", HeaderText = "Heart rate", DataPropertyName = "HeartRate" }); 
      dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Speed", HeaderText = "Speed", DataPropertyName = "Speed" }); 
      dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Power", HeaderText = "Power", DataPropertyName = "Power" }); 
      dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Altitude", HeaderText = "Altitude", DataPropertyName = "Altitude" }); 

      dataGridView1.DataSource = data; 

      int maxSpeed = Speed.Max(); 
      maxSpeed = maxSpeed/10; 
      string MaxSpeed = Convert.ToString(maxSpeed); 
      textBox1.Text = MaxSpeed;  
     } 
    } 
} 

}

我收到一條錯誤 '廉政MAXSPEED = Speed.Max();'

該錯誤消息說,

「名稱‘速度’並沒有在當前的背景下存在」我不知道我應該把什麼樣的價值在那裏代表我的一列。希望有人能夠幫助我解決這個問題。謝謝。

+3

這是一個很大的代碼。你能把它修剪一下嗎?它也可以幫助你自己確定問題。 – 2013-05-12 20:21:41

回答

2

速度不是一個類或一個對象,所以它不知道它是什麼。你必須使用一個函數和一個集合:

int maxSpeed = data.Max(x => x.Speed.Value); 

See the documentation on list max for more information and sample code.

+0

什麼是FindMax? – user2375636 2013-05-12 20:05:14

+0

更新了我的答案,以便與列表一起工作。 – Jason 2013-05-12 20:14:10

+0

出現錯誤:「不能隱式轉換類型'int?'到'int'。一個明確的轉換存在(你是否缺少一個演員?) 「 – user2375636 2013-05-12 20:18:11

0

你或許應該使用Math.Max Method (Double, Double)Math.Min Method (Double, Double)在標準的.NET C#庫包括

0

像這樣的事情可能?

int minAccountLevel = int.MaxValue; 
int maxAccountLevel = int.MinValue; 
foreach (DataGridViewRow dr in table.Rows) 
{ 
    int accountLevel = dr.Cells["Speed"]; 
    minAccountLevel = Math.Min(minAccountLevel, accountLevel); 
    maxAccountLevel = Math.Max(maxAccountLevel, accountLevel); 
} 

來源:https://stackoverflow.com/a/2442717/1908499

+0

它崩潰在線「foreach(dataRow dr在dataGridView1.Rows)」與消息無法投射類型'System.Windows.Forms.DataGridViewRow'類型'System.Data.DataRow'的對象。 – user2375636 2013-05-12 20:13:40

+0

對不起,我的錯。編輯! – 2013-05-12 20:24:14

相關問題