This is a migrated thread and some comments may be shown as answers.

Collection of String Only Displays First Character

4 Answers 163 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 27 Aug 2014, 08:59 PM
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.

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 29 Aug 2014, 01:23 PM
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.
 
0
Raymond
Top achievements
Rank 1
answered on 31 Aug 2014, 08:44 PM
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
0
Accepted
Dimitrina
Telerik team
answered on 01 Sep 2014, 06:37 AM
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.
 
0
Raymond
Top achievements
Rank 1
answered on 02 Sep 2014, 05:04 AM
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
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Raymond
Top achievements
Rank 1
Share this question
or