how can i change my Datagrid column header text and how can i print updated header in excel?

Snack'Eyes

i dont want to change header in sqlite table,i just want to change it on datagrid and then want to print it in excel file....here is my XAML code

        <DataGrid.Columns>
            <DataGridTextColumn Header="A" Width="100" Binding="{Binding A}"/>

            <DataGridTextColumn Header="B" Width="100" Binding="{Binding B}"/>
            <DataGridTextColumn Header="rr" Width="100" Binding="{Binding C}"/>
            <DataGridTextColumn Header="D" Width="100" Binding="{Binding D}"/>
            <DataGridTextColumn Header="E" Width="100" Binding="{Binding E}"/>
            <DataGridTextColumn Header="F" Width="100" Binding="{Binding F}"/>
            <DataGridTextColumn Header="G" Width="100" Binding="{Binding G}"/>
            <DataGridTextColumn Header="H" Width="100" Binding="{Binding H}"/>
            <DataGridTextColumn Header="I" Width="100" Binding="{Binding I}"/>
            <DataGridTextColumn Header="J" Width="100" Binding="{Binding J}"/>
            <DataGridTextColumn Header="K" Width="100" Binding="{Binding K}"/>
            <DataGridTextColumn Header="L" Width="100" Binding="{Binding L}"/>
            <DataGridTextColumn Header="M" Width="100" Binding="{Binding M}"/>
            <DataGridTextColumn Header="N" Width="100" Binding="{Binding N}"/>
            <DataGridTextColumn Header="O" Width="100" Binding="{Binding O}"/>
            <DataGridTextColumn Header="P" Width="100" Binding="{Binding P}"/>
            <DataGridTextColumn Header="Q" Width="100" Binding="{Binding Q}"/>
            <DataGridTextColumn Header="R" Width="100" Binding="{Binding R}"/>
            <DataGridTextColumn Header="S" Width="100" Binding="{Binding S}"/>
            <DataGridTextColumn Header="T" Width="100" Binding="{Binding T}"/>
            <DataGridTextColumn Header="U" Width="100" Binding="{Binding U}"/>
            <DataGridTextColumn Header="V" Width="100" Binding="{Binding V}"/>
            <DataGridTextColumn Header="W" Width="100" Binding="{Binding W}"/>
            <DataGridTextColumn Header="X" Width="100" Binding="{Binding X}"/>
            <DataGridTextColumn Header="Y" Width="100" Binding="{Binding Y}"/>
            <DataGridTextColumn Header="Z" Width="100" Binding="{Binding Z}"/>

        </DataGrid.Columns>

and i want to change it by using textbox and button in cs file

Maro
private void Heder_A_Update_Click(object sender, RoutedEventArgs e)
    {
        var newHederText = MyTextBox.Text;

        var temp = new DataTemplate();
        temp.VisualTree = new FrameworkElementFactory(typeof(TextBlock));
        temp.VisualTree.SetValue(TextBlock.TextProperty, newHederText);
        dataGrid.Columns[0].HeaderTemplate = temp;
    }

Edit

1- Convert your datagrid to dataTable using the following function

public static DataTable GridToDataTable(DataGrid dataGrid)
{
    DataView dView = (DataView)dataGrid.ItemsSource)
    DataTable table= dView.Table.Clone();
    foreach (DataRowView dv in dView )
       dt.ImportRow(dView .Row);
    return table;
}

or

var table= ((DataView)dataGrid.ItemsSource).ToTable();  

2- Create extension method to export datatable to excel

public static void ExportToExcel(this DataTable dataTable, string excelFilePath = null)
        {
            try
            {
                int columnsCount;

                if (dataTable == null || (columnsCount = dataTable.Columns.Count) == 0)
                    throw new Exception("ExportToExcel: Null or empty input table!\n");

                // load excel, and create a new workbook
                var excel = new Microsoft.Office.Interop.Excel.Application();
                excel.Workbooks.Add();

                // single worksheet
                Microsoft.Office.Interop.Excel._Worksheet worksheet = excel.ActiveSheet;

                var header = new object[columnsCount];

                // column headings               
                for (int i = 0; i < columnsCount; i++)
                    header[i] = dataTable.Columns[i].ColumnName;

                Microsoft.Office.Interop.Excel.Range headerRange = worksheet.Range[(Microsoft.Office.Interop.Excel.Range)(worksheet.Cells[1, 1]), (Microsoft.Office.Interop.Excel.Range)(worksheet.Cells[1, columnsCount])];
                headerRange.Value = header;
                headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightSkyBlue);
                headerRange.Font.Bold = true;

                // DataCells
                int rowsCount = dataTable.Rows.Count;
                var cells = new object[rowsCount, columnsCount];

                for (int j = 0; j < rowsCount; j++)
                    for (int i = 0; i < columnsCount; i++)
                        cells[j, i] = dataTable.Rows[j][i];

                worksheet.Range[(Microsoft.Office.Interop.Excel.Range)(worksheet.Cells[2, 1]), (Microsoft.Office.Interop.Excel.Range)(worksheet.Cells[rowsCount + 1, columnsCount])].Value = cells;

                try
                {
                    worksheet.SaveAs(excelFilePath);
                    excel.Quit();
                }
                catch (Exception ex)
                {
                    throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
                        + ex.Message);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("ExportToExcel: \n" + ex.Message);
            }
        }
