Adding multiple Gridview lines

Asked

Viewed 1,397 times

1

I’m trying to include several lines in a Gridview, but it doesn’t add the lines, it always replaces the first.

I would like to know how to make it add several lines, and not replacing the first.

Follow the code of how I’m currently doing:

dt.Columns.Add("ID");
dt.Columns.Add("Nome");
dt.Columns.Add("Quantidade");
dt.Columns.Add("Valor");
dt.Columns.Add("Desconto");
dt.Columns.Add("Valor Final");
dt.Columns.Add("Quitar", typeof(bool));
DataRow dataRow = dt.NewRow();
dataRow[0] = txtidprodutoAdd.Text;
dataRow[1] = cbProdutoAdd.SelectedItem;
dataRow[2] = UpQuantidade.Text;
dataRow[3] = txtValorAdd.Text;
dataRow[4] = txtDescontoAdd.Text;
dataRow[5] = txtValorFinalAdd.Text;
dataRow[6] = true;
dt.Rows.Add(dataRow);
GridView5.DataSource = dt;
GridView5.DataBind();
  • Okay, on the question: where are you trying to insert a second line? In the code only one line is being created.

  • It is the same code, I have a button that opens a modal, through filling the data, is included in gridView. And it’s replacing the line, I use this code, it’s on the button.

  • I’m writing an answer.

2 answers

1

The problem is that you are treating a web application as if it were a normal desktop application. It is necessary to note that a web application is always stateless, that is, no state is kept between a request and another, they are independent.

So whenever the request is received, a new DataTable is created regardless of whether in the previous request it has already been created or not. There are a few ways to deal with this and, most likely there is already a mechanism of the Webforms itself, since the components of the graphical interface maintain a kind of state between a request and another (i.e.: TextBoxes keep the entered values).

As I do not know Webforms, I do not know if my solution is the best, but, by logic, it works. What should be done is to capture the DataSource of DataGridView and add a line in this DataSource.

DataTable dataTable = (DataTable) GridView5.DataSource;

DataRow dataRow = dt.NewRow();
dataRow[0] = 123;
dataRow[1] = "asd";
dataRow[2] = 123;
dataRow[3] = 50.0;
dataRow[4] = 0;
dataRow[5] = 50;
dataRow[6] = true;
dataTable.Rows.Add(dataRow);
  • I tried that way and it didn’t work, I’m trying to adapt the code, to see if it works.

  • Unfortunately this way also did not work. I continue with the same problem.

  • When I have a while I try something, @marianac_costa

  • I managed to solve @jbueno, thank you.

0


I managed to solve, I did it this way:

 if (Session["dt1"] != null)
            {
                dt1 = (DataTable)Session["dt1"];
            }
            else
            {
                dt1.Columns.Add("ID");
                dt1.Columns.Add("Nome");
                dt1.Columns.Add("Quantidade");
                dt1.Columns.Add("Valor");
                dt1.Columns.Add("Desconto");
                dt1.Columns.Add("Valor Final");
                dt1.Columns.Add("Quitar", typeof(bool));

            }



            dr1 = dt1.NewRow();
            dr1["ID"] = txtidprodutoAdd.Text;
            dr1["Nome"] = cbProdutoAdd.SelectedItem;
            dr1["Quantidade"] = UpQuantidade.Text;
            dr1["Valor"] = txtValorAdd.Text;
            dr1["Desconto"] = txtDescontoAdd.Text;
            dr1["Valor Final"] = txtValorFinalAdd.Text;
            dr1["Quitar"] = true;
            dt1.Rows.Add(dr1);
            GridView5.DataSource = dt1;
            GridView5.DataBind();
            Session["dt1"] = dt1;

Browser other questions tagged

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