什么东西,自己以前写的一段代码,看了半天,大致了解。
大概是数据库中有一些数据,将这些字段安装某个自定义的数组顺序排列。用在什么情况,真的很难回想起来。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace TransLevel
{
static class TransLevel
{
/// <summary>
/// 在表中增加一列 keyFiled + "level" 设定keyFiled的级别
/// </summary>
/// <param name="keyValuelist"></param>
/// <param name="keylevellist"></param>
/// <param name="keyFiled"></param>
/// <param name="dt"></param>
static public void TransDt(double[] keyValuelist, string[] keylevellist, string keyFiled, DataTable dt)
{
if (!dt.Columns.Contains(keyFiled ))
{
return ;
}
if (!dt.Columns.Contains(keyFiled + "level"))
{
dt.Columns.Add(keyFiled + "level");
}
foreach (DataRow dr in dt.Rows)
{
try
{
double cuurd = double.Parse(dr[keyFiled].ToString());
int nat = FindIndex(keyValuelist, cuurd);
if (nat >= 0)
{
dr[keyFiled + "level"] = keylevellist[nat];
}
}
catch
{
dr[keyFiled + "level"] = "err";
}
}
}
/// <summary>
/// 在数组中查找当前值的顺序
/// </summary>
/// <param name="aa"></param>
/// <param name="currd"></param>
/// <returns></returns>
static int FindIndex(double[] aa, double currd)
{
int nlen = aa.Length;
if (nlen < 2)
{
return 0;
}
if (aa[0] < aa[1])//升序排列
{
for (int i = 0; i < nlen; i++)
{
if (currd <= aa[i])
{ return i; }
}
}
else
{
for (int i = 0; i < nlen; i++)
{
if (currd >= aa[i])
{ return i; }
}
}
return nlen - 1;
}
}
}
///
double[] keyValuelist = new double[] { 300, 200, 54 };
string[] keylevellist = new string[] { "a", "b", "c" };
string keyFiled = "applyfee";
DataTable dt = DBT.DAL.Query("select applyfee from f_applydetail where applyno='2008090390010014'").Tables[0];
TransLevel.TransLevel.TransDt(keyValuelist, keylevellist, keyFiled, dt); dataGridView1.DataSource = dt;