3- calling the method :
string filename = @"c:\temp\export.xlsx"; //
table.ExportToExcel(filename);

or

GridToDataTable(datagrid).ExportToExcel(filename);

Check if the headers are ok, otherwise add the following to Datatable extension create the header of excel

int index=1;
foreach (var colName in dataGrid.Columns.Select(cs => cs.Header).ToList())
{   
      excel.Cells[1, index] = colName.ToStrihng();
      index++; 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i print page without header?

From Dev

How can I change the table header color and text color?

From Dev

how can I include the header?

From Dev

Change DataGrid column header text

From Dev

How can I change the header of WordPress 'retrieve_password' email?

From Dev

Can I change how gedit prints its page header?

From Dev

How can I change the font of below jtable header

From Dev

How can I change the focused boder color of Tabpane header in javafx

From Dev

How can I make a circle using css and apply it to my header?

From Dev

How i can adding Listview Header to my app

From Dev

How can I make my header completely inline using float?

From Dev

How can I set my UITableView's Header to an Attributed String?

From Dev

How can I add an image header to my php mail form?

From Dev

How can I declare the nested types in my hierarchy header file?

From Dev

How i can adding Listview Header to my app

From Dev

How can I make a circle using css and apply it to my header?

From Dev

How can I disable editing content inside a column on my DataGrid

From Dev

How can I hide/remove a WPF ColumnHeaderStyle column header?

From Dev

how can I do html table left column and header fixed?

From Dev

How can I create a nested/split column header using TornadoFx?

From Dev

How can I insert a text field in the header part of each column in a w2Ui grid?

From Dev

How can I toggle text in the header of the jQuery accordion?

From Dev

How I can align the header text of the ExpandableListview to the extreme left?

From Dev

In a table with row headers and column headers, given a column header text and a cell text, how can I select a cell (td)?

From Dev

How can i modify the host header

From Dev

How can I provide a header or a footer for an MvxCollectionViewController?

From Dev

How can I get the relative header of a cell?

From Dev

How can I create a fixed header?

From Dev

How can I add a request header with AFNetworking?

Related Related

  1. 1

    How can i print page without header?

  2. 2

    How can I change the table header color and text color?

  3. 3

    how can I include the header?

  4. 4

    Change DataGrid column header text

  5. 5

    How can I change the header of WordPress 'retrieve_password' email?

  6. 6

    Can I change how gedit prints its page header?

  7. 7

    How can I change the font of below jtable header

  8. 8

    How can I change the focused boder color of Tabpane header in javafx

  9. 9

    How can I make a circle using css and apply it to my header?

  10. 10

    How i can adding Listview Header to my app

  11. 11

    How can I make my header completely inline using float?

  12. 12

    How can I set my UITableView's Header to an Attributed String?

  13. 13

    How can I add an image header to my php mail form?

  14. 14

    How can I declare the nested types in my hierarchy header file?

  15. 15

    How i can adding Listview Header to my app

  16. 16

    How can I make a circle using css and apply it to my header?

  17. 17

    How can I disable editing content inside a column on my DataGrid

  18. 18

    How can I hide/remove a WPF ColumnHeaderStyle column header?

  19. 19

    how can I do html table left column and header fixed?

  20. 20

    How can I create a nested/split column header using TornadoFx?

  21. 21

    How can I insert a text field in the header part of each column in a w2Ui grid?

  22. 22

    How can I toggle text in the header of the jQuery accordion?

  23. 23

    How I can align the header text of the ExpandableListview to the extreme left?

  24. 24

    In a table with row headers and column headers, given a column header text and a cell text, how can I select a cell (td)?

  25. 25

    How can i modify the host header

  26. 26

    How can I provide a header or a footer for an MvxCollectionViewController?

  27. 27

    How can I get the relative header of a cell?

  28. 28

    How can I create a fixed header?

  29. 29

    How can I add a request header with AFNetworking?

HotTag

Archive