2014-10-23 7 views
2

나는이 문제와 유사한 몇 가지 게시물을 보았다,하지만 난 ... 내가 나오면 무엇을보고 다시 떠 것이라고 생각, 그래서 내 대답을 마련하지 않은확인 체크 박스 프로그래밍

C# .NET을 사용하여 Excel과 API를 통합하기 위해 ExcelDNA를 사용하고 있습니다. DataGridView가 있고 목록에 이미있는 항목을 확인하고 싶습니다.

다음 코드는 버튼 클릭 이벤트와 관련되어 있지만 코드가 메서드에서 호출 될 때는 작동하지 않습니다.

private void SelectAllItems() 
{ 
    foreach (DataGridViewRow row in itemsBySupplier.Rows) 
    { 
     DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"]; 
     check.Value = check.TrueValue; 
    } 
} 

는 나도 다른 곳에서 같은 문제로 실행하고 있습니다 :

foreach (DataGridViewRow row in itemsBySupplier.Rows) 
{ 
    DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"]; 

    string hid = row.Cells["Hid"].Value.ToString(); 
    if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0) 
    { 
     check.Value = check.TrueValue; 
    } 
} 

나는 완전히 비어오고, 몇 시간 동안이 연구 했어요. 어떤 도움이라도 대단히 감사하겠습니다.

+0

값을 true 또는 false로 설정하면됩니다. DataGridView.Rows [0] .Cells [0] .Value = true; 예제는 셀을 검사합니다. – deathismyfriend

+0

코드를 게시 할 수 있습니다. 이벤트에 넣을 때 작동하지 않는다고 명시한 코드에 대해 실행중인 이벤트가있을 수 있습니다. 아마도 뭔가 잘못 접근하고 있습니다. – MethodMan

답변

3

값을 true로 설정하면됩니다. 확인란 셀에 캐스트 할 필요가 없습니다.

private void SelectAllItems() 
    { 
     foreach (DataGridViewRow row in itemsBySupplier.Rows) 
     { 
      // This will check the cell. 
      row.Cells["selectItem"].Value = "true"; 
     } 
    } 

foreach (DataGridViewRow row in itemsBySupplier.Rows) 
{ 
    string hid = row.Cells["Hid"].Value.ToString(); 

    if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0) 
    { 
     row.Cells["selectItem"].Value = "true";       
    } 
}