<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DEACSXs_eCp7ImA9WhRQGEo.&quot;"><id>tag:blogger.com,1999:blog-4048875091755149288</id><updated>2011-12-15T00:32:48.540+09:00</updated><category term="DataGridView" /><category term=".NET" /><title>Developer's HIGH .NET</title><subtitle type="html">開発で調べたこととかハマったことの、やや自分用のメモ。
.NET Frameworkよりな感じ。</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://mechadog.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://mechadog.blogspot.com/" /><author><name>mechadog</name><uri>http://www.blogger.com/profile/17305988737160402386</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://4.bp.blogspot.com/-_ML5fR0tbA0/TXjbtNSNkxI/AAAAAAAAAGo/8DAFe7n7_zI/s220/IMG_0129.JPG" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/DevelopersHighnet" /><feedburner:info uri="developershighnet" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CEIGRXc5cCp7ImA9WhZTEE8.&quot;"><id>tag:blogger.com,1999:blog-4048875091755149288.post-5002318267498965090</id><published>2011-03-11T21:09:00.004+09:00</published><updated>2011-03-13T22:22:04.928+09:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-13T22:22:04.928+09:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGridView" /><title>列のヘッダーに、すべて選択用のチェックボックスを配置する</title><content type="html">DataGridViewにチェックボックス列を用意した場合、どうせならすべて選択用のチェックボックスも用意したいところです。&lt;br /&gt;
列のヘッダーにチェックボックスを配置するには、以下の実装を行います。&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;すべて選択用のチェックボックスを用意する。&lt;br /&gt;
（例えば、Formの任意の位置に配置し、VisibleプロパティをFalseにします。）&lt;/li&gt;
&lt;li&gt;DataGridViewのCellPaintingイベントで、列ヘッダーにチェックボックスを描画する。&lt;/li&gt;
&lt;li&gt;DataGridViewのCellClickイベントで、列ヘッダーのチェックボックスの押下イベントを擬似的に取得する。&lt;/li&gt;
&lt;li&gt;CheckBoxのCheckChangedイベントで、すべての行のチェック状態を切り替える。&lt;/li&gt;
&lt;/ol&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;【実行例】&lt;br /&gt;
&lt;a href="https://lh3.googleusercontent.com/-YuLP30qVJYQ/TXoQrTVzkOI/AAAAAAAAAHU/s09Lfa4xJhA/s1600/DataGridView_ColumnCheckBox.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh3.googleusercontent.com/-YuLP30qVJYQ/TXoQrTVzkOI/AAAAAAAAAHU/s09Lfa4xJhA/s1600/DataGridView_ColumnCheckBox.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;pre name="code" class="csharp"&gt;// すべて選択用のチェックボックスを用意
private CheckBox checkBoxAll = new System.Windows.Forms.CheckBox();

// 列ヘッダーにチェックボックスを表示
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    // 列ヘッダーのみ処理を行う。(CheckBox配置列が先頭列の場合)
    if (e.ColumnIndex == 0 &amp;amp;&amp;amp; e.RowIndex == -1)
    {
        using (Bitmap bmp = new Bitmap(100, 100))
        {
            // チェックボックスの描画領域を確保
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Transparent);
            }

            // 描画領域の中央に配置
            Point pt1 = new Point((bmp.Width - checkBoxAll.Width) / 2, (bmp.Height - checkBoxAll.Height) / 2);
            if (pt1.X &amp;lt; 0) pt1.X = 0;
            if (pt1.Y &amp;lt; 0) pt1.Y = 0;

            // Bitmapに描画
            checkBoxAll.DrawToBitmap(bmp, new Rectangle(pt1.X, pt1.Y, bmp.Width, bmp.Height));

            // DataGridViewの現在描画中のセルの中央に描画
            int x = (e.CellBounds.Width - bmp.Width) / 2;
            int y = (e.CellBounds.Height - bmp.Height) / 2;

            Point pt2 = new Point(e.CellBounds.Left + x, e.CellBounds.Top + y);

            e.Paint(e.ClipBounds, e.PaintParts);
            e.Graphics.DrawImage(bmp, pt2);
            e.Handled = true;
        }
    }
}

// 列ヘッダーのチェックボックスを押したときに、すべて選択用のチェックボックス状態を切り替え
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0 &amp;amp;&amp;amp; e.RowIndex == -1)
    {
        checkBoxAll.Checked = !checkBoxAll.Checked;
    }
}

