<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>DataGridView</title>
        <link>http://onteorasoftware.com/category/11.aspx</link>
        <description>DataGridView</description>
        <language>en-US</language>
        <copyright>Ken Tucker</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <item>
            <title>Paging a Windows Forms DataGridView with LINQ</title>
            <link>http://blog.onteorasoftware.net/archive/2009/10/11/paging-a-windows-forms-datagridview-with-linq.aspx</link>
            <description>&lt;p&gt;Since Visual Studio 2008 is due out by the end of the Month I am updating some of my datagridview samples for LINQ. &lt;/p&gt;
&lt;p&gt;To start off create a new windows forms application in VS 2008 make sure you select FrameWork 3.5 so you can use linq &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;&lt;img alt="" width="804" height="480" src="/image.axd?picture=NewProject.png" /&gt; &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;I now added a new Linq to Sql designer to the project and named it Northwind.  Drag the Northwind Products Table on to the design surface from the Server Explorer. &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;&lt;img alt="" width="510" height="392" src="/image.axd?picture=LinqToSql.png" /&gt; &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;On your windows forms add a DataGridView (DataGridView1) and a NumericUpDown control (nuPage).  For this example I will have the datagridview show 15 items at a time.  In the form load event we will figure out how many pages there are and load the first 15 items into the datagridview.  In the NumericUpdown controls value changed event we will update the data displayed &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;Public Class Form1&lt;br /&gt;
    Dim bs As New BindingSource &lt;/p&gt;
&lt;p&gt;    Private intPages As Integer&lt;br /&gt;
    Dim db As New NorthwindDataContext &lt;/p&gt;
&lt;p&gt;    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load&lt;br /&gt;
        intPages = Math.Ceiling(db.Products.Count / 15)&lt;br /&gt;
        nuPage.Maximum = intPages&lt;br /&gt;
        nuPage.Minimum = 1&lt;br /&gt;
        Dim p = From prod In db.Products _&lt;br /&gt;
                Select prod Skip 0 Take 15&lt;br /&gt;
        bs.DataSource = p&lt;br /&gt;
        bs.AllowNew = False&lt;br /&gt;
        DataGridView1.DataSource = bs&lt;br /&gt;
    End Sub &lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
    Private Sub nuPage_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles nuPage.ValueChanged&lt;br /&gt;
        Dim p = From prod In db.Products _&lt;br /&gt;
                Select prod Skip (nuPage.Value - 1) * 15 Take 15&lt;br /&gt;
        bs.DataSource = p&lt;br /&gt;
    End Sub&lt;br /&gt;
End Class &lt;/p&gt;&lt;img src="http://blog.onteorasoftware.net/aggbug/47.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Blog Author</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2009/10/11/paging-a-windows-forms-datagridview-with-linq.aspx</guid>
            <pubDate>Sun, 11 Oct 2009 09:48:23 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/47.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2009/10/11/paging-a-windows-forms-datagridview-with-linq.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/47.aspx</wfw:commentRss>
        </item>
        <item>
            <title>DataGridViewCombobox AutoComplete</title>
            <link>http://blog.onteorasoftware.net/archive/2008/03/26/datagridviewcombobox-autocomplete.aspx</link>
            <description>&lt;p&gt;
&lt;span&gt;
&lt;p align="left"&gt;
&lt;font face="Arial" size="2"&gt;Here is a quick example on using an autocomplete combobox in the DataGridView.  In this example I load all the possible values for the combobox into a AutoCompleteStringCollection and make that the DataGridViewComboBox's datasource.  In the editingControl showing event you need to set the ComboBox's DropDownStyle, and the auto complete settings.&lt;br /&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p align="left"&gt;
 
&lt;/p&gt;
&lt;p align="left"&gt;
Imports System.Data.SqlClient
&lt;/p&gt;
&lt;p align="left"&gt;
Public Class Form1&lt;br /&gt;
    Dim scAutoComplete As New AutoCompleteStringCollection
&lt;/p&gt;
&lt;p align="left"&gt;
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load&lt;br /&gt;
        Dim strConn As String&lt;br /&gt;
        Dim da As SqlDataAdapter&lt;br /&gt;
        Dim conn As SqlConnection&lt;br /&gt;
        Dim ds As New DataSet&lt;br /&gt;
        strConn = "Server = .\SQLEXPRESS;Database = NorthWind; Integrated Security = SSPI;"&lt;br /&gt;
        conn = New SqlConnection(strConn)&lt;br /&gt;
        da = New SqlDataAdapter("Select * from [Orders]", conn)&lt;br /&gt;
        da.Fill(ds, "Orders")&lt;br /&gt;
        DataGridView1.DataSource = ds.Tables("Orders")
&lt;/p&gt;
&lt;p align="left"&gt;
        Dim cmd As New SqlCommand("Select CustomerID From customers", conn)&lt;br /&gt;
        Dim dr As SqlDataReader&lt;br /&gt;
        conn.Open()&lt;br /&gt;
        dr = cmd.ExecuteReader&lt;br /&gt;
        Do While dr.Read&lt;br /&gt;
            scAutoComplete.Add(dr.GetString(0))&lt;br /&gt;
        Loop&lt;br /&gt;
        conn.Close()
