C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 92 を表示中)
■55184 / )  Re[6]: DataGridViewで文字列の終端を右寄せにしたい
□投稿者/ ごう (199回)-(2010/11/19(Fri) 10:54:51)
2010/11/19(Fri) 11:06:13 編集(投稿者)
No55183 (魔界の仮面弁士 さん) に返信
終端の文字列5~6文字程度が表示されれば使用には問題ないと判断したため
今回はEllipsisPathを使って対応することにしました。
「どうしても終端の文字列の表示が優先で、前方は省略記号で・・・」というのであれば
Ellipsisにもありませんし、地道にやっていかないといけなさそうですね。

EllipsisPathを使って実装したコードです。
お返事どうもありがとうございました。

private: System::Void dataGridView1_CellPainting(System::Object^  sender, System::Windows::Forms::DataGridViewCellPaintingEventArgs^  e) 
{
	// 0列目		 
	if(e->ColumnIndex == 0 && e->RowIndex >= 0)
	{
		// セルを描画する(線と背景)
		e->Paint(e->ClipBounds, DataGridViewPaintParts::Border);
		e->Paint(e->ClipBounds, DataGridViewPaintParts::Background);

		DataGridView^ dgv = (DataGridView^)sender;

		// 表示させる文字列
		String^ s = dgv->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value->ToString(); 


		// 表示領域を取得(セルの座標)
		System::Drawing::Rectangle rect = dgv->GetCellDisplayRectangle(e->ColumnIndex, 
                                                                                  e->RowIndex, true);
		// 微調整			
		rect.Y += 5;
		rect.Height -= 8;
		
		// StringFormatオブジェクトの作成
		StringFormat^ sf = gcnew StringFormat();

		sf->Trimming = StringTrimming::EllipsisPath;
		
		e->Graphics->DrawString(s, 
                                          dgv->RowHeadersDefaultCellStyle->Font, 
                                          Brushes::Black, 
                                          rect, 
                                          sf);

		// 描画完了
		e->Handled = true;
	}
}

解決済み
返信 編集キー/


管理者用

- Child Tree -