Collection of String Only Displays First Character

5 posts, 1 answers
  1. Raymond
    Raymond avatar
    65 posts
    Member since:
    Oct 2013

    Posted 27 Aug 2014 Link to this post

    I am attempting to use a GridView to display the contents of a delimited file.  Initially the application reads the 1st 20 lines of the file and displays these in a single column with one row per line.  The user then selects the field delimiter to use to parse the rows into columns.

    My problem is when the 1st 20 rows are displayed only the first character from each row is displayed in the single column of the GridView;

    private void PreviewFile(string importFile)
    {
        string line = null;
        string[] colData = {"Column 1"};
     
        ObservableCollection<string> colRows = new ObservableCollection<string>();
     
        FileImport.CSV csvImport = new FileImport.CSV(fileName);
     
        for (int iIndex = 0; iIndex < 20; iIndex++)
        {
            line = csvImport.GetLine();
            if (line == null) break;
            colRows.Add(line);
        }
     
        this.gvText.ItemsSource = colRows;
        this.AddColumns(gvText, colData);
        //this.gvText.ShowColumnHeaders = true;
    }
     
    private void AddColumns(RadGridView gv, string[] columnNames)
    {
        gv.Columns.Clear();
     
        for (int i = 0; i < columnNames.Length; i++)
        {
            gv.Columns.Add(new GridViewDataColumn
            {
                Header = columnNames[i],
                DataMemberBinding = new Binding(String.Format("[{0}]", i)),
                Width = new GridViewLength(1, GridViewLengthUnitType.Star)
                //MaxWidth = maxWidth
            });
        }
    }

    I have confirmed that "csvImport.GetLine" is returning a single line from the text file (anything from ~400 to ~1500 characters) and that these are added correctly to the ObservableCollection.

    How can I get the GridView to display the entire string for each row?

    Thanks in advance for any help anyone can provide.
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 29 Aug 2014 Link to this post

    Hi,

    It seems you set each line in the CSV file as a column in RadGridView. I do not see where you set the ItemsSource for your gv instance. 
    Would you please try setting the colRows collection as an ItemsSource instead?

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. Raymond
    Raymond avatar
    65 posts
    Member since:
    Oct 2013

    Posted 31 Aug 2014 in reply to Dimitrina Link to this post

    Hi Didie,

    I am setting the ItemsSource on the line;
    this.gvText.ItemsSource = colRows;

    and setting the column on the following line.  I also (at one point) tried setting the MaxWidth property but this had no effect.

    Rregrads,

    Ray
  4. Answer
    Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 01 Sep 2014 Link to this post

    Hi Ray,

    Indeed, this is correct.
    The reason then should be how you define the DataMemberBinding of the columns:
    gv.Columns.Add(new GridViewDataColumn
    {
        Header = columnNames[i],
        DataMemberBinding = new Binding(String.Format("[{0}]", i)),
        Width = new GridViewLength(1, GridViewLengthUnitType.Star)
        //MaxWidth = maxWidth
    });

    It seems that just one char is requested as you have the following: DataMemberBinding = new Binding(String.Format("[{0}]", i)).

    If this does not help, would you please send me a demo project which I can check locally? You can open a new support thread and attach the solution there.

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  5. Raymond
    Raymond avatar
    65 posts
    Member since:
    Oct 2013

    Posted 02 Sep 2014 in reply to Dimitrina Link to this post

    Hi Didie,

    Problem turned out to be that I was using a collection of strings to populate the grid rather than a collection of string arrays;

    private void PreviewFile(string importFile)
    {
        ObservableCollection<string[]> colRows = new ObservableCollection<string[]>();
     
        FileImport.CSV csvImport = new FileImport.CSV(msFileName);
     
        for (int iIndex = 0; iIndex < 20; iIndex++)
        {
            line[0] = csvImport.GetLine();
            if (line[0] == null) break;
            colRows.Add(line);
        }
     
        this.gvText.ItemsSource = colRows;
     
        string[] colData = { "Column 1" };
        this.AddColumns(gvText, colData);
     
        this.gvText.ShowColumnHeaders = true;
    }

    Once I made this change the grid recognised the first element of the string array as the column and displayed the entire line.

    Thanks for all your help.

    Ray
Back to Top