Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

DataGridView Custom Headers in VB.NET

, 9 Mar 2009 CPOL
How to give Custom Headers to your DataGridView
DGVHeaderColor.zip
DGVHeaderColor
DGVHeaderColor
DGVHeaderColor.exe
DGVHeaderColor.pdb
DGVHeaderColor.vbproj.user
DGVHeaderColor.vshost.exe
My Project
Application.myapp
Settings.settings
Resources
AquaBall Blue.png
Button Gray Stripe 01 050.png
DGVHeaderColor.suo
Docs
Gradient ColumnHeader_files
4BBCD906-6DAD-4443-8F0B-525208645B30.jpg
add.gif
AddTo_Blink.png
AddTo_Delicious.png
AddTo_Digg.png
AddTo_Facebook.png
AddTo_Furl.png
AddTo_Google.png
AddTo_Live.png
AddTo_MrWong.png
AddTo_Newsvine.png
AddTo_Reddit.png
AddTo_Simpy.png
AddTo_stumbleupon.png
AddTo_Technorati.png
AddTo_Yahoo.png
ajax-loader.gif
BE.gif
discuss.gif
forum_faq.gif
forum_newmsg.gif
forum_search.gif
GradientColumnheader.png
javaduke.jpg
LAMP.GIF
link.png
logo225x90.gif
mail.gif
member_sm.gif
MonthlyComp.png
msg_admin.gif
msg_answer.gif
msg_general.gif
msg_joke.gif
msg_news.gif
msg_question.gif
msg_rant.gif
pollcol.gif
print.gif
red.gif
report.gif
ServeHTML_data
developer_express_60Free-Controls-150x80.swf
ServeHTML_data_002
B3424356_data
crystal-160x600.swf
ServeHTML_data_003
Dundas_468x60_Chart.swf
ServeLinks_data
smartclient24.jpg
smiley_confused.gif
smiley_laugh.gif
smiley_OMG.gif
smiley_smile.gif
smiley_wink.gif
t.gif
thumbs_down.gif
thumbs_up.gif
t_002.gif
watchon.png
white.gif
/* Acknowledgments: Josh Heyer / David Stone for initial versions. Bradml for stripdown and package */
var keyMapping = [];
keyMapping['13'] = "ENTER";
keyMapping['37'] = "LEFT";
keyMapping['38'] = "UP";
keyMapping['39'] = "RIGHT";
keyMapping['40'] = "DOWN";
keyMapping['16'] = "SHIFT";
keyMapping['17'] = "CTRL";

var commandMapping = {
	Combinations: {
		"CTRL+ENTER": function() {Submit();},
		"CTRL+LEFT"	: function() {MoveToPost(false, false);},
		"CTRL+RIGHT": function() {MoveToPost(true, false);},
		"CTRL+UP"	: function() {MoveToPost(false, true);},
		"CTRL+DOWN"	: function() {MoveToPost(true, true);}
	}
};

document.onkeydown = function(e) {
	return shortcutEventHandler(e, commandMapping, keyMapping);
};

function getControlKeys(e) {
	var controls = "";
	if (e.ctrlKey)controls += "CTRL+";
	if (e.shiftKey)controls += "SHIFT+";
	if (e.altKey)controls += "ALT+";
	return controls;
}

// return: false == handled, true == keep looking
function shortcutEventHandler(KeyEvent, commandMapping, keyMapping) {
	if ( ! KeyEvent)
		KeyEvent = event;
	//Because IE knows better
	var eventKeyValue = (KeyEvent.keyCode) ? KeyEvent.keyCode: KeyEvent.charCode;
	var eventKeyName = "";
	
	if (keyMapping[eventKeyValue])
		eventKeyName = keyMapping[eventKeyValue];
	var controlValue = getControlKeys(KeyEvent);
	eventKeyName = controlValue + eventKeyName;

	if (commandMapping.Combinations[eventKeyName]) 	{
		commandMapping.Combinations[eventKeyName]();
		
		// Stop propogation if we've found it
		if ( KeyEvent.preventDefault )
			KeyEvent.preventDefault();
		else
			KeyEvent.returnValue = false;
		if ( KeyEvent.stopPropagation )
			KeyEvent.stopPropagation();
		else
			KeyEvent.cancelBubble = true;
		return false;
	} 
	return true;
}

