Невозможно прокрутить в сетке данных - PullRequest
0 голосов
/ 02 марта 2020

Здравствуйте, я разрабатываю simeple c# WPF-программу.

Она читает таблицу Excel и распространяется на странице.

Проблема заключается в том, что при добавлении столбцов активируется полоса прокрутки вручную.

Однако полоса прокрутки не активируется как прикрепленный код.

Спасибо за вашу помощь.

CR1.xaml

    <Page x:Class="_62443_Guide.CR1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:local="clr-namespace:_62443_Guide"
          mc:Ignorable="d" 
          Title="CR1">

        <Grid Name="MainGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>

            <Grid Name="Grid0" Grid.Row="0" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBox HorizontalAlignment="Left" Height="34" Margin="349,43,0,35.4" TextWrapping="Wrap" Text="TextBox" Width="403"/>
                <Button Content="ReadButton" HorizontalAlignment="Left" Height="34" Margin="33,43,0,35.4" Width="103" Click="Button_Click_1"/>
            </Grid>

            <ScrollViewer HorizontalScrollBarVisibility="Auto" Grid.Row="1">
                <DataGrid Name="DG1" MaxColumnWidth="300">
                </DataGrid>
            </ScrollViewer>

        </Grid>
    </Page>

CR1.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Data;

    namespace _62443_Guide
    {
        /// <summary>
        /// CR1.xaml에 대한 상호 작용 논리
        /// </summary>
        public partial class CR1 : Page
        {

            public CR1()
            {
                InitializeComponent();
            }

            string[] ConvertToStringArray(System.Array values)
            {
                // create a new string array
                string[] theArray = new string[values.Length];

                // loop through the 2-D System.Array and populate the 1-D String Array
                for (int i = 1; i <= 27; i++)
                {
                    if (values.GetValue(i, 1) == null)

                        theArray[i - 1] = "";
                    else

                        theArray[i - 1] = (string)values.GetValue(i, 1).ToString();
                }

                return theArray;
            }

            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                int rCnt = 0;
                int cCnt = 0;
                string str;
                string sCellData = "";
                double dCellData;

                var excelApp = new Excel.Application();
                Excel.Workbook wb = excelApp.Workbooks.Open(Filename: @"C:\Users\kang8\Documents\GitHub\62443-guide\data\62443-4-2 guide");
                Excel.Worksheet ws = wb.Worksheets.get_Item(1) as Excel.Worksheet;
                Excel.Range range = ws.UsedRange;

                DataTable dt = new DataTable();

                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    str = (string)(range.Cells[1, cCnt] as Excel.Range).Value2;
                    dt.Columns.Add(str, typeof(string));
                }

                for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
                {
                    string sData = "";
                    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                    {
                        try
                        {
                            sCellData = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            sData += sCellData + "|";
                        }
                        catch (Exception ex)
                        {
                            dCellData = (range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            sData += dCellData.ToString() + "|";
                        }
                    }
                    sData = sData.Remove(sData.Length - 1, 1);
                    dt.Rows.Add(sData.Split('|'));
                }

                DG1.ItemsSource = dt.DefaultView;

                wb.Close(true);
                excelApp.Quit();
            }
        }
    }
...