&lt;/p&gt;
&lt;p align="left"&gt;
        Dim dgvcbc As New DataGridViewComboBoxColumn&lt;br /&gt;
        With dgvcbc&lt;br /&gt;
            .DataPropertyName = "CustomerID"&lt;br /&gt;
            .DataSource = scAutoComplete&lt;br /&gt;
            .HeaderText = "Customer ID"&lt;br /&gt;
        End With&lt;br /&gt;
        DataGridView1.Columns.Remove("CustomerID")&lt;br /&gt;
        DataGridView1.Columns.Insert(1, dgvcbc)&lt;br /&gt;
    End Sub
&lt;/p&gt;
&lt;p align="left"&gt;
    Private Sub
DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Handles DataGridView1.EditingControlShowing&lt;br /&gt;
        If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf e.Control Is ComboBox Then&lt;br /&gt;
            With DirectCast(e.Control, ComboBox)&lt;br /&gt;
                .DropDownStyle = ComboBoxStyle.DropDown&lt;br /&gt;
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend&lt;br /&gt;
                .AutoCompleteSource = AutoCompleteSource.CustomSource&lt;br /&gt;
                .AutoCompleteCustomSource = scAutoComplete&lt;br /&gt;
            End With&lt;br /&gt;
        End If&lt;br /&gt;
    End Sub
&lt;/p&gt;
&lt;p align="left"&gt;
End Class
&lt;/p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fonteorasoftware.net%2fpost%2fDataGridViewCombobox-AutoComplete.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fonteorasoftware.net%2fpost%2fDataGridViewCombobox-AutoComplete.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;img src="http://blog.onteorasoftware.net/aggbug/30.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2008/03/26/datagridviewcombobox-autocomplete.aspx</guid>
            <pubDate>Wed, 26 Mar 2008 10:09:46 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/30.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2008/03/26/datagridviewcombobox-autocomplete.aspx#feedback</comments>
            <slash:comments>12</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/30.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Datagridview and Linq issue</title>
            <link>http://blog.onteorasoftware.net/archive/2008/01/28/datagridview-and-linq-issue.aspx</link>
            <description>&lt;p&gt;
I had some one ask me an interesting question about using linq with the datagridview 
&lt;/p&gt;
&lt;p&gt;
When I bind a datagridview to this query 
&lt;/p&gt;
&lt;p&gt;
Dim names() As String = {"hello11", "hello212", "hello123", "hello124", "hello2325", "hello336", "hello457"}&lt;br /&gt;
Dim query = From s In names _&lt;br /&gt;
            Order By s _&lt;br /&gt;
            Select s&lt;br /&gt;
Dim bs As New BindingSource&lt;br /&gt;
bs.DataSource = query&lt;br /&gt;
DataGridView1.DataSource = bs 
&lt;/p&gt;
&lt;p&gt;
Why do I get these results? 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="/image.axd?picture=WindowsLiveWriter/DatagridviewandLinqissue_5C98/image_4.png"&gt;&lt;img style="border: 0px" src="/image.axd?picture=WindowsLiveWriter/DatagridviewandLinqissue_5C98/image_thumb_1.png" border="0" alt="image" width="164" height="218" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The answer the datagridview will show the properties of the class in the list bound to the datagridview.  In this case we are bound to a list of string and the only bindable property is its Length.   
&lt;/p&gt;
&lt;p&gt;
If you change the query to this 
&lt;/p&gt;
&lt;p&gt;
Dim names() As String = {"hello11", "hello212", "hello123", "hello124", "hello2325", "hello336", "hello457"}&lt;br /&gt;
Dim query = From s In names _&lt;br /&gt;
            Order By s _&lt;br /&gt;
            Select New With {.Name = s}&lt;br /&gt;
Dim bs As New BindingSource&lt;br /&gt;
bs.DataSource = query&lt;br /&gt;
DataGridView1.DataSource = bs 
&lt;/p&gt;
&lt;p&gt;
You will get the expected results 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="/image.axd?picture=WindowsLiveWriter/DatagridviewandLinqissue_5C98/image_6.png"&gt;&lt;img style="border: 0px" src="/image.axd?picture=WindowsLiveWriter/DatagridviewandLinqissue_5C98/image_thumb_2.png" border="0" alt="image" width="164" height="219" /&gt;&lt;/a&gt;   
&lt;/p&gt;
&lt;p&gt;
I should also mention that you can not bind a datagridview to a linq query directory.  You need to bind the query to a bindingsource and bind the datagridview to the bindingsource or bind the datagridview to the queries ToList method. 
&lt;/p&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.onteorasoftware.net%2fpost%2fDatagridview-and-Linq-issue.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.onteorasoftware.net%2fpost%2fDatagridview-and-Linq-issue.aspx" border="0" alt="kick it on DotNetKicks.com" width="164" height="30" /&gt;&lt;/a&gt; 
&lt;img src="http://blog.onteorasoftware.net/aggbug/33.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2008/01/28/datagridview-and-linq-issue.aspx</guid>
            <pubDate>Mon, 28 Jan 2008 07:40:47 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/33.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2008/01/28/datagridview-and-linq-issue.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/33.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Sql Endpoint and DataGridView</title>
            <link>http://blog.onteorasoftware.net/archive/2007/09/16/sql-endpoint-and-datagridview.aspx</link>
            <description>&lt;h3 align="center"&gt;Sql Endpoint and DataGridView&lt;/h3&gt;SQL Server 2005 allows you to create webservices for accessing the data in a database.  These webservices are call SQL End Points.  Lets start by creating a stored procedure to get the contact name and there titles for all the customers in the northwind database.  &lt;br /&gt;
