将文本框值与DataTable列进行比较
问题描述:
我试图通过将它与文本框中输入的值进行比较来删除列(Delta_sigma)中的一些数据,但是我得到错误无法对System.Double和System.String执行'<'操作。
I am trying to delete some data in column (Delta_sigma) by comparing it with value entered in textbox but i get error Cannot perform '<' operation on System.Double and System.String.
DataColumn Delta_sigma = table1.Columns.Add("Delta_Sigma", typeof(double));
Delta_sigma.Expression = "(Convert(Median,'System.Double') - Convert(Median_Refrence,'System.Double')) / (sigma_Refrence)";
double delta_sigma_critical = double.Parse(textBox1.Text);
DataRow[] rows4;
rows4 = table1.Select(" Delta_sigma < delta_sigma_critical ");
foreach (DataRow r in rows4)
r.Delete();
答
Quote:
无法对System.Double和System.String执行'<'操作。
Cannot perform '<' operation on System.Double and System.String.
非常清楚。你无法比较不同类型的数据。
因此,尝试比较相同类型的数据。
Quite clear. You cannot compare data of different types.
So, try to compare data with same type.
double delta_sigma_critical = double.Parse(textBox1.Text);
for(int i = table1.Rows.Count - 1; i >= 0; i--)
{
double d = Convert.ToDouble(table1.Rows[i]["Median"].ToString()) - Convert.ToDouble(table1.Rows[i]["Median_Refrence"].ToString());
d /= Convert.ToDouble(table1.Rows[i]["sigma_Refrence"].ToString());
if (d < delta_sigma_critical) table1.Rows.RemoveAt(i);
}