【问题标题】:DataGridView don't go into Edit ModeDataGridView 不进入编辑模式
【发布时间】:2014-01-13 12:41:20
【问题描述】:

我用这种方式填充 DataGridView:

DataTable dt = new DataTable();

using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MARKETConnectionString))
{
     conn.Open();

     String SQL = @"select INN, Idx, Adr, 'P' as Src from AdrP where Idx is null 
                    union all
                    select INN, Idx, Adr, 'K' as Src  from AdrK where Idx is null";
     SqlCommand cmd = new SqlCommand(SQL, conn);
     var RD = cmd.ExecuteReader();
     if (RD.HasRows) dt.Load(RD);
     dataGridView2.DataSource = dt;
}

但 DataGridView 不会通过双击进入编辑模式。如果我在 SQL 查询中删除'Union All',则双击可以正常工作(在编辑模式下切换 DataGridView)。但我需要从两个带有 Union 的表中进行选择。

我尝试在 DataGridView DoubleClick 事件中调用 dataGridView2.BeginEdit(true)。但这无济于事。

从两个表中选择数据源来编辑 DataGridView 的正确方法是什么?

【问题讨论】:

    标签: c# winforms datagridview edit


    【解决方案1】:

    在这种情况下不要使用 DataReader。改用适配器:

    using (SqlDataAdapter adp = new SqlDataAdapter(SQL, conn)) {
      adp.Fill(dt);
    }
    

    【讨论】:

    • 谢谢!它工作正常。但是为什么 DataReader 在这种情况下不能正常工作呢?
    • @gouph 读者知道“联合”不是可编辑的表,因此它在内部将其标记为只读(或类似的东西)。适配器填充方法没有做出这样的假设。当然,要保存数据,还得自己提供自定义机制,因为DataTable不知道数据是从哪里来的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多