简体   繁体   English

我想检查 datagridview 列是否有数据或空/空

[英]I want to check if datagridview column is having data or null/empty

I want to check if my datagridview value stored in column[2] is empty or null.我想检查存储在列 [2] 中的 datagridview 值是否为空或 null。

How can I access datagridview column?如何访问 datagridview 列?

string[] row = new string[]
{ 
  Convert.ToString(dataReader[0]),
  Convert.ToString(dataReader[1]),
  Convert.ToString(dataReader[2]),
  Convert.ToString(dataReader[3])
};
dataGridView1.Rows.Add(row);

whit this code you can access column from your DataGridView使用此代码,您可以从 DataGridView 访问列

foreach (DataGridViewColumn col in dataGridView1.Columns)

but for checking null values you should check the cells not the column, so just add another for to count your rows and check every cells of your specific column但是要检查 null 值,您应该检查单元格而不是列,因此只需添加另一个 for 来计算行数并检查特定列的每个单元格

your code will be like this:你的代码将是这样的:

for (int colCounter=0 ; colCounter<dataGridView1.Columns.Count; colCounter++)
 for(int rowCounter=0 ; rowCounter<dataGridView1.Rows.Count ; rowCounter++)
   if(DataGridView1.Rows[rowCounter].Cells[colCounter].Value.tostring()==null)
    //your code

I think you need to use a loop to add those data in your datagridview like this:我认为您需要使用循环在datagridview中添加这些数据,如下所示:

foreach(var data in row)
{
    dataGridView1.Rows.Add(data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM