RadListView Fixing column size in DetailView mode

2 Answers 299 Views
ListView
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
Patgat asked on 09 May 2022, 05:57 PM

Dear all,

I am having difficulties to fix the column size in a ListView in Detail mode.

My set up code is very simple and as follow :

        private void SetLisViewProduitBase()
        {
            ListViewDetailColumn PBaseColumn;
            rlvProduitBase.ViewType = ListViewType.DetailsView;
            rlvProduitBase.AutoSizeColumnsMode = ListViewAutoSizeColumnsMode.None;
            rlvProduitBase.HeaderHeight = 18;
            rlvProduitBase.Font = new Font(rlvProduitBase.Font, FontStyle.Regular);

            rlvProduitBase.ShowCheckBoxes = true;
            rlvProduitBase.ThreeStateMode = false;

            PBaseColumn = new ListViewDetailColumn("Code");
            PBaseColumn.Width = 40;
            rlvProduitBase.Columns.Add(PBaseColumn);

            PBaseColumn = new ListViewDetailColumn("Forme");
            PBaseColumn.Width = 50;
            rlvProduitBase.Columns.Add(PBaseColumn);

            PBaseColumn = new ListViewDetailColumn("Nom Usuel");
            PBaseColumn.Width = 100;
            rlvProduitBase.Columns.Add(PBaseColumn);

            PBaseColumn = new ListViewDetailColumn("Nom Botanique");
            PBaseColumn.Width = 100;
            rlvProduitBase.Columns.Add(PBaseColumn);

            PBaseColumn = new ListViewDetailColumn("Origine");
            PBaseColumn.Width = 70;
            rlvProduitBase.Columns.Add(PBaseColumn);

            PBaseColumn = new ListViewDetailColumn("Labels");
            PBaseColumn.Width = 70;
            rlvProduitBase.Columns.Add(PBaseColumn);
        }

While empty, the columns width are displaying fine as seen on image 1.

When I connect the Datasource (as in https://docs.telerik.com/devtools/winforms/controls/listview/populating-with-data/data-binding), all columns get the same "default" size.

As you say (in the Adding Columns doc) that using a datasource will require to set the column width within the ColumnCreating event, I have tried this.

When this event is first called after defining the columns as in the previous code, I get a NullReference exception as if the columns are not defined.

If I try to create the columns them within the event and test first for column existence, I will get a column already exist exception (An item with the same key already exist).

What am I doing wrong ?

Many thanks for your answers

Patrick


2 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 10 May 2022, 10:24 AM

Hello Patrick,

Thank you for the provided details.

Upon checking the code snippet, I could not see where the ColumnCreating event is subscribed to an event handler. The subscription needs to be added before setting the DataSource of the RadListView. Basically, in Data Bound mode in DetailsView ViewType, the values of all the properties of the business objects will be displayed in columns. In the event handler, you can access them and add your customization. It seems that I will need more information when the DataSource of the RadListView is set. Could you modify the project and use the NwindDataSet inside the project and just add your code to populate the control to mimic your implementation. This way I could have a better look and debug it.

Regards,
Dinko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
answered on 11 May 2022, 03:47 PM

Hello Dinko,

thanks for your reply.

The subscription is done in the designer as the control is set up during the design phase.

However, thanks to the example attached, I understood that my mistake was either to try( re)creating the columns within the event and not to test for each column name before applying the column width.

It is now working perfetly well as below:

        private void rlvProduitBase_ColumnCreating(object sender, ListViewColumnCreatingEventArgs e)
        {
            if (e.Column.FieldName == "Code") e.Column.Width = 40;
            if (e.Column.FieldName == "Forme") e.Column.Width = 50;
            if (e.Column.FieldName == "Nom Usuel") e.Column.Width = 100;
            if (e.Column.FieldName == "Nom Botanique") e.Column.Width = 100;
            if (e.Column.FieldName == "Origine") e.Column.Width = 70;
            if (e.Column.FieldName == "Labels") e.Column.Width = 70;
        }

Again, many thanks

Patrick

NB : just one last question : wouldn't a switch be more efficient in this case than 7 tests ?

Dinko | Tech Support Engineer
Telerik team
commented on 12 May 2022, 11:10 AM

You are right, a switch case will be more efficient. This way you can reduce the duplicate code in the event handler.
Tags
ListView
Asked by
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or