【问题标题】:Fill DataGridView on condition from database根据数据库条件填充 DataGridView
【发布时间】:2014-06-24 20:50:58
【问题描述】:

我有 MySQL 数据库和 C# 中的 DataGridView,为了填充 DataGridView,我执行以下操作:

        schoolDataSet schl = new schoolDataSet();
        schoolDataSetTableAdapters.studentinfoTableAdapter adptr = new schoolDataSetTableAdapters.studentinfoTableAdapter();
        adptr.Fill(schl.studentinfo);
        dataGridView1.DataSource = schl.studentinfo.DefaultView;

和不需要的列,我从 DataGridView 属性中将它们设为visible = false,但如果我想指定要在 DataGridView 中填充哪些数据(行),例如应用 where 条件,我会遇到问题: fill data in DataGridView WHERE IsActive = 1 那么我仍然可以使用上面的代码进行一些修改还是我必须编写 SQL 查询并手动填充 DataGridView ?

【问题讨论】:

标签: c# mysql datagridview


【解决方案1】:

在搜索并尝试了大量代码后,我得到了以下最简单的代码:
在上面的代码中,只需注释掉最后一行 dataGridView1.DataSource = schl.studentinfo.DefaultView; 或简单地将其替换为以下代码

DataView dv = new DataView(schoolDataSet.studentinfo, "IsActive = 'false'", "id", DataViewRowState.CurrentRows);

新建一个DataView,根据IsActive列过滤false value,第三个参数id是基于排序,最后可以再写一行 dataGridView1.DataSource = dv; 这将告诉 DataGridView 从 DataView 加载数据。
希望能节省别人的时间。
非常感谢@Karthik Ganesan

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2021-06-04
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多