Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Summary
Great tips and tricks for .NET developers.
Description

Un-hooking an Event Handler
Sometimes you have the need to temporarily un-hook an Event Handler. A good example might be a SelectedIndexChanged event of a ComboBox that is dynamically filled or databound. You probably don't want the code in your eventhandler to be run in such a case, when you're in the middle of making programmatic changes to the ComboBox.

Hooking up the EventHandler in the first place is easy enough:

C#
this.MyCombo.SelectedIndexChanged += new EventHandler(MyCombo_SelectedIndexChange);

private void MyCombo_SelectedIndexChange(object sender, EventArgs e)
{
    // Add event handler code here
}

VB
AddHandler me.MyCombo.SelectedIndexChanged, AddressOf MyCombo_SelectedIndexChange

Private Sub MyCombo_SelectedIndexChange(ByVal sender As Object, ByVal e As EventArgs)
   ' Add event handler code here.
End Sub

Un-hooking the EventHandler is just as easy, although perhaps not as intuitive in C# (which uses -= instead of the +=) as it is in VB (using RemoveHandler instead of AddHandler).

C#
this.MyCombo.SelectedIndexChanged -= new EventHandler(MyCombo_SelectedIndexChange);

VB
RemoveHandler me.MyCombo.SelectedIndexChanged, AddressOf MyCombo_SelectedIndexChange

from a solution provided by Bonnie Berent in Message #1146017

Checking For Dirty Columns
How can you determine which columns in a changed row in a DataTable have changed? The DataRowVersion can tell you:
for (int i=0; i < MyTable.Columns.Count; i++)
{
    if (!MyTable.Rows[RowNum][i, DataRowVersion.Default].Equals(MyTable.Rows[RowNum][i,DataRowVersion.Original]))
       // column has changed
}

from a solution provided by John Baird in Message #1222494

Dynamically Define a WebForm's GridView
Here's how to dynamically change the columns shown in a grid on a webpage. Use a single GridView and dynamically change its content as your data changes:
protected void btnShowCompany_Click(object sender, EventArgs e)
{
  myGridView.DataSource = MyCustomerTable;
  myGridView.AutoGenerateColumns = false;
  myGridView.Columns.Clear();
  AddField(myGridView,"customerID","customer\'s ID");
  AddField(myGridView, "companyName", "Company Name");
  myGridView.DataBind();
}
protected void btnShowContact_Click(object sender, EventArgs e)
{
  myGridView.DataSource = MyContactTable;
  myGridView.AutoGenerateColumns = false;
  myGridView.Columns.Clear();
  AddField(myGridView,"customerID","customer\'s ID");
  AddField(myGridView, "contactName", "Contact Name");
  AddField(myGridView, "contactTitle", "Contact Title");
  myGridView.DataBind();
}

private void AddField(GridView gv, string dataField, string caption)
{
      BoundField bf = new BoundField();
      bf.DataField = dataField;
      bf.HeaderText = caption;
      gv.Columns.Add(bf);
}

from a solution provided by Çetin Basöz in Message #1134680

Programmatically Selecting a Tab Page
Use the SelectedTab property of the TabControl rather than the .Select() method of the TabPage:
public class Form1 : Form
{
	public Form1()
	{
		this.tabControl = new System.Windows.Forms.TabControl();
		this.tabPage1 = new System.Windows.Forms.TabPage();
		this.tabPage2 = new System.Windows.Forms.TabPage();
		this.button = new System.Windows.Forms.Button();
		this.tabControl.Controls.Add(this.tabPage1);
		this.tabControl.Controls.Add(this.tabPage2);
		this.tabPage1.Controls.Add(this.button);
		this.button.Location = new System.Drawing.Point(25, 39);
		this.button.Text = "activate second page";
		this.button.Click += new System.EventHandler(this.button_Click);
		this.Controls.Add(this.tabControl1);
	}
	private System.Windows.Forms.TabControl tabControl;
	private System.Windows.Forms.TabPage tabPage1;
	private System.Windows.Forms.TabPage tabPage2;
	private System.Windows.Forms.Button button;
	
	private void button_Click(object sender, EventArgs e)
	{
		this.tabControl.SelectedTab = this.tabPage2;
	}
}

from a solution provided by Çetin Basöz in Message #1170940

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers