我在XAML代码中创建了这样的DataGrid:
<DataGrid x:Name="dgSalons" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="572" ItemsSource="{Binding}" VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="ИД" Binding="{Binding Path = id}" Width="*"/>
<DataGridTextColumn Header="Назив" Binding="{Binding Path = name}" Width="*"/>
<DataGridTextColumn Header="Адреса" Binding="{Binding Path = adress}" Width="*"/>
<DataGridTextColumn Header="Телефон" Binding="{Binding Path = email}" Width="*"/>
<DataGridTextColumn Header="Интернет адреса" Binding="{Binding Path = web_site}" Width="*"/>
<DataGridTextColumn Header="ПИБ" Binding="{Binding Path = tin}" Width="*"/>
<DataGridTextColumn Header="Матични Број" Binding="{Binding Path = uid}" Width="*"/>
<DataGridTextColumn Header="Жиро рачун" Binding="{Binding Path = bank_account}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
我尝试通过单击按钮“显示”(Прикажи)来填充来自mySql的数据:
private void btnShow_Click(object sender, RoutedEventArgs e)
{
string connection = "server=localhost;user id=root; password=root; database=pop-sf40-database";
string query = String.Format("SELECT * FROM salons");
MySqlConnection cn = new MySqlConnection(connection);
cn.Open();
MySqlCommand command = new MySqlCommand(query, cn);
MySqlDataAdapter da = new MySqlDataAdapter(command);
DataTable data = new DataTable();
da.Fill(data);
dgSalons.DataContext = data;
}
如何通过绑定仅填充一次细胞?