[英]Check for column datatypes on a on a datagridview that is not bound to a datasource in C#
我有一个未绑定到数据库表的datagridview。 在我的程序中,我想检查仅数字列并将其存储在列表中。
List<string> ColumnNamesWithNumericTypes = new List<string>();
foreach (DataGridViewColumn dgvCol in dgv.Columns)
{
foreach (DataGridViewRow dgvRow in dgv.Rows)
{
// what is a good code to check for this condition?
if ( the column is fully of a numeric type(int))
{
ColumnNamesWithNumericTypes.Add(dgvCol.Name);
}
}
}
谢谢。
编辑:您可以针对每种数值数据类型(也称为值类型)测试每条记录,但是可能需要的操作数量取决于您想要的数值数据类型,因为-2.323是数字,但不是整数。 9,223,372,036,854,775,807都不是。 因此,这里不是使用每种Value类型进行测试,而是使用正则表达式的解决方案。
还:如果您可以使用LINQ做到这一点,我不会感到惊讶
我只测试了RegEx部分 ,但是您可以执行以下操作:
using System.Text.RegularExpressions;
接着..
List<string> ColumnNamesWithNumericTypes = new List<string> ( );
foreach (DataGridViewColumn dgvCol in dgv.Columns)
{
string columnname = dgvCol.Name;
bool isnumeric = false;
foreach (DataGridViewRow row in dgv.Rows)
{
if (Regex.IsMatch (row.Cells[columnname].Value , @"^[-]?([-.\d]+?)$"))
isnumeric = true;
else
{
isnumeric = false;
break;
}
}
if (isnumeric == true) ColumnNamesWithNumericTypes.Add (columnname);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.