C# 如何验证我的数据表是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11334451/
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
how to verify if my datatable is empty
提问by JUAN
hello again y have populate my datatable using 1 button an array, next button 2 is going to send my datatable to excel, so what a i do is this using C#, and asp.net: out side of the button y declare my datable so i can be use in both buttons
你好,你已经使用 1 个按钮和数组填充了我的数据表,下一个按钮 2 将我的数据表发送到 excel,所以 AI 所做的是使用 C# 和 asp.net:在按钮的外侧 y 声明我的数据表,所以我可以在两个按钮中使用
System.Data.DataTable _myDataTable =new System.Data.DataTable();
button 1 fill datatable: for this example lets say only 10 columns, caract=number of cells
按钮 1 填充数据表:对于这个例子,假设只有 10 列,caract=单元格数
for (int k=0; k < 10; k++)
{
_myDataTable.Columns.Add();
}
for (int j=0; j < 10; j++)
{
TableRow r = new TableRow();
System.Data.DataRow row=_myDataTable.NewRow();
for (int i = 0; i < caract+1; i++)
{
row[i]=(datar[j,i].ToString());
}
_myDataTable.Rows.Add(row);
Table1.Rows.Add(r);
}
now button 2 allows to the user if he wants to save the data from the datatable to an excel, but first i verifi if the datable is empty
现在按钮 2 允许用户是否要将数据表中的数据保存到 excel,但首先我验证数据表是否为空
if(_myDataTable !=null || _myDataTable.Rows.Count == 0)
{
string name="productos";
Label2.Text="it has data";
}
else{Label2.Text="NO data"; }
no i recibe the text that i have data yupi for me, but when i press button 2 to send the datatable to excel, it creats the document however is empty, so the next thing i would like to try is to verifi cell by cell if it has the data is supposed to have, but only have no idea how to extract the data from the datatable and to be display in a label. I appreciate any help
不,我记录了我有数据 yupi 的文本,但是当我按下按钮 2 将数据表发送到 excel 时,它会创建文档但是它是空的,所以接下来我想尝试的是逐个单元验证,如果它拥有应该拥有的数据,但不知道如何从数据表中提取数据并显示在标签中。我感谢任何帮助
回答by Oded
Your ifcondition looks wrong on a couple of points.
你的if情况在几点上看起来不对。
This is how I would code it:
这就是我编码的方式:
if(_myDataTable !=null && _myDataTable.Rows.Count > 0)
The above means - if _myDataTableis a valid object andit contains values.
上面的意思是 - if_myDataTable是一个有效的对象并且它包含值。
回答by Mehmet Ali Sert
JUAN you can define your DataTable in global scope. Then you can use it in both of your button event handlers.
JUAN 您可以在全局范围内定义您的 DataTable。然后您可以在两个按钮事件处理程序中使用它。
For example:
例如:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private DataTable _myDataTable;
public DataTable MyDataTable {
get
{
return _myDataTable;
}
set
{
_myDataTable = value;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int k=0; k < 10; k++)
{
MyDataTable.Columns.Add();
}
for (int j=0; j < 10; j++)
{
TableRow r = new TableRow();
System.Data.DataRow row = MyDataTable.NewRow();
for (int i = 0; i < caract+1; i++)
{
row[i]=(datar[j,i].ToString());
}
MyDataTable.Rows.Add(row);
Table1.Rows.Add(r);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if(MyDataTable !=null || MyDataTable.Rows.Count == 0)
{
string name="productos";
Label2.Text="it has data";
}
else
{
Label2.Text="NO data";
}
}
}
}
}
回答by YeinCM-Qva
I know this is an old question but there is an easy way to solve this right now as follows:
我知道这是一个老问题,但现在有一种简单的方法可以解决这个问题,如下所示:
if ((dataTableName?.Rows?.Count ?? 0) > 0)

