我在下面有一个示例gridview数据,不使用数据库。我在excel文件上使用数据绑定来加载gridview上的数据。
id sal
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
10 110
11 111
12 112
我想从gridview获得前10个值的结果。
我正在使用此代码,但它只返回前3个结果,并且以下第4到第10个重复,结果相同:
double NONSPRMaxV1 = 0;
double NONSPRMaxV2 = 0;
double NONSPRMaxV3 = 0;
double NONSPRMaxV4 = 0;
double NONSPRMaxV5 = 0;
double NONSPRMaxV6 = 0;
double NONSPRMaxV7 = 0;
double NONSPRMaxV8 = 0;
double NONSPRMaxV9 = 0;
double NONSPRMaxV10 = 0;
for (int i = 0; i < grvData.Rows.Count; i++)
{
if (grvData.Rows[i].Cells[23].Text == "OPEN" && grvData.Rows[i].Cells[28].Text == "NO")
{
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV1)
{
NONSPRMaxV1 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR4.Text = Convert.ToString(Math.Round(NONSPRMaxV1));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV2 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR4.Text))
{
NONSPRMaxV2 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR8.Text = Convert.ToString(Math.Round(NONSPRMaxV2));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV3 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR8.Text))
{
NONSPRMaxV3 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR12.Text = Convert.ToString(Math.Round(NONSPRMaxV3));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV4 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR12.Text))
{
NONSPRMaxV4 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR16.Text = Convert.ToString(Math.Round(NONSPRMaxV4));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV5 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR16.Text))
{
NONSPRMaxV5 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR20.Text = Convert.ToString(Math.Round(NONSPRMaxV5));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV6 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR20.Text))
{
NONSPRMaxV6 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR24.Text = Convert.ToString(Math.Round(NONSPRMaxV6, 0));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV7 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR24.Text))
{
NONSPRMaxV7 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR28.Text = Convert.ToString(Math.Round(NONSPRMaxV7, 0));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV8 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR28.Text))
{
NONSPRMaxV8 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR32.Text = Convert.ToString(Math.Round(NONSPRMaxV8, 0));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV9 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR32.Text))
{
NONSPRMaxV9 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR36.Text = Convert.ToString(Math.Round(NONSPRMaxV9, 0));
if (Convert.ToDouble(grvData.Rows[i].Cells[18].Text) > NONSPRMaxV10 && Convert.ToDouble(grvData.Rows[i].Cells[18].Text) < Convert.ToDouble(NONSPR36.Text))
{
NONSPRMaxV10 = Convert.ToDouble(grvData.Rows[i].Cells[18].Text);
}
NONSPR40.Text = Convert.ToString(Math.Round(NONSPRMaxV10, 0));
帮助!谢谢。
结果:
112
111个
110个
107个
107个
107个
107个
107个
107个
107
答案 0 :(得分:0)
对我来说,始终是将所有数据保存到List中的最佳解决方案,然后使用Linq进行订购。 我希望这有帮助!
List<double> nums = new List<double>();
for (int i = 0; i < grvData.Rows.Count; i++)
{
if (grvData.Rows[i].Cells[23].Text == "OPEN" && grvData.Rows[i].Cells[28].Text == "NO")
{
nums.Add(Convert.ToDouble(grvData.Rows[i].Cells[18].Text));
}
}
nums = nums.OrderByDescending(n => n).ToList();
NONSPR4.Text = nums[0].ToString();
NONSPR8.Text = nums[1].ToString();
NONSPR12.Text = nums[2].ToString();
NONSPR16.Text = nums[3].ToString();
NONSPR20.Text = nums[4].ToString();
NONSPR24.Text = nums[5].ToString();
NONSPR28.Text = nums[6].ToString();
NONSPR32.Text = nums[7].ToString();
NONSPR36.Text = nums[8].ToString();
NONSPR40.Text = nums[9].ToString();