Search code examples
c#winformsbuttondatagridviewdatagridviewcolumn

adding button column to datagrid view causing problems


I have a datagridview binding with data coming from database that works fine. It is loaded with data when the form loads. As per client requirement I have added new button column to datagrid view by using the following code

private void form1_load
{
           var productsbycount = abc.products.GroupBy(x => x.product_Id).Select(a => new
        {
            productid = a.Key,
            prouctnam = 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 buttoncolumn = new DataGridViewButtonColumn();
        productgridview.Columns.Add(buttoncolumn);
        buttoncolumn.Text = "Buy";
        buttoncolumn.HeaderText = "Buy";
        buttoncolumn.UseColumnTextForButtonValue = true;
        buttoncolumn.Name = "btnbuy";
        productgridview.Columns[0].Visible = false;

}

when the form load its working fine.

but I am checking the conditions like if we select the any of the item in something like in list view the datagrid view is sorted according to the selected item ... for that I have done like this....

if (lstviewcategories.SelectedItems[0].Value.ToString() == "All")
{
    var productsbycount = abc.products.GroupBy(x => x.product_Id).Select(a => new
      {
          productid = a.Key,
          prouctnam = 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 buttoncolumn = new DataGridViewButtonColumn();
       productgridview.Columns.Add(buttoncolumn);
       buttoncolumn.Text = "Buy";
       buttoncolumn.HeaderText = "Buy";
       buttoncolumn.UseColumnTextForButtonValue = true;
       buttoncolumn.Name = "btnbuy";
       productgridview.Columns[0].Visible = false;
}
  • if i click on listview first item( "All") the datagrid view is working fine ....
  • if i click again on listview first item("All") the button column is appearing two times ....
  • if i click again on listview first item("All") the button column is appearing three times...

This is what happening for all items in listview.

My question is, is there any other way to add button column to datagrid view?

I have tried to add the button column in form1.cs[design] this will effect the actual columns in the datagridview. I am working in winforms with c# language. Can any one suggest any idea about these?

Suppose if I remove the below code in this loop

if (lstviewcategories.SelectedItems[0].Value.ToString() == "All")
{
}

The actual button column disappeared. Would any one please help on this.

MODIFIED CODE :

FIRST CONDITION : if (lstviewcategories.SelectedItems[0].Text.ToString() == CategoryType.Type2) {

                 var productsbycount = abc.products.GroupBy(x => x.product_Id).Where(a => a.FirstOrDefault().product_Price > 0 && a.FirstOrDefault().product_Price <= 1000)
                              .Select(a => new
                              {
                                  productid = a.Key,
                                  prouctnam = a.FirstOrDefault().product_Name,
                                  productimage = a.FirstOrDefault().product_Image,
                                  productdescr = a.FirstOrDefault().product_Description,
                                  stockavailable = a.LongCount(),
                                  productprice = a.FirstOrDefault().product_Price
                             });
                 productbindingsource.ResetBindings(false);
                 /*productbindingsource.DataSource = productsbycount;
                 productgridview.DataSource = productbindingsource;
                 DataGridViewButtonColumn buttoncolumn = new DataGridViewButtonColumn();
                 productgridview.Columns.Add(buttoncolumn);
                 buttoncolumn.Text = "Buy";
                 buttoncolumn.HeaderText = "Buy";
                 buttoncolumn.UseColumnTextForButtonValue = true;
                 buttoncolumn.Name = "btnbuy";
                 productgridview.Columns[0].Visible = false;*/


             }

Second condition :

           if (lstviewcategories.SelectedItems[0].Text.ToString() == CategoryType.Type3)
             {
                 var productsbycount = abc.products.GroupBy(x => x.product_Id).Where(a => a.FirstOrDefault().product_Price > 500 && a.FirstOrDefault().product_Price <= 1000)
                              .Select(a => new
                              {
                                  productid = a.Key,
                                  prouctnam = a.FirstOrDefault().product_Name,
                                  productimage = a.FirstOrDefault().product_Image,
                                  productdescr = a.FirstOrDefault().product_Description,
                                  stockavailable = a.LongCount(),
                                  productprice = a.FirstOrDefault().product_Price
                              });
                 productbindingsource.ResetBindings(false);
              /* productbindingsource.DataSource = productsbycount;
                 productgridview.DataSource = productbindingsource;
                 DataGridViewButtonColumn buttoncolumn = new DataGridViewButtonColumn();
                 productgridview.Columns.Add(buttoncolumn);
                 buttoncolumn.Text = "Buy";
                 buttoncolumn.HeaderText = "Buy";
                 buttoncolumn.UseColumnTextForButtonValue = true;
                 buttoncolumn.Name = "btnbuy";
                 productgridview.Columns[0].Visible = false;*/

             }

these tow conditions are not working ..


Solution

  • many thanks for your support ,I have solved my problem....like this... At the form load i have added the button column to datagrid view ......and when i am checking the conditions like this below..

    if (lstviewcategories.SelectedItems[0].Text.ToString() == CategoryType.Type2)
    

    I have just assigned like the below....

    productgridview.datasource = productsbycount.Where(a => a.FirstOrDefault().product_Price > 0 && a.FirstOrDefault().product_Price <= 1000)  
    

    Luckily it was working fine .......