2

I am currently working on website/blog/shop in asp.net. I have problems with shop. After I successfully add items to basket and go to page that is supposed to display items via GridView buttons column is not appearing at all and buttons are showing at the bottom of the page.

I dynamically created columns and rows in c#

Grid() function is called on page load. Other columns and rows string type are working properly in GridView.

I have tried googling this problem but I found nothing.

    protected void Grid()
    {
        if (Korpetina.proizvodi.Count > 0) //Items that are in basket
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("Naziv proizvoda", typeof(string));
            dt.Columns.Add("Komada", typeof(double));
            dt.Columns.Add("Ukloni", typeof(Button)); // this is button type column that isn't appearing, other work just fine
            dt.Columns.Add("Cena", typeof(string));
            for (int i = 0; i < Korpetina.proizvodi.Count; i++) //going through items in the basket
            {

                DataRow dr = dt.NewRow();//assiging values to the rows

                dr["Naziv proizvoda"] = Korpetina.proizvodi[i];
                dr["Komada"] = "1";

                Button b = new Button //creating buttons
                {
                    ID = i.ToString(),
                    Text = "Ukloni",

                    Height = 13,
                    Width = 30
                };
                b.Click += new EventHandler(Click);
                form1.Controls.Add(b); //adding to aspnet form control
                dr["Ukloni"] = b;      //assigning button to the button row        
                dr["Cena"] = Korpetina.cene[i];
                dt.Rows.Add(dr);

            }

            DataRow d = dt.NewRow();//adding another row for total price, also works just fine
            d["Cena"] = Korpetina.ukupno;
            dt.Rows.Add(d);
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
    }

There is supposed to be button in each row of the gridview in button type column.

This is how it actually looks : http://prntscr.com/otjs4b 4 buttons for 4 rows in gridview What am I doing wrong here?

  • This may shed some light on your problem, add it to the gridview not the datatable, https://stackoverflow.com/questions/21191950/how-to-add-a-button-to-a-column-in-the-datagridview – David Yenglin Aug 16 '19 at 15:25
  • This article is related to Win Forms and DataGridView , I am working with asp.net and aspnet GridView. I am unable to make it work with this article. – WickedyWick Aug 16 '19 at 15:34
  • The key part is they arent in the gridview, that is why they are down at the bottom of the screen. Try adding the actual button controls to the gridview and not the datatable. The technology isnt important, the key concepts in that other answer are. – David Yenglin Aug 19 '19 at 11:25

0 Answers0