Adding button column to datagrid view causing problems?

Firstly I would strongly recomend reading up on the DRY principle. To answer your question I would recomend using the refresh method of your BindingSource so that you can refresh the data in your datagridview without changing the datastructure. You should only do all of the following once on load, then refresh the data anywhere else unless you actually want to change the datatype e.g. The data is coming from a different source 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. Columns0. Visible = false Instead of doing this all again in the following code block just call productbindingsource.

ResetBindings(false) if (lstviewcategories. SelectedItems0.Value.ToString() == "All") { } UPDATE: Also instead of defining the var productsbycount in this code block, make it a class variable at the very top so that you can access it all your methods, that way when you refresh the data you will have overwritten the previous var productsbycount and it will work marvellously UPDATE2: ResetBindings() can be used at any point after you have changed the data but as I have already said, you need a class level variable (AKA putting var productsbycount at the top of the class) and in this example it would go after this productprice = a.FirstOrDefault(). Product_Price }) UPDATE3: I repeat we are here to help and give advice not write the entire code piece for you but as I am feeling nice =) I also repeat there appears to be a great deal of duplication here and as the code base grows this may cause you great pain!

Namespace Test { public partial class Form2 : Form { BindingSource productbindingsource; var productsbycount; public Form2() { InitializeComponent(); Load += new EventHandler(Form2_Load); } void Form2_Load(object sender, EventArgs e) { 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. Columns0. Visible = false; } private void methodNameHere() { productsbycount = abc.products.

GroupBy(x => x. Product_Id). Where(a => a.FirstOrDefault().

Product_Price > 0 && a.FirstOrDefault(). Product_Price 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); } } }.

Firstly I would strongly recomend reading up on the DRY principle. To answer your question I would recomend using the refresh method of your BindingSource so that you can refresh the data in your datagridview without changing the datastructure. You should only do all of the following once on load, then refresh the data anywhere else unless you actually want to change the datatype e.g. The data is coming from a different source; 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. Columns0. Visible = false; Instead of doing this all again in the following code block just call productbindingsource.

ResetBindings(false); if (lstviewcategories. SelectedItems0.Value.ToString() == "All") { } UPDATE: Also instead of defining the var productsbycount in this code block, make it a class variable at the very top so that you can access it all your methods, that way when you refresh the data you will have overwritten the previous var productsbycount and it will work marvellously. UPDATE2: 'ResetBindings() can be used at any point after you have changed the data but as I have already said, you need a class level variable (AKA putting var productsbycount at the top of the class) and in this example it would go after this; productprice = a.FirstOrDefault().

Product_Price }); UPDATE3: I repeat we are here to help and give advice not write the entire code piece for you but as I am feeling nice =) I also repeat there appears to be a great deal of duplication here and as the code base grows this may cause you great pain! ; namespace Test { public partial class Form2 : Form { BindingSource productbindingsource; var productsbycount; public Form2() { InitializeComponent(); Load += new EventHandler(Form2_Load); } void Form2_Load(object sender, EventArgs e) { 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. Columns0.

Visible = false; } private void methodNameHere() { productsbycount = abc.products. GroupBy(x => x. Product_Id).

Where(a => a.FirstOrDefault(). Product_Price > 0 && a.FirstOrDefault(). Product_Price 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); } } }.

Is it also refresh the button column or it only refreshes the data in that.. – pratapchandra Aug 25 at 13:22 Please re-phrase/expand the question, I am not sure what you mean – CodeBlend Aug 25 at 13:23 The ResetBindings() method - Causes a control bound to the BindingSource to reread all the items in the list and refresh their displayed values. – CodeBlend Aug 25 at 13:30 Did this help you with your problem, can you follow it? – CodeBlend Aug 25 at 13:56 where do I have to put productbindingsource.

ResetBindings(false); this one ... – pratapchandra Aug 25 at 14:04.

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. SelectedItems0.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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions