I have populated datagridview using the following method and I have added button column too:
private void populatedatagridview()
{
categorieslist();
productgridview.RowTemplate.Height = 130;
var productsbycount = axe.products.GroupBy(x => x.product_Id).Select(a => new
{
productid = a.Key,
productnam = a.FirstOrDefault().product_Name,
productimage = a.FirstOrDefault().product_Image,
productdescr = a.FirstOrDefault().product_Description,
stockavailable = a.LongCount(),
productprice = a.FirstOrDefault().product_Price
});
productbindingsource.DataSource = productsbycount;
productgridview.DataSource = productbindingsource;
DataGridViewButtonColumn column = new DataGridViewButtonColumn();
productgridview.Columns.Add(column);
column.FlatStyle = FlatStyle.System;
column.DefaultCellStyle.ForeColor = Color.ForestGreen;
column.DefaultCellStyle.Padding = new Padding(10,48,10,48);
column.Text = "Buy";
column.HeaderText = "Buy";
column.UseColumnTextForButtonValue = true;
column.Name = "btnbuy";
productgridview.Columns[0].Visible = false;
for (int i = 0 ; i < productgridview.Columns.Count; i++)
if (productgridview.Columns[i] is DataGridViewImageColumn)
{
((DataGridViewImageColumn)productgridview.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
break;
}
}
My problem is whenever I call this function, the datagrid view adds another button column so it’s automatically increasing the number of button columns.
And I have a combobox I want to populate the datagrid view depends on the combobox text. At that time I also have to call this function.
Unfortunately, it automatically added button column again and again whenever this function is called. Would anyone please help on this?
Change this block:
to: