How to remove extra column Datagrid

后端 未结 7 383
一生所求
一生所求 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:21

    Well since its unused space, another way around will be use a weighted width rather than fixed one :

    <DataGrid x:Name="dataGrid" IsReadOnly="True" AutoGenerateColumns="False">
        <DataGrid.Columns>
           <DataGridTextColumn Binding="{Binding OrderId}" Header="OrderId" Width="1*"/>
           <DataGridTextColumn Binding="{Binding ProductId}" Header="ProductId" Width="1*"/>
           <DataGridTextColumn Binding="{Binding UnitPrice}" Header="UnitPrice" Width="1*"/>
           <DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity" Width="1*"/>
    

    0 讨论(0)
  • 2020-12-09 03:23

    I was fighting over this for some times now, the Width="\*" solved my problem while datagrid ColumnWidth="*" not working in my case.

    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
  • 2020-12-09 03:33

    Add the attribute ColumnWidth="*" in the DataGrid object.

    <Grid>
        <DataGrid x:Name="datagridname" Margin="10" ItemsSource="{Binding}" IsReadOnly="True" ColumnWidth="*"/>
    </Grid>
    
    0 讨论(0)
  • 2020-12-09 03:34

    One way to avoid is to set AutoGenerateColumns to False (XAML only approach).

    Provide your own collection of columns and set width for last column to *.

    <DataGrid x:Name="dataGrid" IsReadOnly="True" AutoGenerateColumns="False">
        <DataGrid.Columns>
           <DataGridTextColumn Binding="{Binding OrderId}" Header="OrderId"/>
           <DataGridTextColumn Binding="{Binding ProductId}" Header="ProductId"/>
           <DataGridTextColumn Binding="{Binding UnitPrice}" Header="UnitPrice"/>
           <DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity"/>
           <DataGridTextColumn Binding="{Binding Discount}" Header="Discount"
                               Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
    

    Replace bindings for your columns to corresponding properties in your model class. Output will be like this:

    enter image description here

    However, in case you want to distribute available space equally for all columns. You can set width to * for all columns. Output will be like this:

    enter image description here

    0 讨论(0)
  • 2020-12-09 03:36

    Solution 1 :

    Set AutoGenerateColumns="False" and Width="*" for all Columns

     <DataGrid x:Name="dataGrid" IsReadOnly="True" AutoGenerateColumns="False">
        <DataGrid.Columns>
           <DataGridTextColumn Binding="{Binding OrderId}" Header="OrderId" Width="*"/>
           <DataGridTextColumn Binding="{Binding ProductId}" Width="*" Header="ProductId"/>
           <DataGridTextColumn Binding="{Binding UnitPrice}" Width="*" Header="UnitPrice"/>
           <DataGridTextColumn Binding="{Binding Quantity}" Width="*" Header="Quantity"/>
           <DataGridTextColumn Binding="{Binding Discount}" Header="Discount"
                               Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
    

    Solution 2 : You can set like this to achieve your requirement

    <DataGrid HorizontalAlignment="Left" Margin="50,0,0,0" Width="500"
              Name="dataGrid"  IsReadOnly="True"  VerticalAlignment="Top"
              ItemsSource="{Binding Cus}" AutoGenerateColumns="True"/>
    
    
     this.dataGrid.AutoGeneratingColumn += dataGrid_AutoGeneratingColumn;
    
     void dataGrid_AutoGeneratingColumn(object sender, 
                                        DataGridAutoGeneratingColumnEventArgs e)
     {
         e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
     }
    
    0 讨论(0)
提交回复
热议问题