Search code examples
postgresqldatatabledatagridnpgsqldynamic-columns

What is the best and fast way to get database data type of table field in c# for Postgres?


I have code:

MyDataAdapter.SelectCommand = MySelectCommand;
MyDataAdapter.Fill(MyDataTable);
MyDataGrid.ItemsSource = MyDataTable.DefaultView;
MyDataGrid.Columns.Add(MyDataGridTextColumn1);
MyDataGrid.Columns.Add(MyDataGridTextColumn2);
MyDataGrid.Columns.Add(MyDataGridTextColumn3);
...
MyDataGrid.Columns.Add(MyDataGridTextColumnXXX);

At next step I want to format added columns according to theirs database data type. But there is time limit for building of MyDataGrid. How to do it by the best and fast way?


Solution

  • MyDataTable.Columns[i].DataType.Name;
    

    or

    NpgsqlDataReader dr = MyDataAdapter.SelectCommand.ExecuteReader();
    dr.GetDataTypeName(i);
    

    where i - index of interesting column