0
votes

In my form I have datagrid view and I select the data into datagridview by using the following stored procedure :

create proc [dbo].[GET_CULTURE_RESULT_DETAILS]
@ORDER_ID int 
as 
select LAB_MICRO_RESULTS_DETAILS.[organism_id] as 'Org.Id' , organism_desc as 'Organism Name'
      ,LAB_MICRO_RESULTS_DETAILS.[Antibiotic_id] as 'Ant.Id', Antibiotic_Name as 'Antibiotic Name'
      ,LAB_MICRO_RESULTS_DETAILS.[sensitivityId] as 'Sens.Id', Sensitivity_desc as 'Sensitivity Name'
from LAB_MICRO_RESULTS_DETAILS 
inner join lab_organisms on LAB_MICRO_RESULTS_DETAILS.organism_id = lab_organisms.organism_id
inner join lab_antibiotics on LAB_MICRO_RESULTS_DETAILS.Antibiotic_id = lab_antibiotics.Antibiotic_id
inner join Lab_Sensitivity on LAB_MICRO_RESULTS_DETAILS.sensitivityId = Lab_Sensitivity.sensitivityId

where LAB_MICRO_RESULTS_DETAILS.order_id = @ORDER_ID

Then when select the order number datagridview filled using key down event this is the code :

private void txtOrder_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && txtOrder.Text != string.Empty)

            {
                dgvcultures.DataSource = micro.GET_CULTURE_RESULT_DETAILS(Convert.ToInt32(txtOrder.Text));
                txtPCfile.Focus();
            }
        }

Finally the error appeared when I try to add new row into the datagrid view by using this code :

private void btnAdd_Click(object sender, EventArgs e)
        {
            
                    if (dgvcultures.Columns.Count >= 0)
                    {
               
                        dgvcultures.ColumnCount = 6;
                        dgvcultures.Columns[0].Name = "Org.Id";
                        dgvcultures.Columns[1].Name = "Organism Name";
                        dgvcultures.Columns[2].Name = "Ant.Id";
                        dgvcultures.Columns[3].Name = "Antibiotic Name";
                        dgvcultures.Columns[4].Name = "Sens.Id";
                        dgvcultures.Columns[5].Name = "Sensitivity Name";



                        dgvcultures.Rows.Add(comboOrganism.SelectedValue.ToString(), comboOrganism.Text,
                            comboAntibiotics.SelectedValue.ToString(), comboAntibiotics.Text,
                            comboSensitivity.SelectedValue.ToString(), comboSensitivity.Text);
                        // comboGrowth.SelectedValue.ToString(),comboGrowth.Text);

                    }
     }

when click add button and try to insert new row I got this error :

ColumnCount property cannot be set on a data-bound DataGridView control. c#

I checked the solutions in this site and they said you have to clear the datagridview before insert new rows I tried it its clear the filled data from stored procedure and I dont need to clear datagridview , I need to keep the data and add new rows without clear the current data :

//Clear the binding.
dgvcultures.DataSource = null;

this command will remove the rows in datagridview , but I need to keep rows.

How to solve this error please.

1
Add the new row to the grids DataSource. As the error clearly states you can NOT directly ADD rows into the "GRID" when it has a DataSource... data-bound..JohnG
@JohnG how to save the rows in datasource and append new rows to that datasource without clear the available rows ?Ziad Adnan

1 Answers

2
votes

From what I can decipher, the grids DataSource looks like a DataTable with the following columns…

Org.Id, Organism Name, Ant.Id, Antibiotic Name, Sens.Id and Sensitivity Name.

I assume this from the first snippet of code that gets the table from the DB.

If this DataTable is used as a DataSource to the grid, then in the button click event, it is unnecessary to “re-create” the columns. Those columns already exist in the table. The code simply needs to add the row to the DataTable. It is not clear if the column “types” are all strings and the code may have to convert some values, otherwise the code in the button click event may look something like…

DataTable gridDT = (DataTable)dgvcultures.DataSource;
gridDT.Rows.Add(comboOrganism.SelectedValue.ToString(),
                comboOrganism.Text,
                comboAntibiotics.SelectedValue.ToString(),
                comboAntibiotics.Text,
                comboSensitivity.SelectedValue.ToString(),
                comboSensitivity.Text);

Lastly, you should consider using a BindingSource. It may make things easier.

I hope this makes sense.