function Submit() {
	var theForm = document.forms['aspnetForm'];
	if (!theForm) theForm = document.aspnetForm;
	if (theForm) theForm.submit();
}

function MoveToPost(next, thread) {
	if (Selected=="" || isNaN(Selected)) return;
	var ForumTable = document.getElementById("ForumTable");
	if (!ForumTable) return;
	var elems = getElementsByClass("MsgHd",ForumTable,"tr");
	if (!elems) return;
	
	var moveTo = null;
	var selectedId = parseInt(Selected);
			
	var selectedIndex = 0;
	var selectedThreadIndex = 0;
	for (var i=0; i<elems.length; ++i) 	{
		var id = elems[i].id.substring(1, elems[i].id.length-3);
		id = parseInt(id);
		
		if (IsThreadHead(elems[i]))
			selectedThreadIndex = i;
		
		if (id == selectedId) {
			selectedIndex = i;
			break;
		}
	}
	
	var nextId = null;
	for (var i=selectedIndex + (next ? 1 : -1); i<elems.length && i >= 0; next ? ++i : --i)	{
		if ((thread && IsThreadHead(elems[i])) || !thread) {
			nextId = elems[i].id.substring(1, elems[i].id.length-3);
			nextId = parseInt(nextId);
			break;
		}
	}
	
	if (nextId && nextId != parseInt(Selected)) {
		SwitchMessage(null,nextId);
		//EnsureMessageVisible(Selected, true);
	}
}

function IsThreadHead(elem) {
	return elem.className.indexOf("Rt") >= 0;
}

function EnsureMessageVisible(msgID, bShowTop) {
	var msgHeader = document.getElementById("F" + msgID + "_h0");
	var msgBody = document.getElementById("F" + msgID + "_h1");
	if (!msgBody || !msgHeader) return;

	// determine scroll position of top and bottom
	var scrollContainer = document.documentElement;
	var top = getRealPos(msgHeader, 'Top');
	var bottom = getRealPos(msgBody, 'Top') + msgBody.offsetHeight;

	// if not already visible, scroll to make it so
	var scrollTop = scrollContainer.scrollTop;
	if (scrollTop > top && !bShowTop)
		scrollTop = top - scrollContainer.clientHeight / 10;
	if (scrollTop + scrollContainer.clientHeight < bottom)
		scrollTop = bottom - scrollContainer.clientHeight;
	if (scrollTop > top && bShowTop)
		scrollTop = top - scrollContainer.clientHeight / 10;

	// apply corrections
	scrollContainer.scrollTop = scrollTop;
	//$(scrollContainer).stop().animate({'scrollTop' : scrollTop}, 400);
}

function getRealPos(i,which) {
	iPos = 0
	while (i!=null)  {
		iPos += i["offset" + which];
		i = i.offsetParent;
	}
	return iPos
}

function getElementsByClass(className,node,tag) {
  var found = new Array();
  if (node == null)node = document;
  if (tag == null)tag = '*';
  var elms = node.getElementsByTagName(tag);
  var length = elms.length;
  var pattern = new RegExp("(^|\\s)"+className+"(\\s|$)");
  for (i=0, j=0; i<length; i++) {
	 if (pattern.test(elms[i].className) ) {
		found[j] = elms[i];
		j++;
	 }
  }
  return found;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Luca Crisi, MCP
Software Developer (Senior)
Italy Italy
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 9 Mar 2009
Article Copyright 2009 by Luca Crisi, MCP
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid