开发者

Amount of button column increasing in datagridview

开发者 https://www.devze.com 2023-04-01 01:02 出处:网络
I have populated datagridview using the following method and I have added button column too: private void populatedatagridview()

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,
        prod开发者_开发问答uctimage = 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:

        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";

to:

if (productgridview.Columns["btnbuy"] == null)
{
        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";
}


You're getting the extra column every time your method is called because your method adds the column:

productgridview.Columns.Add(column); 

I would separate the code that formats your DataGridView from the code that data binds it. Run the code that formats the grid just once and run the code that data binds it when needed.

Your populatedatagridview method is really doing two things: querying a data source and binding the result of the query to your grid, and changing the appearance of your DataGridView by adding a column and setting the ImageLayout property of some columns.

I'd remove all the code that changes your grid's appearance into a new method and call that just once, perhaps in your Form Load. This leaves the populatedatagridview responsible only for populating your DataGridView.

0

精彩评论

暂无评论...
验证码 换一张
取 消