We help IT Professionals succeed at work.

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

Olukayode Oluwole
on
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
Comment
Watch Question

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:

Did you try using ListRepsonbileMgrModel instead of var?

Olukayode OluwoleSystems Analyst

Author

Commented:
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 OluwoleSystems Analyst

Author

Commented:
And ListResponsibleMgrModel is full list of all fields and their properties.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:

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 OluwoleSystems Analyst

Author

Commented:
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
Chief Technology Ninja
Distinguished Expert 2019
Commented:

Hi Olukay,


Please use:

string teststring = 
 dgvcmbDepartment.  
  SelectedItem.ToString()
    
 


Regards,

Chinmay.

Olukayode OluwoleSystems Analyst

Author

Commented:
Thanks for the assistance

Eventually it accepted   .Selected   NOT .SelectedItem

I guess thats a function of the version of c#

Olukay