Avatar of Olukayode Oluwole
Olukayode Oluwole
Flag for Canada asked on

How can i cast contents of a list into 2 fields withing a Grid

I have a Grid called dgvDetailsTables in my C# application

I need to populated 2 columns of the Grid   (Department and Division)

from a list . Currently A click puts the whole record into the Department column.

The code below loads the Combo within the Grid  and also gives me  a list within the Combo  to select from

 dgvcmbDepartment.DataSource = GlobalConfig.Connection.GetResponsibleMgr_All();
 dgvcmdDepartment.DisplayMember = "FullCodeName"


 public string FullCodeName => $"{divisioncode}  {deptcode }  { deptdesc} ";


See  the Grid with the Combo loaded  elements of "FullCodeName"

Combo Withing the Grid already having a list"

I now need to cast the content of "FullCodeName"  so that the first element   goes into column 6  (Division )

and the Second element goes into coulmn7 (Department)

I am trying to Cast the content of FullCodeName such that i can achieve this goal

but i know my cast statement has an error  ( See Code below )

Wrong  Cast  Statement
Can someone please help to look at the Cast statement and how i should modify it

so that on Click event  I get the 2 columns into the grid columns desired.

Thanks


Olukay
C#* DataGridView

Avatar of undefined
Last Comment
Olukayode Oluwole

8/22/2022 - Mon
Chinmay Patel

Did you try using ListRepsonbileMgrModel instead of var?

Olukayode Oluwole

ASKER
ListResponsibleMgr is the table from where I loaded the Combo.
It has among others the following fields
Division , deptcode and description. These are the 3 fields in the Combo.
Olukayode Oluwole

ASKER
And ListResponsibleMgrModel is full list of all fields and their properties.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Chinmay Patel

Hi Olukay,


Sorry for the delayed answer, you can not cast a DisplayMember like that. You should ideally bring that data separately from the data source and if that is not possible, then you will have to split the string using String.Split(" ").


PS: My Initial answer was wrong, I did not realize you are trying to DisplayMemeber to a string.


Regards,

Chinmay.

Olukayode Oluwole

ASKER
I have decided to use split  ( see code Below )

[private void dgvDetailsTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            switch (dgvDetailsTable.Columns[e.ColumnIndex].Name)
            {
                case "dgvcmbDepartment":

                    string phrase =  "TESTDIV TESTDEPT TESTRECORD";
                    string teststring = dgvcmbDepartment.Selected();
                                       
                    string[] words = phrase.Split(' ');
                    int i = 0;
                    foreach (var word in words)
                    {
                        i +=  1;

                        if (i == 1)
                        {
                            string var1 = $"{word}";
                            this.dgvDetailsTable.CurrentRow.Cells[6].Value = var1;
                        }
                    }
                    break;
            }
        }[/code]

It works when i used  a local string called phrase

When i tried to use the selected row from the combo i got an error

(see  screen below)

Error flagged
How can I get the selected row from the combo as my string.

The code for the loading the combo is shown below

dgvcmbDepartment.DataSource = GlobalConfig.Connection.GetResponsibleMgr_All();
 dgvcmbDepartment.DisplayMember = "FullCodeName";
 dgvcmbDepartment.ValueMember = "deptcode";

Thanks


Olukay
ASKER CERTIFIED SOLUTION
Chinmay Patel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Olukayode Oluwole

ASKER
Thanks for the assistance

Eventually it accepted   .Selected   NOT .SelectedItem

I guess thats a function of the version of c#

Olukay
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.