Forum  General Visual ...  How do I...?  How to change column name after binding datagridview ?
Previous Previous
 
Next Next
New Post 6/14/2012 9:28 AM
  Ewans
600 posts
1st Level Poster




Re: How to change column name after binding datagridview ? 

Hi

This sounds like you are poulating the DGV during component initialization ie in the procedure right at the start of each part that contains the line

InitiailizeComponent();

You must not populate your DGV in this section of code as it will not work as expected.

Hope this helps

Ewan

 
New Post 6/14/2012 10:19 AM
  ckish
12 posts
No Ranking


Re: How to change column name after binding datagridview ? 

Personally, I call a method that binds the grid in the form_load event and formats the headers at the same time. See below snippet:

private void GetDiscounts(object sender, EventArgs e)
{
 try
 {
  Discounts = new BLL.Discounts().GetDiscounts(this.Pricebook.Id).DefaultView;

  if (Discounts.Count > 0)
  {
   DiscountsGridView.DataSource = Discounts;

   DiscountsGridView.Columns["Id"].Visible = false;

   DiscountsGridView.Columns["Name"].HeaderText = " Name";
   DiscountsGridView.Columns["Description"].HeaderText = " Description";
   DiscountsGridView.Columns["StartDate"].HeaderText = " Start Date";
   DiscountsGridView.Columns["EndDate"].HeaderText = " End Date";
   DiscountsGridView.Columns["Expires"].HeaderText = " Expires";
   DiscountsGridView.Columns["Active"].HeaderText = " Active";

   DiscountsGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
   DiscountsGridView.ClearSelection();
  }
  else
  {
   DiscountsGridView.DataSource = "";
  }
 }
 catch (Exception ex)
 {
  MessageBox.Show(string.Format("Error Getting Discounts: {0}", ex.Message), "ERP", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
}

 
New Post 6/18/2012 3:56 PM
  palli
14323 posts
1st Level Poster




Re: How to change column name after binding datagridview ? 

 Hi now77ak,

What you are probably missing is after step 3, call DataGridView.Update(); and it should update the grid.

If you want to make sure your changes are made after the DataGridView is fully bound to the datasource, you can use DataGridView.DataBindingCompete event, which is fired when the data has been bound and the columns have been created. You can do your magic there and then call DataGridView.Update().

Hope this helps,

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
Previous Previous
 
Next Next
  Forum  General Visual ...  How do I...?  How to change column name after binding datagridview ?
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS