none
WPF中datagrid绑定了datatable了,第一次能显示,第二次就没了,是什么原因? RRS feed

  • 问题

  • public partial class PowerSupplyCheck : Window
        {
            public PowerSupplyCheck()
            {
                InitializeComponent();
               
    
                DataGrid.ItemsSource = MyCommands.dt.DefaultView;
    
                
    
                DataGrid.GridLinesVisibility = DataGridGridLinesVisibility.All;
    
               
    
            }
        }

    第二次运行程序debug的时候运行到

    DataGrid.GridLinesVisibility = DataGridGridLinesVisibility.All
    dt里面是有数据的,但是最后就是不显示,请问是什么原因呢?

    2021年3月28日 16:02

全部回复

  • 你好,

    我在Visual Studio2019(.NET Framework 4.7.2)中创建了项目,将DataTable绑定到DataGrid,它可以正常工作。
    您可以提供XAML代码和MyCommands.dt的相关代码让我重现您的问题并进行分析吗? 

    Best regards
    Hui Liu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    <audio controls="controls" style="display:none;"></audio>
    2021年3月30日 7:42
  • 你好,

    我在Visual Studio2019(.NET Framework 4.7.2)中创建了项目,将DataTable绑定到DataGrid,它可以正常工作。
    您可以提供XAML代码和MyCommands.dt的相关代码让我重现您的问题并进行分析吗? 

    Best regards
    Hui Liu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    <audio controls="controls" style="display:none;"></audio>
    <Window x:Class="ChangeTextStyle.PowerSupplyCheck"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="PowerSupplyCheck" Height="389" Width="491">
        <Grid>
            <DataGrid Name="DataGrid" Margin="0"/>
    
        </Grid>
    </Window>

    xaml代码

    public partial class PowerSupplyCheck : Window
        {
            public PowerSupplyCheck()
            {
                InitializeComponent();
    
                //DataGrid.ItemsSource = null;
    
                DataGrid.Items.Refresh();
                DataGrid.AutoGenerateColumns = false;
                DataGrid.AutoGenerateColumns = true;
                DataGrid.UpdateLayout();
    
                DataGrid.ItemsSource = MyCommands.dt.DefaultView;
    
                
    
    
                DataGrid.GridLinesVisibility = DataGridGridLinesVisibility.All;
    
               
    
            }
        }
    public static System.Data.DataTable dt = new System.Data.DataTable();
    
     //创建一个空的datatable表
                for (int i = 0; i < column_count+1; i++)
                {
                    dt.Columns.Add("column" + i, typeof(string));
                }
    
                for (int j = 0; j < row_count+1; j++)
                {
                    DataRow row = dt.NewRow();
                    for (int i = 0; i < column_count; i++)
                    {
                        row["column" + i] = "";
                    }
                    dt.Rows.Add(row);
                }
    
                //把list_rD的数据放到dt表
                for (int i = 0; i < list_rD.Count; i++)
                {
                    dt.Rows[list_rD[i].id_Row][list_rD[i].id_Column] = list_rD[i].Text;
                }

    datatable的列数是动态生成的,然后第一次动态生成的时候,列名和数据都是有的,第二次动态生成的时候,只有动态生成的列名,没有显示数据了

    我看了下其他的文章,说是datatable没实现INotifyPropertyChanged 接口,所以没有生成数据,需要在datagrid绑定每个列的数据,才能更新,是这样吗?

    有没有其他解决方案呢?


    2021年4月8日 3:59