&lt;br /&gt;
Create PROCEDURE [dbo].[GetContacts] &lt;br /&gt;
AS&lt;br /&gt;
BEGIN&lt;br /&gt;
    SET NOCOUNT ON;&lt;br /&gt;
&lt;br /&gt;
    SELECT [ContactName],[ContactTitle] FROM Customers ORDER BY [ContactName];&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
Now we can create the end point.  Note for this example I am using port 88 for the end point to prevent errors if you have IIS installed.&lt;br /&gt;
&lt;br /&gt;
CREATE ENDPOINT NW_Contacts&lt;br /&gt;
    STATE = Started&lt;br /&gt;
AS HTTP&lt;br /&gt;
    (&lt;br /&gt;
        PATH = '/Contacts',&lt;br /&gt;
        AUTHENTICATION = (INTEGRATED),&lt;br /&gt;
        PORTS = (CLEAR),&lt;br /&gt;
CLEAR_PORT = 88,&lt;br /&gt;
        SITE = '*'&lt;br /&gt;
    )&lt;br /&gt;
FOR SOAP&lt;br /&gt;
    (&lt;br /&gt;
        WEBMETHOD 'GetContacts'&lt;br /&gt;
            (NAME = 'Northwind.dbo.GetContacts'),&lt;br /&gt;
        WSDL = DEFAULT,&lt;br /&gt;
        DATABASE = 'Northwind',&lt;br /&gt;
        NAMESPACE = DEFAULT&lt;br /&gt;
    )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now we need to create a windows forms application.  I called the app EndPointTest.  On the form add a datagridview and set its dock property to fill.&lt;br /&gt;
Then add a web reference to the app.  For the url use http://localhost:88/contacts?WSDL.  For the name I used ContactEndPoint.&lt;br /&gt;
Here is some sample code for filling the datagridview with data from the end point&lt;br /&gt;
&lt;br /&gt;
Imports System.Net&lt;br /&gt;
&lt;br /&gt;
Public Class Form1&lt;br /&gt;
&lt;br /&gt;
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load&lt;br /&gt;
        Dim ws As New ContactEndPoint.NW_Contacts&lt;br /&gt;
&lt;br /&gt;
        ws.Credentials = CredentialCache.DefaultCredentials&lt;br /&gt;
&lt;br /&gt;
        Dim ds As DataSet = TryCast(ws.GetContacts(0), DataSet)&lt;br /&gt;
        DataGridView1.DataSource = ds.Tables(0)&lt;br /&gt;
&lt;br /&gt;
    End Sub&lt;br /&gt;
End Class
&lt;img src="http://blog.onteorasoftware.net/aggbug/62.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2007/09/16/sql-endpoint-and-datagridview.aspx</guid>
            <pubDate>Sun, 16 Sep 2007 22:36:43 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/62.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2007/09/16/sql-endpoint-and-datagridview.aspx#feedback</comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/62.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Get XML string into a DataGridView</title>
            <link>http://blog.onteorasoftware.net/archive/2007/09/16/get-xml-string-into-a-datagridview.aspx</link>
            <description>&lt;h3 align="center"&gt;Get XML string into a DataGridView&lt;/h3&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;
In the msdn forums I see people asking how do they get a string that contains xml to display in the DataGridView. They way to do this is to read the string into a StringReader and use the DataSet,Readxml method to convert the xml into a datatable. Note the xml must be well formed for this to work. 
&lt;/p&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;font face="Lucida Console" size="2"&gt; &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; srXML &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;New&lt;/font&gt; IO.StringReader(strXML)&lt;br /&gt;
 &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; dsXML &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;New&lt;/font&gt; DataSet&lt;br /&gt;
 dsXML.ReadXml(srXML)&lt;br /&gt;
&lt;br /&gt;
 DataGridView1.DataSource = dsXML.Tables(0)&lt;/font&gt;
&lt;img src="http://blog.onteorasoftware.net/aggbug/76.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2007/09/16/get-xml-string-into-a-datagridview.aspx</guid>
            <pubDate>Sun, 16 Sep 2007 19:28:16 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/76.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2007/09/16/get-xml-string-into-a-datagridview.aspx#feedback</comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/76.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Draw an Icon in a DataGridViewButtonCell</title>
            <link>http://blog.onteorasoftware.net/archive/2007/09/16/draw-an-icon-in-a-datagridviewbuttoncell.aspx</link>
            <description>&lt;h3 align="center"&gt;Draw an Icon in a DataGridViewButtonCell&lt;/h3&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;
