1
I have a Datagridview where I fill it with data from a mysql table and then need to add lines with some textbox data, when I try returns the following error: "It is not possible to add lines programmatically to the collection of Datagridview lines when the control is associated with data".
my code :
private void button1_Click(object sender, EventArgs e)
{
Hide();
FormPai.dataGridView1.Rows.Add(txtCod.Text, servicosBox.Text, txtPreco.Text);
FormPai.dataGridView1.Refresh();
}
Hello Francisco, gave the following error when I tried to use your code: cannot Convert from 'System.Windows.Forms.Datagridview' to 'System.Data.Datatable'
– Lucas Lima
I made a mistake, you have to pass the datasource in the parameters, instead of the datagridview.
– Francisco
I didn’t understand this part of passing the Datasource in the parameters, I used the following code to fill my Datagridview: Mysqlcommand command2 = new Mysqlcommand("select Cod,servico,preco from guardaservicos Where Codos=@Codos ", conect.conexao); command2.Parameters.Add("@Codos", Mysqldbtype.Varchar). Value = lblNumOS.Text; Mysqldataadapter adapter2 = new Mysqldataadapter(command2); Datatable data2 = new Datatable(); adapter2.Fill(data2); dataGridView1.Datasource = data2;
– Lucas Lima
I didn’t find what I have to put as a parameter there.
– Lucas Lima
In your case, the variable
data2
, because she is your Datasource– Francisco
Thank you very much,.
– Lucas Lima
Don’t forget to mark the answer as correct to close the topic and also help those who have the same problem!
– Francisco