Does not insert lines in Datagridview

Asked

Viewed 1,397 times

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

  • recommend using Bindinglist to work on datagridview, it will be much easier

1 answer

1

You need to add the line to the object that does the Binding data and not direct to DataGridView.

In your case, you need to add the lines to the DataTable

Something like

private void CriarRow(DataTable source)
{
    var row = source.NewRow();
    row[0] = "Alguma coisa";
    row[1] = "E assim por diante";
    source.Rows.Add(row);
}

Use

var dataTable = (DataTable)dataGridProdutos.DataSource;
CriarRow(dataTable);
  • Hi, I tried to suit here, but nothing happens:

  • @Jessf And no mistake?

  • No, it just add an empty line.

  • So it doesn’t mean that nothing happens, it means that you are doing what is expected. Only that something must be missing.

  • @Jessf How’s your code that adds the line?

  • I asked the question.

  • When I try to change an existing registration, it does nothing, but when I try to insert a new registration and insert the item in the datagrid it gives me this error: "Additional information: Object reference not defined for an instance of an object."

  • So the problem has absolutely nothing to do with my solution. Let’s split up, okay? First thing, if you remove all the code from your method and do just var row = source.NewRow(); source.Rows.Add(row);. Works great?

  • It gives this error: "Additional information: Undefined object reference to an instance of an object."

  • Within the method, the value of source is as null?

  • Ahh boy! That’s it! var datatable = (Datatable)dataGridServicos.Datasource; Criarow(datatable);, it’s like this.

  • And what does that mean?

  • it sets datagrid as source

  • @Jessf No, this captures the Datagrid Datasource. You still haven’t answered my other question. souce is as null within the method?

  • I’m confused. It doesn’t pass any value to the source.

  • @He Jessf who? Who are you talking about?

  • The method. It only passes those two lines that I put up there.

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.