Here is a simple example on how to draw in a DataGridViewButtonCell. In this example an icon is displayed when button is pushed and the icon is made invisible when its pushed again. I am storing the if the button has been pressed in the cell's tag. I use the cellPainting event to draw the icon when needed. 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
using System; &lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.ComponentModel;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using System.Drawing;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Windows.Forms;
&lt;/p&gt;
&lt;p&gt;
namespace CSButtonColumn&lt;br /&gt;
{&lt;br /&gt;
    public partial class Form1 : Form&lt;br /&gt;
    {&lt;br /&gt;
        public Form1()&lt;br /&gt;
        {&lt;br /&gt;
            InitializeComponent();&lt;br /&gt;
        }
&lt;/p&gt;
&lt;p&gt;
        private void Form1_Load(object sender, EventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            String strConn = "Server = .\\SqlExpress;Database = Pubs;Integrated Security = SSPI;";&lt;br /&gt;
            DataTable dt = new DataTable();&lt;br /&gt;
            SqlConnection conn = new SqlConnection(strConn);&lt;br /&gt;
            SqlDataAdapter da = new SqlDataAdapter("Select * from titles", conn);&lt;br /&gt;
            da.Fill(dt);&lt;br /&gt;
            dataGridView1.DataSource = dt;&lt;br /&gt;
            DataGridViewButtonColumn bc = new DataGridViewButtonColumn();&lt;br /&gt;
            bc.Tag = false;&lt;br /&gt;
            dataGridView1.Columns.Insert(0, bc);&lt;br /&gt;
        }
&lt;/p&gt;
&lt;p&gt;
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            if (e.ColumnIndex == 0)&lt;br /&gt;
            {&lt;br /&gt;
                e.Value = "Repair";&lt;br /&gt;
                e.FormattingApplied = true;&lt;br /&gt;
            }&lt;br /&gt;
        }
&lt;/p&gt;
&lt;p&gt;
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            if (e.ColumnIndex == 0 &amp;amp;&amp;amp; e.RowIndex &amp;gt;= 0)&lt;br /&gt;
            {&lt;br /&gt;
                e.Paint(e.CellBounds, DataGridViewPaintParts.All);&lt;br /&gt;
                DataGridViewButtonCell bc = dataGridView1[0, e.RowIndex] as DataGridViewButtonCell;&lt;br /&gt;
                bool x;&lt;br /&gt;
                if (bc.Tag == null)&lt;br /&gt;
                {&lt;br /&gt;
                    x = false;&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    x = (bool)bc.Tag;&lt;br /&gt;
                }&lt;br /&gt;
                if (x)&lt;br /&gt;
                {&lt;br /&gt;
                    Icon ico = new Icon("repair.ico");&lt;br /&gt;
                    e.Graphics.DrawIcon(ico, e.CellBounds.Left+3, e.CellBounds.Top+3 );&lt;br /&gt;
                }&lt;br /&gt;
                e.Handled = true;&lt;br /&gt;
            }&lt;br /&gt;
        }
&lt;/p&gt;
&lt;p&gt;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            if (e.ColumnIndex == 0 &amp;amp;&amp;amp; e.RowIndex &amp;gt;= 0)&lt;br /&gt;
            {&lt;br /&gt;
                DataGridViewButtonCell bc = dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewButtonCell;&lt;br /&gt;
                if (bc.Tag == null)&lt;br /&gt;
                {&lt;br /&gt;
                    bc.Tag = true;&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    bc.Tag = !(bool)bc.Tag;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}
&lt;/p&gt;
&lt;p&gt;
 
&lt;/p&gt;
&lt;img src="http://blog.onteorasoftware.net/aggbug/82.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2007/09/16/draw-an-icon-in-a-datagridviewbuttoncell.aspx</guid>
            <pubDate>Sun, 16 Sep 2007 19:25:25 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/82.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2007/09/16/draw-an-icon-in-a-datagridviewbuttoncell.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/82.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Data From Multiple Tables in a DataGridView</title>
            <link>http://blog.onteorasoftware.net/archive/2007/09/16/data-from-multiple-tables-in-a-datagridview.aspx</link>
            <description>&lt;h3 align="center"&gt;Data From Multiple Tables in a DataGridView&lt;/h3&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;
There is &lt;a href="http://support.microsoft.com/kb/325682"&gt;Msdn Knowledge base article&lt;/a&gt; which shows how to create a JoinView class. The JoinView class is for joining 2 tables together for data binding. Basically you load 2 or more tables into a dataset and set up some data relations for the related tables. When you create the Joinview the first argument is the main table, 2nd is a list of fields you want to show, 3rd is a filter, and 4th is the field to sort on. The last 2 arguments are option. Since the article includes a vb sample I am posting a c# example. I would recommend compiling JoinView.VB into a class so you use it with c#. I named my dll MSDNClasses. 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.ComponentModel;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using System.Drawing;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
using MSDNClasses;
&lt;/p&gt;
&lt;p&gt;
namespace CSDGVMultiTable&lt;br /&gt;
{&lt;br /&gt;
    public partial class Form1 : Form&lt;br /&gt;
    {&lt;br /&gt;
        public Form1()&lt;br /&gt;
        {&lt;br /&gt;
            InitializeComponent();&lt;br /&gt;
        }
&lt;/p&gt;
&lt;p&gt;
        DataSet ds = new DataSet();&lt;br /&gt;
        JoinView jv;
&lt;/p&gt;
&lt;p&gt;
        private void Form1_Load(object sender, EventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            SqlConnection conn = new SqlConnection(@"server=.\sqlexpress;integrated security=true;database=northwind");&lt;br /&gt;
            SqlDataAdapter daCust = new SqlDataAdapter(@"Select * From Customers", conn);&lt;br /&gt;
            SqlDataAdapter daEmp = new SqlDataAdapter(@"Select * From Employees", conn);&lt;br /&gt;
            SqlDataAdapter daOrd = new SqlDataAdapter(@"Select * From Orders", conn);
&lt;/p&gt;
&lt;p&gt;
            daCust.Fill(ds, "Cust");&lt;br /&gt;
            daEmp.Fill(ds, "Emp");&lt;br /&gt;
            daOrd.Fill(ds, "Ord");
&lt;/p&gt;
&lt;p&gt;
            // Create some data relations for the joinview to use
&lt;/p&gt;
&lt;p&gt;
            ds.Relations.Add("CustOrd", ds.Tables["Cust"].Columns["CustomerID"], ds.Tables["Ord"].Columns["CustomerID"]);&lt;br /&gt;
            ds.Relations.Add("EmpOrd", ds.Tables["Emp"].Columns["EmployeeID"], ds.Tables["Ord"].Columns["EmployeeID"]);
&lt;/p&gt;
&lt;p&gt;
            // Create Join View&lt;br /&gt;
            // Select fields for JoinView,  a filter to use, and Column to sort on
&lt;/p&gt;
&lt;p&gt;
            jv = new JoinView(ds.Tables["Ord"],&lt;br /&gt;
                "OrderID,CustomerID,EmployeeID,OrderDate,CustOrd.CompanyName Company,CustOrd.ContactName Contact,CustOrd.ContactTitle Position,EmpOrd.FirstName,EmpOrd.LastName",&lt;br /&gt;
                "", "");
&lt;/p&gt;
&lt;p&gt;
            dataGridView1.DataSource = jv;
&lt;/p&gt;
&lt;p&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}
&lt;/p&gt;
&lt;img src="http://blog.onteorasoftware.net/aggbug/86.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2007/09/16/data-from-multiple-tables-in-a-datagridview.aspx</guid>
            <pubDate>Sun, 16 Sep 2007 19:02:45 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/86.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2007/09/16/data-from-multiple-tables-in-a-datagridview.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/86.aspx</wfw:commentRss>
        </item>
        <item>
            <title>AutoComplete in DataGridView</title>
            <link>http://blog.onteorasoftware.net/archive/2007/09/16/autocomplete-in-datagridview.aspx</link>
            <description>&lt;h3 align="center"&gt;AutoComplete in DataGridView&lt;/h3&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;
In the datagridview's editing control showing event you have better access to the textboxes properties. Here is an example of adding autocomplete to the textbox. 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
VB Sample 
&lt;/p&gt;
&lt;p&gt;
Imports System.Data.SqlClient
&lt;/p&gt;
&lt;p&gt;
Public Class Form1
&lt;/p&gt;
&lt;p&gt;
    Dim scAutoComplete As New AutoCompleteStringCollection
&lt;/p&gt;
&lt;p&gt;
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load&lt;br /&gt;
        Dim strConn As String&lt;br /&gt;
        Dim da As SqlDataAdapter&lt;br /&gt;
        Dim conn As SqlConnection&lt;br /&gt;
        Dim ds As New DataSet&lt;br /&gt;
        strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;"&lt;br /&gt;
        conn = New SqlConnection(strConn)&lt;br /&gt;
        da = New SqlDataAdapter("Select * from [Orders]", conn)&lt;br /&gt;
        da.Fill(ds, "Orders")&lt;br /&gt;
        DataGridView1.DataSource = ds.Tables("Orders")
&lt;/p&gt;
&lt;p&gt;
        Dim cmd As New SqlCommand("Select CustomerID From customers", conn)&lt;br /&gt;
        Dim dr As SqlDataReader
&lt;/p&gt;
&lt;p&gt;
        conn.Open()&lt;br /&gt;
        dr = cmd.ExecuteReader&lt;br /&gt;
        Do While dr.Read&lt;br /&gt;
            scAutoComplete.Add(dr.GetString(0))&lt;br /&gt;
        Loop&lt;br /&gt;
        conn.Close()&lt;br /&gt;
    End Sub
&lt;/p&gt;
&lt;p&gt;
    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing&lt;br /&gt;
        If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf e.Control Is TextBox Then&lt;br /&gt;
            With DirectCast(e.Control, TextBox)&lt;br /&gt;
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend&lt;br /&gt;
                .AutoCompleteSource = AutoCompleteSource.CustomSource&lt;br /&gt;
                .AutoCompleteCustomSource = scAutoComplete&lt;br /&gt;
            End With&lt;br /&gt;
        End If&lt;br /&gt;
    End Sub&lt;br /&gt;
End Class
&lt;/p&gt;
&lt;p&gt;
 
&lt;/p&gt;
&lt;p&gt;
C# Sample 
&lt;/p&gt;
&lt;p&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.ComponentModel;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Drawing;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
using System.Data.SqlClient;
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
namespace DGCAutoComplete&lt;br /&gt;
{&lt;br /&gt;
    public partial class Form1 : Form&lt;br /&gt;
    {&lt;br /&gt;
        public Form1()&lt;br /&gt;
        {&lt;br /&gt;
            InitializeComponent();&lt;br /&gt;
        }
&lt;/p&gt;
&lt;p&gt;
        AutoCompleteStringCollection scAutoComplete = new AutoCompleteStringCollection();
&lt;/p&gt;
&lt;p&gt;
        private void Form1_Load(object sender, EventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            DataTable dt = new DataTable();&lt;br /&gt;
            String strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;";&lt;br /&gt;
            SqlConnection conn = new SqlConnection(strConn);&lt;br /&gt;
            SqlDataAdapter da = new SqlDataAdapter("Select * from [Orders]", conn);&lt;br /&gt;
            da.Fill(dt);&lt;br /&gt;
            dataGridView1.DataSource = dt;&lt;br /&gt;
            SqlCommand cmd = new SqlCommand("Select CustomerID From customers", conn);&lt;br /&gt;
            SqlDataReader dr;&lt;br /&gt;
                conn.Open();&lt;br /&gt;
                dr=cmd.ExecuteReader();&lt;br /&gt;
                while(dr.Read())&lt;br /&gt;
                {&lt;br /&gt;
                    scAutoComplete.Add(dr.GetString(0));&lt;br /&gt;
                }&lt;br /&gt;
                conn.Close();&lt;br /&gt;
        }
&lt;/p&gt;
&lt;p&gt;
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            if (dataGridView1.CurrentCellAddress.X == 1)&lt;br /&gt;
            {&lt;br /&gt;
                TextBox txt = e.Control as TextBox;&lt;br /&gt;
                txt.AutoCompleteCustomSource = scAutoComplete;&lt;br /&gt;
                txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;&lt;br /&gt;
                txt.AutoCompleteSource = AutoCompleteSource.CustomSource;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}
&lt;/p&gt;
&lt;img src="http://blog.onteorasoftware.net/aggbug/94.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2007/09/16/autocomplete-in-datagridview.aspx</guid>
            <pubDate>Sun, 16 Sep 2007 19:00:07 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/94.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2007/09/16/autocomplete-in-datagridview.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/94.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Predicates</title>
            <link>http://blog.onteorasoftware.net/archive/2007/09/16/predicates.aspx</link>
            <description>&lt;h3 align="center"&gt;Predicates&lt;/h3&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;
I recently read an article in the MSDN magazine about &lt;a href="http://msdn.microsoft.com/msdnmag/issues/06/09/AdvancedBasics/"&gt;Predicates and Actions&lt;/a&gt; written by Ken Getz. I was thinking that predicates will make it easy to create a master details datagridview for a business object. This example creates 2 classes Customers and Orders. I used the Northwind database to fill a list of customers and a list of orders. I bound one grid to a binding source who's datasource is the list of customers. In the binding sources position changed event I use the list of orders findall method to show all the orders for a customer. 
&lt;/p&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;
The classes 
&lt;/p&gt;
&lt;p&gt;
Public Class Orders&lt;br /&gt;
    Private mintOrderID As Integer&lt;br /&gt;
    Public Property OrderID() As Integer&lt;br /&gt;
        Get&lt;br /&gt;
            Return mintOrderID&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As Integer)&lt;br /&gt;
            mintOrderID = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Private mdtOrderDate As Date&lt;br /&gt;
    Public Property OrderDate() As Date&lt;br /&gt;
        Get&lt;br /&gt;
            Return mdtOrderDate&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As Date)&lt;br /&gt;
            mdtOrderDate = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Private mstrCustID As String&lt;br /&gt;
    Public Property CustomerID() As String&lt;br /&gt;
        Get&lt;br /&gt;
            Return mstrCustID&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As String)&lt;br /&gt;
            mstrCustID = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
End Class 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
Public Class Customers&lt;br /&gt;
    Private mstrCustID As String&lt;br /&gt;
    Public Property CustomerID() As String&lt;br /&gt;
        Get&lt;br /&gt;
            Return mstrCustID&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As String)&lt;br /&gt;
            mstrCustID = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Private mstrCompanyName As String&lt;br /&gt;
    Public Property CompanyName() As String&lt;br /&gt;
        Get&lt;br /&gt;
            Return mstrCompanyName&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As String)&lt;br /&gt;
            mstrCompanyName = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Private mstrContactName As String&lt;br /&gt;
    Public Property ContactName() As String&lt;br /&gt;
        Get&lt;br /&gt;
            Return mstrContactName&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As String)&lt;br /&gt;
            mstrContactName = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
End Class 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
&lt;br /&gt;
  
&lt;/p&gt;
&lt;p&gt;
The Main code 
&lt;/p&gt;
&lt;p&gt;
Imports System.Data.SqlClient 
&lt;/p&gt;
&lt;p&gt;
Public Class Form1&lt;br /&gt;
    Dim lstCustomers As New List(Of Customers)&lt;br /&gt;
    Dim lstOrders As New List(Of Orders)&lt;br /&gt;
    Dim WithEvents bsCustomers As New BindingSource&lt;br /&gt;
    Private CustId As String = "" 
&lt;/p&gt;
&lt;p&gt;
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load&lt;br /&gt;
        FillCustomers()&lt;br /&gt;
        FillOrders()&lt;br /&gt;
        bsCustomers.DataSource = lstCustomers&lt;br /&gt;
        DataGridView1.DataSource = bsCustomers&lt;br /&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Private Sub FillCustomers()&lt;br /&gt;
        Dim conn As SqlConnection&lt;br /&gt;
        Dim strConn As String&lt;br /&gt;
        Dim dr As SqlDataReader&lt;br /&gt;
        Dim cmd As SqlCommand&lt;br /&gt;
        Dim strSql As String 
&lt;/p&gt;
&lt;p&gt;
        strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;" 
