2009-10-29 4 views
1

Sharepoint 용 webpart에서 작성 중이므로 문제가되는 Datagrid를 생성해야합니다.Itemtemplate Asp Datagrid의 그림 (코드)

상황은 내가 Dataview를 얻고, Gris를 생성하고 데이터를 바인딩한다는 것입니다. 하나의 열에 이미지가 표시되어야하므로 항목 템플릿이있는 템플릿 열을 생성해야합니다. 내가 항목 템플릿을 만들 수 있지만 어떻게 DataView를의 "imgUrl에"에 대한 코드와 결합 키우면 이미지 URL 하나를 구성하는 ASP에 대한 예를 수십을 발견

//Instantiate the DataGrid, and set the DataSource 
_grdResults = new DataGrid(); 
_grdResults.AutoGenerateColumns = false; 
_grdResults.DataSource = view; 
TemplateColumn colPic = new TemplateColumn(); 
colPic.HeaderText = "Image"; 

:

그래서 코드는 다음과 같습니다 ? 어떤 조언을

덕분에

르네

답변

1

당신은 ITemplate 인터페이스 것을 구현하는 클래스를 작성해야합니다.

public class TemplateImplementation : ITemplate 
{ 
    public void InstantiateIn(Control container) 
    { 
     Image image = new Image(); 
     image.DataBinding += Image_DataBinding; 
     container.Controls.Add(image); 
    } 

    void Image_DataBinding(object sender, EventArgs e) 
    { 
     Image image = (Image)sender; 
     object dataItem = DataBinder.GetDataItem(image.NamingContainer); 
     // If the url is a property of the data item, you can use this syntax 
     //image.ImageUrl = (string)DataBinder.Eval(dataItem, "ThePropertyName"); 
     // If the url is the data item then you can use this syntax 
     image.ImageUrl = (string)dataItem; 
    } 
} 

그런 다음 ItemTemplate을이 클래스의 인스턴스로 설정하십시오.

colPic.ItemTemplate = new TemplateImplementation(); 
+0

여러 열을 사용해야 할 경우 InstantiateIn 및 DataBinding 메서드에 대한 대리자를 사용하는 GenericTemplate 클래스를 만들고 싶을 수 있습니다. –

+0

완벽한 두 번째 순간에 나는 혼자서 그것을 발견했다 ;-) –