- PR -

GridViewに動的にカラムを追加する方法について

1
投稿者投稿内容
むろき
会議室デビュー日: 2008/11/07
投稿数: 2
投稿日時: 2008-11-07 15:44
お世話になってなります。

ASP.NETでGridViewを使い動的にTemplateFieldのカラムを追加したいのですが
うまくいきません。言語は「C#」を使っています。
以下のURLを参考にして、ItemTemplateにラベルを使い表示ができることを
確認しました。

【参考URL】
http://msdn.microsoft.com/ja-jp/library/system.web.ui.webcontrols.templatefield.templatefield(VS.80).aspx

今回は、ラベルではなくユーザーコントロールを貼りたいのですが、
ユーザコントロールを生成しても、
ユーザーコントロール側で貼っているテキストボックス等がnullのままになってしまいます。
下記のソースでユーザーコントロールを生成する記述
(ucLaborTimeBox ucLaborTimeBox1 = new ucLaborTimeBox();)の後に、
WindowsのコントロールのInitializeComponentメソッドのようなものを呼べばよいのではないか
と考えましたが、わかりませんでした。
どなたかわかる人がいたらご教授ください。

InstantiateInメソッドでは以下のように記述しています。

コード:
public void InstantiateIn(System.Web.UI.Control container)
{
	// Create the content for the different row types.
	switch (templateType)
	{
		case DataControlRowType.Header:
			// Create the controls to put in the header
			// section and set their properties.
			Literal lc = new Literal();
			lc.Text = "<b>" + columnName + "</b>";

			// Add the controls to the Controls collection
			// of the container.
			container.Controls.Add(lc);
			break;
		case DataControlRowType.DataRow:
			// Create the controls to put in a data row
			// section and set their properties.
			ucLaborTimeBox ucLaborTimeBox1 = new ucLaborTimeBox();

			// To support data binding, register the event-handling methods
			// to perform the data binding. Each control needs its own event
			// handler.
			ucLaborTimeBox1.DataBinding += new EventHandler(this.FirstName_DataBinding);
			//lastName.DataBinding += new EventHandler(this.LastName_DataBinding);

			// Add the controls to the Controls collection
			// of the container.
			container.Controls.Add(ucLaborTimeBox1);
			break;

		// Insert cases to create the content for the other 
		// row types, if desired.

		default:
			// Insert code to handle unexpected values.
			break;
	}
}



[補足]
ユーザーコントロールには、MultiViewを貼ってあります。
View1とView2が用意されており、
設定によりコントロールが切り替わるようになっています。
View1とView2にはテキストボックス等のコントロールが貼ってあります。

todo
ぬし
会議室デビュー日: 2003/07/23
投稿数: 682
投稿日時: 2008-11-07 17:21
方法 : プログラムによって ASP.NET ユーザー コントロールのインスタンスを作成する
http://msdn.microsoft.com/ja-jp/library/c0az2h86(VS.80).aspx

[ メッセージ編集済み 編集者: todo 編集日時 2008-11-07 17:22 ]
むろき
会議室デビュー日: 2008/11/07
投稿数: 2
投稿日時: 2008-11-07 18:23
解決しました!
ありがとうございます。
1

スキルアップ/キャリアアップ(JOB@IT)