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.
DataSource
. As the error clearly states you can NOT directly ADD rows into the "GRID" when it has aDataSource
... data-bound.. – JohnG