Files
2015-05-20 17:48:28 -07:00

602 lines
35 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><link rel="stylesheet" type="text/css" href="description/Combined.css,0:HeaderFooterSprite,0:Header.NonMtps,1:LinkList;/Areas/Centers/Themes/StandardDevCenter/Content:0,/Areas/Epx/Themes/Base/Content:1&amp;amp;hashKey=C9973DA951AE6202C9B348379A1BE49D" xmlns="http://www.w3.org/1999/xhtml" />
<link type="text/css" rel="stylesheet" xmlns="http://www.w3.org/1999/xhtml" />
<link rel="stylesheet" type="text/css" href="description/1c1e217c-1475-400e-91a7-b2be1bd6efa0Combined.css,0:HeaderFooterSprite,0:Footer.NonMtps,1:LinkList;/Areas/Centers/Themes/StandardDevCenter/Content:0,/Areas/Epx/Themes/Base/Content:1&amp;amp;hashKey=F576C687BC536B84D6E5B3246EE39B49" xmlns="http://www.w3.org/1999/xhtml" />
<link type="text/css" rel="stylesheet" xmlns="http://www.w3.org/1999/xhtml" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Windows Forms DataGridView demo (VBWinFormDataGridView)</title>
<link href="description/Galleries.css" type="text/css" rel="Stylesheet" /><link href="description/Layout.css" type="text/css" rel="Stylesheet" /><link href="description/Brand.css" type="text/css" rel="Stylesheet" />
<link href="description/iframedescription.css" rel="Stylesheet" type="text/css" />
<script src="description/offline.js" type="text/javascript"></script>
<style type="text/css">
#projectInfo {
overflow: auto;
}
#longDesc {
clear:both;
margin: 25px 0 10px 0;
}
#SampleIndexList{
margin-left: 15px;
}
</style>
</head>
<body>
<div id="offlineDescription">
<h1>Windows Forms DataGridView demo (VBWinFormDataGridView)</h1>
<br/>
<div id="projectInfo">
<div class="section">
<div class="itemBarLong tagsContainer">
<label for="Technologies">Technologies</label>
<div id="Technologies">
Windows Forms
</div>
</div>
<div class="itemBarLong tagsContainer">
<label for="Topics">Topics</label>
<div id="Topics">
Data Binding, DataGridView
</div>
</div>
<div class="itemBarLong">
<label for="Platforms">Platforms</label>
<div id="Platforms">
Desktop
</div>
</div>
<div class="itemBarLong">
<label for="Requirements">Requirements</label>
<div id="Requirements">
</div>
</div>
<div class="itemBar">
<label for="LastUpdated">Primary language</label>
<div id="LastUpdated">en-US</div>
</div>
<div class="itemBar">
<label for="LastUpdated">Updated</label>
<div id="LastUpdated">3/4/2012</div>
</div>
<div class="itemBarLong">
<label for="License">License</label>
<div id="License">
<a href="license.rtf">MS-LPL</a></div>
</div>
<div class="itemBar">
<div class="viewonlinecont">
<a data-link="online" href="http://code.msdn.microsoft.com/VBWinFormDataGridView-961db2a6">View this sample online</a>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function initializePage() {
var otherTabClass = 'otherTab';
var hiddenPreClass = 'hidden';
var htmlDecode = function(encodedData) {
var decodedData = "";
if (encodedData) {
var div = document.createElement('div');
div.innerHTML = encodedData;
decodedData = div.firstChild.nodeValue.replace( /\\r\\n/ig , '\r\n');
}
return decodedData;
};
Galleries.iterateElem(Galleries.findElem(null, 'div', 'scriptcode'), function (index, scriptBlock) {
var titleElem = Galleries.findElem(scriptBlock, 'div', 'title')[0];
var labelElems = Galleries.findElem(titleElem, 'span');
if (labelElems.length == 0) {
labelElems = titleElem;
}
var languageSpans = Galleries.findElem(scriptBlock, 'span', 'hidden');
var pres = Galleries.findElem(scriptBlock, 'pre');
if (languageSpans.length > 0 && pres.length > 1) {
Galleries.iterateElem(labelElems, function(index, elem) {
var codePre = pres[index];
var labelSpan = elem;
var languageSpan = languageSpans[index];
elem.code = codePre.innerHTML.replace( /(\r(\n)?)|((\r)?\n)/ig , '\\r\\n');
codePre.className = codePre.className.replace(hiddenPreClass, '');
languageSpan.parentNode.removeChild(languageSpan);
});
pres = Galleries.findElem(scriptBlock, 'pre');
Galleries.iterateElem(labelElems, function(index, elem) {
var codePre = pres[index];
var labelSpan = elem;
if (index == 0) {
scriptBlock.activeTab = 0;
}
else {
labelSpan.className += otherTabClass;
codePre.className += hiddenPreClass;
}
Galleries.attachEventHandler(labelSpan, 'click', function(e) {
var activeTab = scriptBlock.activeTab;
labelElems[activeTab].className += otherTabClass;
pres[activeTab].className += hiddenPreClass;
codePre.className = codePre.className.replace(hiddenPreClass, '');
labelSpan.className = labelSpan.className.replace(otherTabClass, '');
scriptBlock.activeTab = index;
});
});
var preview = Galleries.findElem(scriptBlock, 'div', 'preview');
if (preview.length == 0) {
preview.push(pres[pres.length - 1]);
}
Galleries.iterateElem(preview, function(index, elem) {
elem.parentNode.removeChild(elem);
});
if (window.clipboardData && clipboardData.setData) {
var copyLink = document.createElement('a');
copyLink.href = 'javascript:void(0);';
copyLink.className = 'copyCode';
copyLink.innerHTML = 'Copy code';
Galleries.attachEventHandler(copyLink, 'click', function (e) {
clipboardData.setData("Text", htmlDecode(labelElems[scriptBlock.activeTab].code));
return false;
});
scriptBlock.insertBefore(copyLink, scriptBlock.childNodes[0]);
}
}
});
}
Galleries.onWindowLoad(function(){
initializePage();
});
</script>
<div id="longDesc">
<h1><span style="font-family:������">WINDOWS FORMS APPLICATION</span> (<span style="font-family:������">VBWinFormDataGridView</span>)</h1>
<h2>Introduction</h2>
<p class="MsoNormal"><span style="">There are five samples in this solution to demonstrate DataGridView:
</span></p>
<p class="MsoListParagraphCxSpFirst" style=""><span style=""><span style="">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>CustomDataGridViewColumn<span style="">:</span></p>
<p class="MsoListParagraphCxSpMiddle">This sample demonstrates how to create a custom DataGridView column<span style="">.
</span></p>
<p class="MsoListParagraphCxSpMiddle" style=""><span style=""><span style="">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>DataGridViewPaging<span style="">:</span></p>
<p class="MsoListParagraphCxSpMiddle">This sample demonstrates how to page data in the<span style="">&nbsp;
</span>DataGridView control<span style="">. </span></p>
<p class="MsoListParagraphCxSpMiddle" style=""><span style=""><span style="">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>EditingControlHosting<span style="">:</span></p>
<p class="MsoListParagraphCxSpMiddle">This sample demonstrates how to host a control in the current DataGridViewCell<span style="">&nbsp;
</span>for editing.</p>
<p class="MsoListParagraphCxSpMiddle" style=""><span style=""><span style="">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>JustInTimeDataLoading<span style="">:</span></p>
<p class="MsoListParagraphCxSpMiddle">If you are working with a very large table in a remote database, for example, you might want to avoid startup delays by retrieving only the data that is necessary for display and retrieving additional data only when the
user scrolls<span style="">&nbsp; </span>new rows into view. If the client computers running your application have a<span style="">&nbsp;
</span>limited amount of memory available for storing data, you might also want to<span style="">&nbsp;
</span>discard unused data when retrieving new values from the database.</p>
<p class="MsoListParagraphCxSpMiddle">This sample demonstrates how to use virtual mode in the DataGridView control<span style="">&nbsp;
</span>with a data cache that loads data from a server only when it is needed.<span style="">&nbsp;
</span>This kind of data loading is called &quot;Just-in-time data loading&quot;.</p>
<p class="MsoListParagraphCxSpMiddle" style=""><span style=""><span style="">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>MultipleLayeredColumnHeader<span style="">:</span></p>
<p class="MsoListParagraphCxSpLast">This sample demonstrates how to display multiple layer column headers on the DataGridView contorl.<span style="">
</span></p>
<h2>Running the Sample</h2>
<p class="MsoListParagraph" style=""><span style=""><span style="">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>CustomDataGridViewColumn.</p>
<p class="MsoNormal"><span style=""><img src="description/image.png" alt="" width="576" height="407" align="middle">
</span><span style=""></span></p>
<p class="MsoListParagraph" style=""><span style=""><span style="">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>DataGridViewPaging<span style="">.</span></p>
<p class="MsoNormal"><span style=""><img src="description/4c4d2c39-e583-48a8-95c7-13f2534db7e5image.png" alt="" width="576" height="512" align="middle">
</span></p>
<p class="MsoListParagraph" style=""><span style=""><span style="">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><span style="">EditingControlHosting. </span></p>
<p class="MsoNormal"><span style=""><img src="description/e91a529a-733b-4283-a54e-e28c15880dc6image.png" alt="" width="576" height="428" align="middle">
</span><span style=""></span></p>
<p class="MsoListParagraph" style=""><span style=""><span style="">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>JustInTimeDataLoading<span style="">. </span></p>
<p class="MsoNormal"><span style=""><img src="description/12c98344-11ba-4fbc-84f9-8e78d71d1afeimage.png" alt="" width="576" height="460" align="middle">
</span><span style=""></span></p>
<p class="MsoListParagraph" style=""><span style=""><span style="">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>MultipleLayeredColumnHeader<span style="">.</span></p>
<p class="MsoNormal"><span style=""><img src="description/a89925e1-4a65-4f1f-9f5a-8e70ce5146b4image.png" alt="" width="576" height="431" align="middle">
</span></p>
<p class="MsoNormal">. </p>
<h2>Using the Code</h2>
<p class="MsoListParagraph" style=""><span style=""><span style="">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>CustomDataGridViewColumn<span style="">. </span></p>
<p class="MsoNormal" style="margin-left:36.0pt">There're six standard DataGridViewColumn types for use as follows:</p>
<p class="MsoNormal" style="margin-left:72.0pt"><span style=""><img src="description/f1d00a92-af6e-43ae-9c09-84b019f2c544image.png" alt="" width="433" height="407" align="middle">
</span><span style="background:#D9D9D9"></span></p>
<p class="MsoNormal" style="margin-left:36.0pt">However, developers may want to use a different control for editing on the column,<span style="">
</span>e.g. MarkedTextBox, DateTimePicker etc. This feature can be achieved in two ways:</p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="">A</span>. Create a custom DataGridViewColumn;
</p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="">&nbsp;&nbsp; </span>
The code in this CustomDataGridViewColumn sample demonstrates how to do this;<span style="">
</span></p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="">B</span>. Place the editing control on the current cell when editing begins, and hide the editing control when the editing completes. For the details of this<span style="">
</span>approach, please refer to the <span style=""><span style="">&nbsp;&nbsp;</span></span>EditingControlHosting sample.<span style="">
</span></p>
<p class="MsoListParagraph" style=""><span style=""><span style="">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>DataGridViewPaging<span style=""> </span></p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="">A. Get total count of the rows in the table;
</span></p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="">B. Calculate total count of pages;
</span></p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="">C. Load each page on demand;
</span></p>
<p class="MsoListParagraphCxSpFirst" style=""><span style=""><span style="">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>EditingControlHosting<span style=""> </span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">A. Create an instance of the editing control, in this sample the editing control is MaskedTextBox.<span style="">&nbsp;&nbsp;&nbsp;
</span></span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">B. Specify a mask for the MaskedTextBox and add the MaskedTextBox to the control collection of the DataGridView;<span style="">&nbsp;&nbsp;
</span></span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">C. Hide the MaskedTextBox;
</span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">D. Handle the CellBeginEdit event to show the MaskedTextBox on the current editing cell;<span style="">&nbsp;&nbsp;
</span></span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">E. Handle the CellEndEdit event to hide the MaskedTextBox when editing completes;
</span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">F. Handle the Scroll event to adjust the location of the MaskedTextBox as it is showing when scrolling the DataGridView;
</span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">G. Handle the EditingControlShowing event to pass the focus to the MaskedTextBox when begin editing with keystrokes;
</span></p>
<p class="MsoListParagraphCxSpMiddle" style=""><span style=""><span style="">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>JustInTimeDataLoading</p>
<p class="MsoListParagraphCxSpLast"><span style="">A</span>.<span style="">&nbsp;
</span>Enable VirtualMode on the DataGridView control by setting the VirtualMode property to true:<span style="">&nbsp;&nbsp;&nbsp;
</span><span style=""></span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
' Enable VirtualMode on the DataGridView
Me.dataGridView1.VirtualMode = True
</pre>
<pre id="codePreview" class="vb">
' Enable VirtualMode on the DataGridView
Me.dataGridView1.VirtualMode = True
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraphCxSpFirst"><span style=""></span></p>
<p class="MsoListParagraphCxSpMiddle"><span style="">B</span>.<span style="">&nbsp;
</span>Add columns to the DataGridView according to the data in the database;<span style="">
</span></p>
<p class="MsoListParagraphCxSpLast"><span style="">C</span>.<span style="">&nbsp;
</span>Retrieve the row count of the data in the database and set the RowCount<span style="">&nbsp;
</span>property for the DataGridView;<span style="">&nbsp;&nbsp;&nbsp; </span><span style=""></span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
' Handle the CellValueNeeded event to retrieve the requested cell value
' from the data store or the Customer object currently in edit.
' This event occurs whenever the DataGridView control needs to paint a cell.
' Create a DataRetriever and use it to create a Cache object
' and to initialize the DataGridView columns and rows.
Try
Dim retriever As DataRetriever = New DataRetriever(connectionString, table)
memoryCache = New Cache(retriever, 16)
For Each column As DataColumn In retriever.Columns
dataGridView1.Columns.Add(column.ColumnName, column.ColumnName)
Next
Me.dataGridView1.RowCount = retriever.RowCount
Catch ex As Exception
MessageBox.Show(&quot;Connection could not be established. &quot; &#43; _
&quot;Verify that the connection string is valid.&quot;)
Application.Exit()
End Try
</pre>
<pre id="codePreview" class="vb">
' Handle the CellValueNeeded event to retrieve the requested cell value
' from the data store or the Customer object currently in edit.
' This event occurs whenever the DataGridView control needs to paint a cell.
' Create a DataRetriever and use it to create a Cache object
' and to initialize the DataGridView columns and rows.
Try
Dim retriever As DataRetriever = New DataRetriever(connectionString, table)
memoryCache = New Cache(retriever, 16)
For Each column As DataColumn In retriever.Columns
dataGridView1.Columns.Add(column.ColumnName, column.ColumnName)
Next
Me.dataGridView1.RowCount = retriever.RowCount
Catch ex As Exception
MessageBox.Show(&quot;Connection could not be established. &quot; &#43; _
&quot;Verify that the connection string is valid.&quot;)
Application.Exit()
End Try
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraphCxSpFirst"><span style=""></span></p>
<p class="MsoListParagraphCxSpLast"><span style="">D</span>.<span style="">&nbsp;
</span>Handle the CellValueNeeded event to retrieve the requested cell value from the data store or the Customer object currently in edit.<span style="">
</span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
Private Sub dataGridView1_CellValueNeeded(ByVal sender As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Object.aspx" title="Auto generated link to System.Object">System.Object</a>, ByVal e As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Windows.Forms.DataGridViewCellValueEventArgs.aspx" title="Auto generated link to System.Windows.Forms.DataGridViewCellValueEventArgs">System.Windows.Forms.DataGridViewCellValueEventArgs</a>) Handles dataGridView1.CellValueNeeded
e.Value = memoryCache.RetrieveElement(e.RowIndex, e.ColumnIndex)
End Sub
</pre>
<pre id="codePreview" class="vb">
Private Sub dataGridView1_CellValueNeeded(ByVal sender As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Object.aspx" title="Auto generated link to System.Object">System.Object</a>, ByVal e As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Windows.Forms.DataGridViewCellValueEventArgs.aspx" title="Auto generated link to System.Windows.Forms.DataGridViewCellValueEventArgs">System.Windows.Forms.DataGridViewCellValueEventArgs</a>) Handles dataGridView1.CellValueNeeded
e.Value = memoryCache.RetrieveElement(e.RowIndex, e.ColumnIndex)
End Sub
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraphCxSpFirst"><span style=""></span></p>
<p class="MsoListParagraphCxSpMiddle" style=""><span style=""><span style="">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span>MultipleLayeredColumnHeader</p>
<p class="MsoListParagraphCxSpLast"><span style="">A</span>.<span style="">&nbsp;
</span>Enable resizing on the column headers by setting the ColumnHeadersHeightSizeMode property as follows:<span style="">
</span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
' Enable resizing on the column headers
Me.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
</pre>
<pre id="codePreview" class="vb">
' Enable resizing on the column headers
Me.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraphCxSpFirst"><span style=""></span></p>
<p class="MsoListParagraphCxSpLast"><span style="">B</span>.<span style="">&nbsp;
</span>Adjust the height for the column headers to make it wide enough for two layers;<span style="">
</span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
' Adjust the height for the column headers
Me.dataGridView1.ColumnHeadersHeight = Me.dataGridView1.ColumnHeadersHeight * 2
</pre>
<pre id="codePreview" class="vb">
' Adjust the height for the column headers
Me.dataGridView1.ColumnHeadersHeight = Me.dataGridView1.ColumnHeadersHeight * 2
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraphCxSpFirst"><span style=""></span></p>
<p class="MsoListParagraphCxSpLast"><span style="">C</span>.<span style="">&nbsp;
</span>Adjust the text alignment on the column headers to make the text display at the center of the bottom;<span style="">&nbsp;&nbsp;&nbsp;
</span><span style=""></span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
' Adjust the text alignment on the column headers to make the text display
' at the center of the bottom
Me.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
' Handle the CellPainting event to draw text for each header cell
</pre>
<pre id="codePreview" class="vb">
' Adjust the text alignment on the column headers to make the text display
' at the center of the bottom
Me.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
' Handle the CellPainting event to draw text for each header cell
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraphCxSpFirst"><span style=""></span></p>
<p class="MsoListParagraphCxSpLast"><span style="">D</span>.<span style="">&nbsp;
</span>Handle the DataGridView.CellPainting event to draw text for each header<span style="">&nbsp;
</span>cell;<span style=""> </span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
Private Sub dataGridView1_CellPainting(ByVal sender As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Object.aspx" title="Auto generated link to System.Object">System.Object</a>, ByVal e As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Windows.Forms.DataGridViewCellPaintingEventArgs.aspx" title="Auto generated link to System.Windows.Forms.DataGridViewCellPaintingEventArgs">System.Windows.Forms.DataGridViewCellPaintingEventArgs</a>) Handles dataGridView1.CellPainting
If e.RowIndex = -1 AndAlso e.ColumnIndex &gt; -1 Then
e.PaintBackground(e.CellBounds, False)
Dim r2 As Rectangle = e.CellBounds
r2.Y &#43;= e.CellBounds.Height / 2
r2.Height = e.CellBounds.Height / 2
e.PaintContent(r2)
e.Handled = True
End If
End Sub
</pre>
<pre id="codePreview" class="vb">
Private Sub dataGridView1_CellPainting(ByVal sender As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Object.aspx" title="Auto generated link to System.Object">System.Object</a>, ByVal e As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Windows.Forms.DataGridViewCellPaintingEventArgs.aspx" title="Auto generated link to System.Windows.Forms.DataGridViewCellPaintingEventArgs">System.Windows.Forms.DataGridViewCellPaintingEventArgs</a>) Handles dataGridView1.CellPainting
If e.RowIndex = -1 AndAlso e.ColumnIndex &gt; -1 Then
e.PaintBackground(e.CellBounds, False)
Dim r2 As Rectangle = e.CellBounds
r2.Y &#43;= e.CellBounds.Height / 2
r2.Height = e.CellBounds.Height / 2
e.PaintContent(r2)
e.Handled = True
End If
End Sub
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraphCxSpFirst"><span style=""></span></p>
<p class="MsoListParagraphCxSpLast"><span style="">E</span>.<span style="">&nbsp;
</span>Handle the DataGridView.Paint event to draw &quot;merged&quot; header cells;<span style="">
</span></p>
<div class="scriptcode">
<div class="pluginEditHolder" pluginCommand="mceScriptCode">
<div class="title"><span>VB</span></div>
<div class="pluginLinkHolder"><span class="pluginEditHolderLink">Edit</span>|<span class="pluginRemoveHolderLink">Remove</span>
</div>
<span class="hidden">vb</span>
<pre class="hidden">
Private Sub dataGridView1_Paint(ByVal sender As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Object.aspx" title="Auto generated link to System.Object">System.Object</a>, ByVal e As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Windows.Forms.PaintEventArgs.aspx" title="Auto generated link to System.Windows.Forms.PaintEventArgs">System.Windows.Forms.PaintEventArgs</a>) Handles dataGridView1.Paint
' Data for the &quot;merged&quot; header cells
Dim monthes As String() = {&quot;January&quot;, &quot;February&quot;, &quot;March&quot;}
For j As Integer = 0 To Me.dataGridView1.ColumnCount - 1 Step 2
' Get the column header cell bounds
Dim r1 As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(j, -1, True)
r1.X &#43;= 1
r1.Y &#43;= 1
r1.Width = r1.Width * 2 - 2
r1.Height = r1.Height / 2 - 2
Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
e.Graphics.FillRectangle(br, r1)
End Using
Using p As Pen = New Pen(SystemColors.InactiveBorder)
e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
End Using
Using format As StringFormat = New StringFormat()
Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(monthes(j / 2), Me.dataGridView1.ColumnHeadersDefaultCellStyle.Font, _
br, r1, format)
End Using
End Using
Next
End Sub
</pre>
<pre id="codePreview" class="vb">
Private Sub dataGridView1_Paint(ByVal sender As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Object.aspx" title="Auto generated link to System.Object">System.Object</a>, ByVal e As <a class="libraryLink" href="http://msdn.microsoft.com/en-US/library/System.Windows.Forms.PaintEventArgs.aspx" title="Auto generated link to System.Windows.Forms.PaintEventArgs">System.Windows.Forms.PaintEventArgs</a>) Handles dataGridView1.Paint
' Data for the &quot;merged&quot; header cells
Dim monthes As String() = {&quot;January&quot;, &quot;February&quot;, &quot;March&quot;}
For j As Integer = 0 To Me.dataGridView1.ColumnCount - 1 Step 2
' Get the column header cell bounds
Dim r1 As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(j, -1, True)
r1.X &#43;= 1
r1.Y &#43;= 1
r1.Width = r1.Width * 2 - 2
r1.Height = r1.Height / 2 - 2
Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
e.Graphics.FillRectangle(br, r1)
End Using
Using p As Pen = New Pen(SystemColors.InactiveBorder)
e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
End Using
Using format As StringFormat = New StringFormat()
Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(monthes(j / 2), Me.dataGridView1.ColumnHeadersDefaultCellStyle.Font, _
br, r1, format)
End Using
End Using
Next
End Sub
</pre>
</div>
</div>
<div class="endscriptcode">&nbsp;</div>
<p class="MsoListParagraph"><span style=""></span></p>
<h2>More Information</h2>
<p class="MsoListParagraphCxSpFirst" style="margin-bottom:0cm; margin-bottom:.0001pt; line-height:normal; text-autospace:none">
<span style="font-family:Symbol"><span style="">��<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><span style="font-family:������"><a href="http://windowsclient.net/blogs/faqs/archive/tags/Custom+Designers/default.aspx">Windows Forms FAQs</a>
</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin-bottom:0cm; margin-bottom:.0001pt; line-height:normal; text-autospace:none">
<span style="font-family:Symbol"><span style="">��<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><span style="font-family:������"><a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx">DataGridView Class</a><span style="">&nbsp;&nbsp;
</span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin-bottom:0cm; margin-bottom:.0001pt; line-height:normal; text-autospace:none">
<span style="font-family:Symbol"><span style="">��<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><span style="font-family:������"><a href="http://msdn.microsoft.com/en-us/library/ms180996.aspx">DataGridView Custom Column Sample</a>
</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin-bottom:0cm; margin-bottom:.0001pt; line-height:normal; text-autospace:none">
<span style="font-family:Symbol"><span style="">��<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><span style="font-family:������"><a href="http://msdn.microsoft.com/en-us/library/ms171624.aspx">Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control.</a><span style="">&nbsp;&nbsp;
</span></span></p>
<p class="MsoListParagraphCxSpLast"><span style="font-family:������"><span style="">&nbsp;&nbsp;
</span></span></p>
<hr>
<div><a href="http://go.microsoft.com/?linkid=9759640" style="margin-top:3px"><img alt="" src="description/f0ce43d4-e8ea-415f-bfdd-f6bb39d3f968image.png">
</a></div>
</div>
</div>
</body>
</html>