Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag 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"

User generated image"

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 )

User generated image
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
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Did you try using ListRepsonbileMgrModel instead of var?

Avatar of 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.
And ListResponsibleMgrModel is full list of all fields and their properties.

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.

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)

User generated image
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
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the assistance

Eventually it accepted   .Selected   NOT .SelectedItem

I guess thats a function of the version of c#

Olukay