2016-07-19 3 views
0

오른쪽에 이미지 열이있는 Gridview가 있습니다. 사용자가 확인란을 선택하면 Null 이미지가 아닌 항목 만 표시되어야합니다.데이터베이스 값이 Null 인 경우 GridView 이미지 열 숨기기

행에 해당하는 데이터베이스 이미지가 비어있는 경우 Gridview가 기본 이미지를 사용하는 것으로 나타났습니다.이 작업을 위해 새 저장 프로 시저를 작성해야하거나 더 좋은 방법이 있습니다.

는 현재이

try 
{ 
    if (checkBox1.Checked == true) 
    { 
     dgvGetData.Columns["image"].Visible = true; 
     foreach (DataGridViewRow row in dgvGetData.Rows) 
     { 
      Console.WriteLine("LOOP"); 
      if (row.Cells[16].Value == null) 
      { 
       Console.WriteLine("######################################> NULL"); 
       row.Visible = false; 
      } 
      else 
      { 
       Console.WriteLine("######################################> NOT NULL"); 
      } 
     } 
    } 
    else 
    { 
     dgvGetData.Columns["image"].Visible = false; 
    } 
} 
catch (Exception error) 
{ 
    MessageBox.Show(error.Message); 
} 

답변

0

나는 당신이 CellFormatting 이벤트 핸들러를 사용하는 것이 좋습니다 것 구현 한 I. 몇 줄의 코드를 작성하여 올바른 열에 있는지 확인한 다음 원하는 열을 표시 할 수 있습니다.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (e.Value == null && dataGridView1.Columns[e.ColumnIndex].Name == "Image") 
     { 
      dataGridView1.Rows[e.RowIndex].Visible = false; 
     } 
    } 

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellformattingeventhandler(v=vs.110).aspx

: 여기

부분적인 예는
관련 문제