&lt;/p&gt;
&lt;p&gt;
        conn = New SqlConnection(strConn) 
&lt;/p&gt;
&lt;p&gt;
        conn.Open() 
&lt;/p&gt;
&lt;p&gt;
        strSql = "Select CustomerID, CompanyName, ContactName from Customers" 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
        cmd = New SqlCommand(strSql, conn) 
&lt;/p&gt;
&lt;p&gt;
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection) 
&lt;/p&gt;
&lt;p&gt;
        Do While dr.Read&lt;br /&gt;
            Dim cls As New Customers&lt;br /&gt;
            With cls&lt;br /&gt;
                .CompanyName = dr("CompanyName").ToString&lt;br /&gt;
                .ContactName = dr("ContactName").ToString&lt;br /&gt;
                .CustomerID = dr("CustomerID").ToString&lt;br /&gt;
            End With&lt;br /&gt;
            lstCustomers.Add(cls)&lt;br /&gt;
        Loop&lt;br /&gt;
        conn.Close() 
&lt;/p&gt;
&lt;p&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Private Sub FillOrders()&lt;br /&gt;
        Dim conn As SqlConnection&lt;br /&gt;
        Dim strConn As String&lt;br /&gt;
        Dim dr As SqlDataReader&lt;br /&gt;
        Dim cmd As SqlCommand&lt;br /&gt;
        Dim strSql As String 
&lt;/p&gt;
&lt;p&gt;
        strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;" 
&lt;/p&gt;
&lt;p&gt;
        conn = New SqlConnection(strConn) 
&lt;/p&gt;
&lt;p&gt;
        conn.Open() 
&lt;/p&gt;
&lt;p&gt;
        strSql = "Select CustomerID, OrderID, OrderDate from Orders" 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
        cmd = New SqlCommand(strSql, conn) 
&lt;/p&gt;
&lt;p&gt;
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection) 
&lt;/p&gt;
&lt;p&gt;
        Do While dr.Read&lt;br /&gt;
            Dim cls As New Orders&lt;br /&gt;
            With cls&lt;br /&gt;
                .OrderID = dr.GetInt32(1)&lt;br /&gt;
                .OrderDate = dr.GetDateTime(2)&lt;br /&gt;
                .CustomerID = dr("CustomerID").ToString&lt;br /&gt;
            End With&lt;br /&gt;
            lstOrders.Add(cls)&lt;br /&gt;
        Loop&lt;br /&gt;
        conn.Close() 
&lt;/p&gt;
&lt;p&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Private Function FindCustomerOrders(ByVal Ordr As Orders) As Boolean&lt;br /&gt;
        Return Ordr.CustomerID = CustId&lt;br /&gt;
    End Function 
&lt;/p&gt;
&lt;p&gt;
    Private Sub bsCustomers_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsCustomers.PositionChanged&lt;br /&gt;
        Dim cust As Customers 
&lt;/p&gt;
&lt;p&gt;
        cust = TryCast(bsCustomers.Current, Customers)&lt;br /&gt;
        If Not cust Is Nothing Then&lt;br /&gt;
            CustId = cust.CustomerID&lt;br /&gt;
            DataGridView2.DataSource = lstOrders.FindAll(AddressOf FindCustomerOrders)&lt;br /&gt;
        End If&lt;br /&gt;
    End Sub&lt;br /&gt;
End Class 
&lt;/p&gt;
&lt;p&gt;
 
