DataGridView不进入编辑模式

时间:2014-01-13 12:41:20

标签: c# winforms datagridview edit

我用这种方式填充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的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

在这种情况下,请勿使用DataReader。改为使用适配器:

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