简体   繁体   English

将DataTable绑定到DataGrid

[英]Bind DataTable to a DataGrid

I've created this code to fill three DataGrids with data from other three DataTable. 我已创建此代码以使用来自其他三个DataTable的数据填充三个DataGrids。 The first and the last work perfect. 第一个也是最后一个完美的工作。 The problem is with the second DataGrid. 问题出在第二个DataGrid上。 When I bind it to the DT_Clientes (the second DataTable) and try to edit anything from the second DataGrid, the program return an error that say that the Index must be more than 0. 当我将它绑定到DT_Clientes (第二个DataTable)并尝试从第二个DataGrid编辑任何内容时,程序返回一个错误,指出索引必须大于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

I tested change Datagrid.ItemsSource for Datagrid.DataContext but it didn't work. 我为Datagrid.DataContext测试了更改Datagrid.ItemsSource ,但它没有用。 I checked the DataGrid after running this code and there's no column in it, even showing the data from my DataTable. 我在运行此代码后检查了DataGrid,其中没有列,甚至显示来自我的DataTable的数据。

What I think is the problems is here: 我认为问题在于:

SQL = "select codigo, nome from cliente where situacao = 'Ativo' order by codigo asc;";

This query is not returning any data, which means there is no situacao who's value is Ativo . 这个查询没有返回任何数据,这意味着没有situacao的价值是Ativo You should try and run this query directly in your database and see if it fetch any result, probably it'll not. 您应该尝试直接在您的数据库中运行此查询,看看它是否获取任何结果,可能它不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM