Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

WPF Datagrid: Values in Column are not displayed

13.11.2018 (👁12337)

WPF Datagrid: Column is not displayed

 

 

Problem:

If you load a DataGrid with a data source in WPF and there is a column in the header of the tables. Point in the end,

then column will be displayed but the values ​​will not be loaded.

 

Solution:

Since data binding in WPF to a DataGrid Control is simplified internally, only the tables header or table column names can be checked to ensure that there are no points as endings, or that they are removed at runtime.

The same should apply to the usual special characters.

Alternatively, you can manually fill the DataGrid with items.

 

Subject:

WPF DataGrid, AutoGenerateColumns, DataContext

 

<DataGrid x:Name="ctlDataGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"

         AutoGenerateColumns="True"

         GridLinesVisibility="All"

         Background="#F0f0f0"

         ItemsSource="{Binding}" 

         SelectionMode="Single" 

         IsReadOnly="True" 

         MouseDoubleClick="ctlDataGrid_MouseDoubleClick"

          HeadersVisibility="All"                  

          Grid.Row="1" 

         />

 

 

Data loading with data binding

Load data into a DataGrid via data binding

 

A simple code example in C #

In WPF DataGrid

DataTable dataTable = new DataTable();

dataTable.Columns.Add("A");

dataTable.Columns.Add("B");

dataTable.Columns.Add("C");

 

DataRow row = dataTable.NewRow();

row[1] = 1;

row[2] = "2";

dataTable.Rows.Add(row);

 

 

_dataGrid.AutoGenerateColumns = true;

_dataGrid.DataContext = dataTable;

 

Example standard

Data from Excel without a dot as an example

 

The WPF Datagrid loads properly

 

Error Reproduction

With a . Dot in the header of column C.

 

As you can see, the actual data is not displayed

 

 

C # code example

Code Example for loading a WPF datagrid with data.

public async void load_DataGrid_with_Excel()

        {

            //----------------< load_DataGrid_with_Excel() >------------

            Excel.Range usedRange = _worksheet.UsedRange;

            

            //*fast Excel-Read: 

            //< create 2D Array >

            //*from excel with cell-content-object

            object[,] values = usedRange.Value2;

            _progressbar.Maximum = usedRange.Columns.Count;

 

            if (usedRange.Rows.Count > 0)

            {

                //< build datasouce >

                DataTable dataTable = new DataTable();

                //</ build datasouce >

 

                int nColumnsMax = usedRange.Columns.Count;

                

                //----< Read_Header >----

                for (int iColumn = 1; iColumn <= nColumnsMax; iColumn++)

                {

                    string sValue = Convert.ToString(values[1, iColumn]);

                    if (sValue == "" || sValue == null)

                    {

                        nColumnsMax = iColumn;

                        break;

                    }

                    if (iColumn>50)

                    {

                        nColumnsMax = iColumn;

                        break;

                    }

                    dataTable.Columns.Add(sValue);                    

                }

                //----</ Read_Header > ----

 

                //----< Read_DataRows >----

                

                for (int iRow = 2; iRow <= usedRange.Rows.Count; iRow++)

                {

                    if (iRow > 10000) break;

                    if (iRow % 50==0)

                    { 

                        _progressbar.Value = iRow;

                        lblStatus.Content = iRow + "/" + usedRange.Rows.Count;

                        await DoEvents();

 

                    }

                    //_progressbar.UpdateLayout();

                    //_progressbar.Dispatcher.Invoke(() => _progressbar.Value = iRow);

 

                    await Task.Run(() =>

                    {

                        //----< Row >----

                        //< add_Row >

                        DataRow row = dataTable.NewRow();

                        //</ add_Row >

 

                        

                        for (int iColumn = 1; iColumn <= nColumnsMax; iColumn++)

                        {

                            //----< read_cells_to_table >----

                            //< read >

                            string sValue = Convert.ToString(values[iRow, iColumn]);

                            if (iColumn == 1)

                            {

                                if (sValue == "" || sValue == null) { iRow=usedRange.Rows.Count; }

                            }

                            //</ read >

 

                            //< write >

                            row[iColumn -1] = sValue;

                            //</ write >

                            //--</ Transfer Cells > --

                            //----</ read_cells_to_table >----

                        }

                        dataTable.Rows.Add(row);

                        //----</ Row >----

                    });

                }

                //----</ Read_DataRows >----

 

                //< show Data >

                _dataGrid.AutoGenerateColumns = true;

                _dataGrid.DataContext = dataTable;

                

                //</ show Data >

            }

 

 

            //return true;

            //----------------</ load_DataGrid_with_Excel() >------------

        }