如何使用int ID为null的条件过滤DataTable

时间:2018-01-20 11:19:42

标签: 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;

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

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

但是我得到了空桌子。为什么呢?

1 个答案:

答案 0 :(得分:1)

我认为ID是数字。你不希望= null,你想要IS NULL,这是你在DataView(以及SQL)中检查空值的方法。

view.RowFilter = "ID IS NULL";