c# - 如何以int ID为null的条件过滤DataTable

标签 c# mysql

我有 gridview 和

GridViewName.DataSource = table;

表值:

string myConnection = ConfigurationManager.ConnectionStrings["vizitka"].ToString();
string Query_sel = MySqlString;
MySqlConnection conDataBase = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase_sel = new MySqlCommand(Query_sel, conDataBase);
try
{
    MySqlDataAdapter sda = new MySqlDataAdapter();
    sda.SelectCommand = cmdDataBase_sel;
    DataTable dbdataset = new DataTable();
    sda.Fill(dbdataset);
    BindingSource bSource = new BindingSource();
    bSource.DataSource = dbdataset;
    TableName.DataSource = bSource;
    sda.Update(dbdataset);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

我得到的值: Datatable

然后,我向表中添加新行:

DataRow newRow = table.NewRow();
table.Rows.Add(newRow);

并使用空单元格获取值(ID 也是空的,这对我有好处):

new Table

然后:

GridViewName.DataSource = table;

一切都很好,但是如果我想从表中删除这个新创建的行,我不能。我正在尝试再次过滤并绑定(bind) GridViewName。

DataView view = new DataView(table);
view.RowFilter = "CONVERT(id, System.String) = null or CONVERT(id, System.String) = ''";
Console.WriteLine(view);

但是我的 table 是空的。为什么?

最佳答案

我假设 ID 是数字。您不需要 = null,您需要 IS NULL,这就是在 DataView(以及 SQL)中检查 null 的方式。

view.RowFilter = "ID IS NULL";

关于c# - 如何以int ID为null的条件过滤DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48355496/

相关文章:

c# - 生产环境中transactionScope和ServiceBus的问题

sql - GROUP BY CASE 的问题

c# - 解析网页中的超链接

sql - Postgres 中的位运算符

mysql - 为什么 "10ddd"在我的 SQL 查询中等于 "10"?

python - 获取executemany后插入的主键

mysql - 如何获取下面提到的条件下的数据?

c# - 显示超过 24 小时的时间跨度的标准方法是什么?

c# - c# 中的 sql 查询中的索引(从零开始)

c# - NLog 控件到现有的 RichTextBox Windows 窗体