vb.net 如何检查数据行中是否存在具有给定名称的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1984893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:34:34  来源:igfitidea点击:

How to check if a column with a given name exists in a datarow

vb.netdatarow

提问by Dr. Rajesh Rolen

I want to insert a value from loop in datarow so before entering value in datarow, I want to check that a perticular column NAME exist in table or not.

Please tell me how can I check it. (vb.net preferred).

我想在数据行中从循环中插入一个值,因此在数据行中输入值之前,我想检查表中是否存在特定列 NAME。

请告诉我如何检查。(vb.net 首选)。

回答by Dr. Rajesh Rolen

I got the answer.and its working . its:

我得到了答案。它的工作原理。它的:

  If dr.Table.Columns.Contains("columnname") = True Then
   --your work---
  End If

回答by Anuraj

Try this

尝试这个

Dim dt As New DataTable
For Each dc As DataColumn In dt.Columns
    If dc.ColumnName = "" Then

    End If
Next

回答by Leidr Garcia

try:

尝试:

if dr.Table.Columns("nameColumn") == null then

 //....

回答by Quethzel Díaz

The shortest solution.

最短的解决方案。

 If dr.Table.Columns.Contains("columnname") Then
     'your code here
 End If

回答by Alexander Abakumov

Here is another way to find out if a column exists:

这是确定列是否存在的另一种方法:

If dataRow.Table.Columns("ColumnName") IsNot Nothing Then
    -- Your code if a column exists
End If

See this answerfor further reference when this approach might be handier than the Contains("ColumnName")one.

当这种方法可能比方法更方便时,请参阅此答案以获取进一步参考Contains("ColumnName")