DataGrid Row Header WPF

后端 未结 2 723
北荒
北荒 2021-02-15 16:37

I have a populated DataTable (xDataTable) with 7 defined columns - the first column I want to use as my RowHeader.

I also have a DataGrid:

 

        
相关标签:
2条回答
  • 2021-02-15 16:52

    Use the below style (usual case):

    <DataGrid.RowHeaderStyle>
        <Style TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},Path=Columns[0].Header,Mode=OneTime}" />
        </Style>
    </DataGrid.RowHeaderStyle>
    

    If we want separate RowHeader for separate Row then use:

    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="IsSelected" Value="{Binding IsRowSelected}" />
            <Setter Property="Header" Value="{Binding Content}" />
        </Style>
    </DataGrid.RowStyle>
    

    Just change the above binding as required.

    If the first column is:

    <DataGridTextColumn Binding="{Binding Content}" Header="Content"/>
    

    then remove this column and use this binding for the Header.

    0 讨论(0)
  • 2021-02-15 16:57

    you can set any headers whatever you want. Just add your columns like that:

     DataTable.Columns.Add("I am a Column Header!:)");
    

    Let' see MVVM example:

    public class YourViewModel : ViewModelBase
    {
        public YourViewModel()
        {
            PopulateDataTable();
        }
    
        private void PopulateDataTable()
        {
            var _ds = new DataSet("Test");
            employeeDataTable = new DataTable();
            employeeDataTable = _ds.Tables.Add("DT");
            for (int i = 0; i < 20; i++)
            {                
                //you can set any Header in the following line
                employeeDataTable.Columns.Add(i.ToString());
            }
            for (int i = 0; i < 10; i++)
            {
                var theRow = employeeDataTable.NewRow();
                for (int j = 0; j < 20; j++)
                {
                    theRow[j] = "a";                    
                }
                employeeDataTable.Rows.Add(theRow);
            }
        }
    
         private DataTable employeeDataTable;
    
         public DataTable EmployeeDataTable
         {
            get { return employeeDataTable; }
            set
            {
                employeeDataTable = value;
                OnPropertyChanged("EmployeeDataTable");
            }
         }
    }
    

    Your XAML:

    <DataGrid ItemsSource="{Binding EmployeeDataTable}" />
    

    Update:

    Let's see code-behind example:

    Your XAML:

    <DataGrid Name="dataGrid"/>
    

    Your code-behind:

    //constructor of the Window
    public MainWindow()
    {
        InitializeComponent();
        PopulateDataGrid();
    }
    
    DataTable employeeDataTable = new DataTable();
    
    
    private void PopulateDataGrid()
    {
       var _ds = new DataSet("Test");
    
       employeeDataTable = _ds.Tables.Add("DT");
       for (int i = 0; i < 10; i++)//create columns
       {   
            employeeDataTable.Columns.Add("I am a column!:)");
       }
    
       for (int i = 0; i < 50; i++)//fill data to rows
       {
           var theRow = employeeDataTable.NewRow();
           for (int j = 0; j < 10; j++)
           {
              if (j % 2 == 0)                    
                  theRow[j] = "a";  
              else                  
                  theRow[j] = "b";
           }
           employeeDataTable.Rows.Add(theRow);
       }
       dataGrid.ItemsSource = employeeDataTable.AsDataView();
    }
    
    0 讨论(0)
提交回复
热议问题