R
R
Ruslan Serg2019-12-12 06:43:01
WPF
Ruslan Serg, 2019-12-12 06:43:01

What to do so that after the selection from the database, the signs "_" do not disappear when entering data into the DataGrid?

After selecting data from a database (MySql) in which the names in the columns look something like this - "rabs_iin", "rabs_firstname", etc. from the insertion of this selection into the DataGrid, "_" disappears from the column name.
That is, we make a selection in getAllRabs, return the DataTable, and insert data from the DataTable.DefaultView into the grid.ItemsSource property, and "_" disappears in the column names.

public class Rabs
    {
        public static int getAllRabs()
        {
DB db = new DB();
            MySqlConnection conn = db.connection;

            DataTable table = new DataTable();

            MySqlDataAdapter adapter = new MySqlDataAdapter();

            MySqlCommand command = new MySqlCommand();
            command.CommandText = "SELECT * FROM companies";
            command.Connection = conn;

            adapter.SelectCommand = command;
            adapter.Fill(table);

            return table;
         }
}
/**************************/
 DataTable rabs = Rabs.getAllRabs();
//grid - элемент DataGrid
 this.grid.ItemsSource = rabs.DefaultView;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Prokhorov, 2019-12-13
@Athari

It's an ambush with ContentPresenter.RecognizesAccessKey on columns. You need to override the template:

<Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
  <Setter Property="ContentTemplate">
    <Setter.Value>
      <DataTemplate>
        <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center"/>
      </DataTemplate>
    </Setter.Value>
  </Setter>
</Style>

Well, or you can hack the replacement of "_" with "__" in the AutoGeneratingColumn event.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question