// すべての行のチェック状態を切り替える
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        row.Cells[0].Value = checkBoxAll.Checked;
    }
}
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4048875091755149288-5002318267498965090?l=mechadog.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ESqalh6PlvZ3OMbKg3QaFFcAEcM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ESqalh6PlvZ3OMbKg3QaFFcAEcM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ESqalh6PlvZ3OMbKg3QaFFcAEcM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ESqalh6PlvZ3OMbKg3QaFFcAEcM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DevelopersHighnet/~4/AcuFPA6uoeU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://mechadog.blogspot.com/feeds/5002318267498965090/comments/default" title="コメントの投稿" /><link rel="replies" type="text/html" href="http://mechadog.blogspot.com/2011/03/blog-post_11.html#comment-form" title="0 件のコメント" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4048875091755149288/posts/default/5002318267498965090?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4048875091755149288/posts/default/5002318267498965090?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/DevelopersHighnet/~3/AcuFPA6uoeU/blog-post_11.html" title="列のヘッダーに、すべて選択用のチェックボックスを配置する" /><author><name>mechadog</name><uri>http://www.blogger.com/profile/17305988737160402386</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://4.bp.blogspot.com/-_ML5fR0tbA0/TXjbtNSNkxI/AAAAAAAAAGo/8DAFe7n7_zI/s220/IMG_0129.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://lh3.googleusercontent.com/-YuLP30qVJYQ/TXoQrTVzkOI/AAAAAAAAAHU/s09Lfa4xJhA/s72-c/DataGridView_ColumnCheckBox.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://mechadog.blogspot.com/2011/03/blog-post_11.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkIMRXgyeyp7ImA9WhZTEUQ.&quot;"><id>tag:blogger.com,1999:blog-4048875091755149288.post-9207955297249103004</id><published>2011-03-11T00:16:00.005+09:00</published><updated>2011-03-15T23:16:24.693+09:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-15T23:16:24.693+09:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGridView" /><title>データバインド時のソートに対応したチェックボックス</title><content type="html">バインド列と非バインド列が混在したデータで、DataGridViewのソートを行うと、チェックが消えてしまう現象が発生します。&lt;br /&gt;
MSDNには、バインド列と非バインド列が混在するDataGridViewコントロールを並べ替えるときには、非バインド列の値を自動的に保持できないとの記述がありました。&lt;br /&gt;
&lt;div style="text-align: right;"&gt;(MSDN - [&lt;a href="http://msdn.microsoft.com/ja-jp/library/95scxcdy(v=VS.100).aspx"&gt;Windows フォーム DataGridView コントロール内の列の並べ替えモード&lt;/a&gt;])&lt;/div&gt;このページを見ると、「VirtualMode を使え」と言っているのですが、VirtualModeを元々使うような、大量のデータならともかく、VirtualModeを使用するにはCellValueNeededイベントを実装したりと何かと面倒です。&lt;br /&gt;
VirtualModeを使用せずにこの問題を解決するには、以下の手順で実現可能です。&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;データベースから取得後のDataTableに、Boolean型の列を追加する。&lt;/li&gt;
&lt;li&gt;列を追加したDataTableを、DataGridViewにバインドする。&lt;/li&gt;
&lt;li&gt;チェックボックスの表示列を先頭に配置する。&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;&lt;br /&gt;
【実行例】&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh5.googleusercontent.com/-KG37WR88XlM/TXjqZSh_ntI/AAAAAAAAAHQ/Q4YWObTQqcY/s1600/DataGridView_CheckBox.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh5.googleusercontent.com/-KG37WR88XlM/TXjqZSh_ntI/AAAAAAAAAHQ/Q4YWObTQqcY/s1600/DataGridView_CheckBox.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;pre class="csharp" name="code"&gt;DataTable dataTable1;
// dataTable1 にデータを読み込み

// チェックボックスの列を追加
DataColumn columnCheck = new DataColumn("columnCheck", Type.GetType("System.Boolean"));
dataTable1.Columns.Add(columnCheck);

// DataTableをDataGridViewにバインド
this.dataGridView1.DataSource = dataTable1;

// 並べ替えモードを自動に設定
this.dataGridView1.Columns["columnCheck"].SortMode = DataGridViewColumnSortMode.Automatic;

// 表示位置を一番左に設定
this.dataGridView1.Columns["columnCheck"].DisplayIndex = 0;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4048875091755149288-9207955297249103004?l=mechadog.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Y7cRKuwbLI0bkzo2xify69VTkVY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Y7cRKuwbLI0bkzo2xify69VTkVY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Y7cRKuwbLI0bkzo2xify69VTkVY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Y7cRKuwbLI0bkzo2xify69VTkVY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DevelopersHighnet/~4/NHIbN-YfNfU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://mechadog.blogspot.com/feeds/9207955297249103004/comments/default" title="コメントの投稿" /><link rel="replies" type="text/html" href="http://mechadog.blogspot.com/2011/03/blog-post.html#comment-form" title="0 件のコメント" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4048875091755149288/posts/default/9207955297249103004?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4048875091755149288/posts/default/9207955297249103004?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/DevelopersHighnet/~3/NHIbN-YfNfU/blog-post.html" title="データバインド時のソートに対応したチェックボックス" /><author><name>mechadog</name><uri>http://www.blogger.com/profile/17305988737160402386</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://4.bp.blogspot.com/-_ML5fR0tbA0/TXjbtNSNkxI/AAAAAAAAAGo/8DAFe7n7_zI/s220/IMG_0129.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://lh5.googleusercontent.com/-KG37WR88XlM/TXjqZSh_ntI/AAAAAAAAAHQ/Q4YWObTQqcY/s72-c/DataGridView_CheckBox.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://mechadog.blogspot.com/2011/03/blog-post.html</feedburner:origLink></entry></feed>

