How to remove extra column Datagrid

后端 未结 7 415
一生所求
一生所求 2020-12-09 02:59

i have binded itemsource to Datatable for Datagrid . it shows extra columns how to remove it

My code :



        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 03:33

    If you want to:

    1. keep AutoGenerateColumns="True" AND
    2. keep the columns Width being automatically adjusted to the data from the DataTable rather then have the same width

    you need to set "1*" to the last column only. Please:

    1) Add the event handler to .xaml

    2) Add the following code to .xaml.cs

        private void dataGrid_AutoGeneratedColumns(object sender, EventArgs e)
        {
            int i = ((DataGrid)sender).Columns.Count;
            DataGridColumn column = ((DataGrid)sender).Columns[i - 1];
            column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
        }
    

提交回复
热议问题