0
I make a select in my database and it brings the values in the datagrid. However when I try to add/delete lines, it gives error:
It is not possible to add lines programmatically to the Datagridview line collection when the control is associated with data."
Select code:
DataSet ds = new DataSet();
string comando = "SELECT * FROM item_orcamento where id_orcamento = '"+ Convert.ToInt32(txtidos.Text) + "' and tipo_item='Produto' order by id_orcamento";
MySqlDataAdapter da = new MySqlDataAdapter(comando, stringCon);
MySqlCommandBuilder cmdb = new MySqlCommandBuilder(da);
da.Fill(ds, "item_orcamento");
dataGridProdutos.DataSource = ds.Tables["item_orcamento"];
da.Dispose();
Code of the Insert button:
private void addDataServicos(string codServico, string nomeServico, string qtdServico, string valorServico, string valorTotalServico)
{
String[] row = { codServico, nomeServico, qtdServico, valorServico, valorTotalServico };
dataGridServicos.Rows.Add(row);
}
How can I insert new lines?
private void CriarRow(DataTable source)
{
string valor = txtprecoServico.Text;
valor = valor.Replace("R$", "");
int qtd = 0;
qtd = Convert.ToInt32(txtquantidades.Text);
double soma = Convert.ToDouble(valor);
double valorTotalServico = qtd * soma;
var row = source.NewRow();
row[0] = 0;
row[1] = txtcodServico.Text;
row[2] = 0;//2-tipo_item,
row[3] = txtnomeServico.Text;
row[4] = txtquantidades.Text;
row[5] = (valor);//5-valor_item,
row[6] = valorTotalServico.ToString();
row[7] = txtidos.Text;
source.Rows.Add(row);
}
You are using two techniques to fill, I believe it is, or is one or the other! maybe a better structure of List would be better option
– novic
recommend using Bindinglist to work on datagridview, it will be much easier
– Rovann Linhalis