&lt;/p&gt;
&lt;img src="http://blog.onteorasoftware.net/aggbug/98.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2007/09/16/predicates.aspx</guid>
            <pubDate>Sun, 16 Sep 2007 18:52:58 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/98.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2007/09/16/predicates.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/98.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Create a Pivot Table</title>
            <link>http://blog.onteorasoftware.net/archive/2007/09/16/create-a-pivot-table.aspx</link>
            <description>&lt;h3 align="center"&gt;Create a Pivot Table&lt;/h3&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;
Here is a quick example on how to rotate or pivot the data in a datagridview. This is a c# program which creates a new data table with a rotated version of the data to bind to. There is a VB version on the &lt;a href="http://www.vb-tips.com/dbPages.aspx?ID=28f49f7c-ec40-4472-9b28-2b9e762316ab"&gt;vb-tips website&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.ComponentModel;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Drawing;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Windows.Forms; 
&lt;/p&gt;
&lt;p&gt;
namespace Pivot&lt;br /&gt;
{&lt;br /&gt;
    public partial class Form1 : Form&lt;br /&gt;
    {&lt;br /&gt;
        public Form1()&lt;br /&gt;
        {&lt;br /&gt;
            InitializeComponent();&lt;br /&gt;
        }&lt;br /&gt;
        DataTable dt1; 
&lt;/p&gt;
&lt;p&gt;
        private void Form1_Load(object sender, EventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            dt1 = CreateTable();&lt;br /&gt;
            DataTable dt2 = new DataTable("Reflected");&lt;br /&gt;
            for (int i = 0; i &amp;lt; dt1.Rows.Count; i++)&lt;br /&gt;
            {&lt;br /&gt;
                dt2.Columns.Add(i.ToString());&lt;br /&gt;
            } 
&lt;/p&gt;
&lt;p&gt;
            for (int x = 0; x &amp;lt; dt1.Columns.Count; x++)&lt;br /&gt;
            {&lt;br /&gt;
                DataRow dr = dt2.NewRow();&lt;br /&gt;
                for (int y = 0; y &amp;lt; dt1.Rows.Count; y++)&lt;br /&gt;
                {&lt;br /&gt;
                    dr[y] = dt1.Rows[y][x];&lt;br /&gt;
                }&lt;br /&gt;
                dt2.Rows.Add(dr);&lt;br /&gt;
            } 
&lt;/p&gt;
&lt;p&gt;
            dataGridView1.RowHeadersWidth = 60;&lt;br /&gt;
            dataGridView1.DataSource = dt2;&lt;br /&gt;
            dataGridView1.AllowUserToAddRows = false;&lt;br /&gt;
        } 
&lt;/p&gt;
&lt;p&gt;
        public DataTable CreateTable()&lt;br /&gt;
        {&lt;br /&gt;
            DataTable dt = new DataTable("Orginal");&lt;br /&gt;
            dt.Columns.Add("Name");&lt;br /&gt;
            dt.Columns.Add("State");&lt;br /&gt;
            dt.Columns.Add("Country"); 
&lt;/p&gt;
&lt;p&gt;
            Object[] arRow = new Object[3];&lt;br /&gt;
            arRow[0] = "Ken";&lt;br /&gt;
            arRow[1]="Florida";&lt;br /&gt;
            arRow[2] = "US"; 
&lt;/p&gt;
&lt;p&gt;
            dt.LoadDataRow(arRow,true); 
&lt;/p&gt;
&lt;p&gt;
            arRow[0] = "Cor";&lt;br /&gt;
            arRow[1] = "Holland";&lt;br /&gt;
            arRow[2] = "EU"; 
&lt;/p&gt;
&lt;p&gt;
            dt.LoadDataRow(arRow, true);&lt;br /&gt;
            return dt;&lt;br /&gt;
        } 
&lt;/p&gt;
&lt;p&gt;
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)&lt;br /&gt;
        {&lt;br /&gt;
            if (e.ColumnIndex == -1 &amp;amp;&amp;amp; e.RowIndex &amp;gt;= 0)&lt;br /&gt;
            {&lt;br /&gt;
                StringFormat sf = new StringFormat();&lt;br /&gt;
                sf.Alignment = StringAlignment.Center;&lt;br /&gt;
                sf.LineAlignment = StringAlignment.Center;&lt;br /&gt;
                e.PaintBackground(e.ClipBounds, true);&lt;br /&gt;
                e.Graphics.DrawString(dt1.Columns[e.RowIndex].ColumnName.ToString(), this.Font, Brushes.Black, e.CellBounds, sf );&lt;br /&gt;
                e.Handled = true;&lt;br /&gt;
            }&lt;br /&gt;
        } 
&lt;/p&gt;
&lt;p&gt;
    } 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
} 
&lt;/p&gt;
&lt;p&gt;
 
&lt;/p&gt;
&lt;img src="http://blog.onteorasoftware.net/aggbug/100.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2007/09/16/create-a-pivot-table.aspx</guid>
            <pubDate>Sun, 16 Sep 2007 18:49:37 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/100.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2007/09/16/create-a-pivot-table.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/100.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>