我创建了此代码,以用来自其他三个DataTable的数据填充三个DataGrid。第一个和最后一个工作完美。问题出在第二个DataGrid上。当我将其绑定到DT_Clientes
(第二个DataTable)并尝试编辑第二个DataGrid中的任何内容时,程序返回一个错误,指出Index必须大于0。
#region GRUPOS
SQL = "select descricao from grupos;";
command = new FbCommand(SQL, Connection);
Connection.Open();
FbDataAdapter adapter = new FbDataAdapter(command);
DT_Grupos = new DataTable();
adapter.Fill(DT_Grupos);
Connection.Close();
#region AJUSTANDO DATAGRID
Dgv_Rel01_Grupo.ItemsSource = DT_Grupos.DefaultView;
Dgv_Rel01_Grupo.HeadersVisibility = DataGridHeadersVisibility.None;
Dgv_Rel01_Grupo.Columns[0].Header = "Grupos";
Dgv_Rel01_Grupo.Columns[0].Width = 295;
#endregion
#endregion
#region CLIENTES
SQL = "select codigo, nome from cliente where situacao = 'Ativo' order by codigo asc;";
command = new FbCommand(SQL, Connection);
Connection.Open();
adapter = new FbDataAdapter(command);
DT_Clientes = new DataTable();
adapter.Fill(DT_Clientes);
Connection.Close();
#region AJUSTANDO DATAGRID
Dgv_Rel03_Clientes.ItemsSource = DT_Clientes.DefaultView;
Dgv_Rel03_Clientes.HeadersVisibility = DataGridHeadersVisibility.None;
Dgv_Rel03_Clientes.Columns[0].Width = 50;
Dgv_Rel03_Clientes.Columns[1].Width = 360;
#endregion
#endregion
#region FORNECEDORES
SQL = "select codigo, nome from fornecedor where situacao = 'Ativo' order by codigo asc;";
command = new FbCommand(SQL, Connection);
Connection.Open();
adapter = new FbDataAdapter(command);
DT_Fornecedores = new DataTable();
adapter.Fill(DT_Fornecedores);
Connection.Close();
#region AJUSTANDO DATAGRID
Dgv_Rel01_Fornecedor.ItemsSource = DT_Fornecedores.DefaultView;
Dgv_Rel01_Fornecedor.HeadersVisibility = DataGridHeadersVisibility.None;
Dgv_Rel01_Fornecedor.Columns[0].Header = "Código";
Dgv_Rel01_Fornecedor.Columns[1].Header = "Razão Social";
Dgv_Rel01_Fornecedor.Columns[0].Width = 50;
Dgv_Rel01_Fornecedor.Columns[1].Width = 280;
#endregion
#endregion
我测试了Datagrid.ItemsSource
的更改Datagrid.DataContext
,但是没有用。运行此代码后,我检查了DataGrid,并且其中没有任何列,甚至显示了我的DataTable中的数据。
答案 0 :(得分:0)
我认为问题出在这里:
SQL = "select codigo, nome from cliente where situacao = 'Ativo' order by codigo asc;";
此查询不返回任何数据,这意味着不存在值为situacao
的{{1}}。您应该尝试直接在数据库中运行此查询,看看它是否获取任何结果,也许不会。