Display column value by id from another table in RadGridView

1 Answer 114 Views
GridView
hDrummer
Top achievements
Rank 1
hDrummer asked on 16 Aug 2022, 06:37 AM

Hi!

I have dataset with one table and I used it to fill RadGridView with columns.

One of the columns is foreign key (SQL Server type bigint) and autogenerated as GridViewDecimalColumn.

I do not want to make master-detail view, but I'd like to show values from another table based on this foreign key.

 

For example,

Table 1

id bigint

code_id_form_table2 bigint

 

Table 2

id bigint

name nvarchar(50)

So, I'd like to show name instead of code_id_from_table2 in the RadGridView. I know how to do it with built-in DataGridView, but how can I achieve the same result with Telerik component?

1 Answer, 1 is accepted

Sort by
1
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Aug 2022, 09:03 AM

Hello, Vitalii,  

Let's take for example the Northwind database and its Products and Categories table. If you bind the RadGridView control to the Products table, you will notice that there is an automatically generated GridViewDecimalColumn that stores the CategoryID:

However, you can replace this column with a GridViewComboBoxColumn that uses the Categories table for its population. More information how to setup such a column is demonstrated here: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/column-types/gridviewcomboboxcolumn 

I have prepared a sample code snippet for your reference: 

        private void RadForm1_Load(object sender, EventArgs e)
        { 
            this.productsTableAdapter.Fill(this.nwindDataSet.Products); 
            this.categoriesTableAdapter.Fill(this.nwindDataSet.Categories);

            this.radGridView1.DataSource = this.productsBindingSource;
            this.radGridView1.Columns.Remove("CategoryID");

            GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("CategoryID"); 
            comboColumn.DataSource = this.categoriesBindingSource;
            comboColumn.ValueMember = "CategoryID";
            comboColumn.DisplayMember = "CategoryName";
            comboColumn.FieldName = "CategoryID"; 
            this.radGridView1.Columns.Insert(2, comboColumn);

            this.radGridView1.BestFitColumns();
        }

Please refer to the attached sample project. I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

hDrummer
Top achievements
Rank 1
commented on 16 Aug 2022, 11:49 AM

Thank you, it works.

In my case it looks like

                rdgvMNT.Columns.Remove("CODE_DISP_LIST_CATALOG_BY_DATE");

                GridViewComboBoxColumn clmnDLData = new GridViewComboBoxColumn("DL Date");
                clmnDLData.DataSource = bs_dtCATALOG_BY_DATE;
                clmnDLData.ValueMember = "ID";
                clmnDLData.DisplayMember = "DATA_DISP_LIST_DATE";
                clmnDLData.FieldName = "CODE_DISP_LIST_CATALOG_BY_DATE";

Where ValueMember and DisplayMember are from Table 2, FieldName is from Table 1.

To the point, if I do not remove the column, I can't see inserted one. But if I remove it - I do. Why?

hDrummer
Top achievements
Rank 1
commented on 16 Aug 2022, 12:16 PM

Another question is - I group the data by this runtime created column programmatically, but I want to sort this grouped data not by valuemember, but by displaymember.

How to get the desired result?

Dess | Tech Support Engineer, Principal
Telerik team
commented on 16 Aug 2022, 01:43 PM

It is recommended to remove the automatically generated column and then add the desired column type, because RadGridView allows only one column to be added with the respective Name. Duplicated columns are not allowed.

As to the sort order of the groups, RadGridView offers a convenient way to control the order of the groups. The following KB article demonstrates a sample approach: https://docs.telerik.com/devtools/winforms/controls/gridview/grouping/sorting-group-rows 

You can implement a custom comparer according to the requirement you are trying to achieve.

hDrummer
Top achievements
Rank 1
commented on 24 Aug 2022, 05:56 AM

Hi,

I would like to sort Groups, not rows in a group.

As you can see from above question, I've got several groups, then replace ID to DateTime value and now I want several groups to be sorted by this DateTime value, not by ID value.

How can I get this?

Dess | Tech Support Engineer, Principal
Telerik team
commented on 25 Aug 2022, 05:22 AM

The previously referred KB article demonstrates exactly how to sort the group rows /the grid is grouped by DepartmentID/, not the rows within the groups. For this purpose, it is necessary to implement IComparer<Group<GridViewRowInfo>> that controls the condition for comparing the group headers. Please give this approach a try and see how it works on your end.
Tags
GridView
Asked by
hDrummer
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or