2013-09-06 2 views
0

DataGridView이 제 바인딩 어댑터 중 하나에 바인딩되어 있습니다. 내 첨부 파일에 "type" (즉, ".pdf")에 해당하는 열이 있습니다. 이것은 격자보기 열에 텍스트로 표시됩니다 (예상대로). 유형을 나타내는 이미지가되도록 열 값을 변경할 수 있어야합니다. 예를 들어 유형이 PDF 인 경우 ".pdf"이 아닌 열에 PDF 이미지가 있어야합니다.바인딩시 동적으로 열 유형 변경

셀을 추가 할 때 동적으로이 작업을 수행 할 수있는 방법이 있습니까? 아니면 모든 세포가로드 된 후에해야 할 일이 필요합니까?

건배.

+1

당신이 아직 아이디어를 시도했지만 이유를 대신 템플릿 필드를 사용 : 셀에 이미지를 그리려면 이벤트 CellPainting를 처리 할 수 ​​있고, 여기에 코드입니다 당신은 시도 할 수 있습니다? 'BoundField'는'imagebutton'을 사용합니다. 그러나 이것은'PdF'에 대한 상수 이미지를 주거나'RowBound' 이벤트에 이미지를 동적으로 추가하거나 이미'Pdf'에 대한 이미지가 있습니까? –

+0

혼란스러운 질문입니까? 열 유형을 변경 하시겠습니까? 열 이름 또는 열 필드의 내용? – Remy

답변

0

예, 이미지를 사용하고 해당 이름의 아이콘이 몇 개 있습니다.

예. pdf.png,

는 다음과 같이 링크를 구축 word.png : 당신은 열 type 자신의 이미지를 그릴 필요가

<img src="<%# LinkRoot + Eval("type").ToString() + ".png" %>" height="32" width="32" /> 
0

을, 물론 그려진 이미지는 text (설명 파일에 대응 유형 : 예 : .pdf, .txt, ...). 모든 이미지를 직접 준비해야합니다. 알 수없는 파일 유형에 해당 이미지가 없으면 Unknown file type image을 사용할 수 있습니다. 예를 동적으로이 작업을 수행 할 수

//Dictionary to store the pairs of `text` and the corresponding image 
Dictionary<string, Image> dict = new Dictionary<string, Image>(StringComparer.CurrentCultureIgnoreCase); 
//load data for your dict 
dict["Unknown"] = yourUnknownImage;//This should always be added 
dict[".pdf"] = yourPdfImage; 
dict[".txt"] = yourTxtImage; 
//..... 
//CellPainting event handler for your dataGridView1 
//Suppose the column at index 1 is the type column. 
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){ 
    if(e.ColumnIndex == 1 && e.RowIndex > -1){ 
    var image = dict["Unknown"]; 
    if(e.Value != null) { 
     Image img; 
     if(dict.TryGetValue(e.Value.ToString(), out img)) image = img;    
    } 
    //Draw the image 
    e.Graphics.DrawImage(image, new Rectangle(2,2, e.Bounds.Height-4, e.Bounds.Height-4)); 
    } 
}