Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

How do i write Values from a List and combo to a DataGrid

I have a c#  application in which i am trying to
write values from a list  and a combo  into a grid and later on to  save it.

The grade level is loaded to a  List Box  while
The Entry codes are loaded to a combo (within the Grid)  see  attached screen

User generated image
I have a problem specifying the the statement that will move the items from the List and Combo
to the Grid.

I believe the first issue  is the fact that the script attached below is not under the right event
as i am not able to address  the Currentrow under this event.

1. Which event should i use to ensure i have access  to the Current row so that my selections
will be writing on the current row

2. I am casting the Model  Proportion Model  to a Combox  ( loaded within the grid )
Is this correct or i should be casting the model to a DataGrid instead  ( see code below )

private void dgvDetailsTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                string varentcode = "";
                string varenttype = "";
                var data = (sender as ComboBox).SelectedItem as ProportionsModel;
                varentcode = data.Ent_code;
                varenttype = data.Ent_type;

                Convert.ToString(dgvDetailsTable.CurrentRow.Cells["txtcmbCode"]) = data.Ent_code;
                Convert.ToString(dgvDetailsTable.CurrentRow.Cells["txtenttype"]) = data.Ent_type;
            }
        }

Open in new window


3. What would be correct syntax for the 2 lines flagged as error  (see  screen below)

User generated image
I will appreciate suggestion to enable me resolve this issue

Regards

Olukay
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

As you already have the strings, I can't see why to attempt a convert. So try something like:

                varentcode = data.Ent_code;
                varenttype = data.Ent_type;

                dgvDetailsTable.CurrentRow.Cells["txtcmbCode"].Value = varentcode;
                dgvDetailsTable.CurrentRow.Cells["txtenttype"].Value = varenttype;

Open in new window

Avatar of Olukayode Oluwole

ASKER

Thanks Gustav.

I have redone the code and currently have no syntax issues (see code script below)

 private void dgvDetailsTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.ColumnIndex == 1)
            //{
            //            string varentcode = "";
            //            string varenttype = "";
            //            var data = (sender as ComboBox).SelectedItem as ProportionsModel;
            //            varentcode = data.Ent_code;
            //            varenttype = data.Ent_type;

            //            dgvDetailsTable.CurrentRow.Cells["txtcmbCode"].Value = varentcode;
            //            dgvDetailsTable.CurrentRow.Cells["txtenttype"].Value = varenttype;
            //            dgvDetailsTable.CurrentRow.Cells["txtgradelevel"].Value = GradeLevelValue.Text;
            //            dgvDetailsTable.CurrentRow.Cells["txtFilterType"].Value = FilterFieldValue.Text;

            //}
        }

        private void dgvDetailsTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                string varentcode = "";
                string varenttype = "";
                var data = (sender as ComboBox).SelectedItem as ProportionsModel;
                varentcode = data.Ent_code;
                varenttype = data.Ent_type;

                dgvDetailsTable.CurrentRow.Cells["txtcmbCode"].Value = varentcode;
                dgvDetailsTable.CurrentRow.Cells["txtenttype"].Value = varenttype;
                dgvDetailsTable.CurrentRow.Cells["txtgradelevel"].Value = GradeLevelValue.Text;
                dgvDetailsTable.CurrentRow.Cells["txtFilterType"].Value = FilterFieldValue.Text;

            }
        }

Open in new window


But I have an issues of where to put it .

I have tried the 2 events above but the result is not satisfactory

Under  
private void dgvDetailsTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)

The form does not load  because it keeps seeing a null reference  for the combo which during load is empty.

under

private void dgvDetailsTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

the form loads but getting the mouse double click event to fire  has not worked properly.

What i want is that the form form should load first with the List and Combo  populated

Then when ever i select a value from the combo the 4 values in the grid should be populated on that particular line.

In other words if i stay on a line and keep changing the combo values the 4 grid values should keep changing.

under which even should i put the script

Thanks

olukay
The cell should hold the value with no further actions. Here's an example snippet from a combobox in a DataGridView:

User generated image
hi Gustav,
Now the form loads  with the list AND combo  populated  with the code below

private void dgvDetailsTable_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                string varentcode = "";
                string varenttype = "";
                var data = (sender as ComboBox).SelectedItem as ProportionsModel;
                varentcode = data.Ent_code;
                varenttype = data.Ent_type;

                dgvDetailsTable.CurrentRow.Cells["txtcmbCode"].Value = varentcode;
                dgvDetailsTable.CurrentRow.Cells["txtenttype"].Value = varenttype;
                dgvDetailsTable.CurrentRow.Cells["txtgradelevel"].Value = GradeLevelValue.Text;
                dgvDetailsTable.CurrentRow.Cells["txtFilterType"].Value = FilterFieldValue.Text;

            }
        }

Open in new window


But nothing happens when the code in the combo is changed  

I placed the debugger just before the code  and changing  the code in the combo
does nothing. The application does not go to that section at all.

Below is the screen shot to show where i have my cursor in the debugger

User generated image
What am I doing wrong and how can i fix this last bit

I seem so close yet so far   !!!!

Thanks

olukay
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark 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
I did not get it to work.

But i will  continue to struggle to fix it.

Thanks

Olukay