2016-11-11 1 views
0

datagridview에 체크 박스의 값을 가져 오는 데 문제가 있습니다. 나는 이드에게 모든 확인란을 선택하고 싶지만 최신 체크 만 받았다.체크 박스에서 값을 가져 오는 중 DataGridview가 표시됨 C#

string Id = ""; 

ds_utilityTableAdapters.tbl_membersTableAdapter tam = new ds_utilityTableAdapters.tbl_membersTableAdapter();  
ds_utility.tbl_membersDataTable dtm = new ds_utility.tbl_membersDataTable(); 

foreach (DataGridViewRow row in dgv_members.Rows)  
{   
    if (row.Cells[0].Value != null && (Boolean)row.Cells[0].Value == true) 
    { 
     Id= row.Cells[1].Value.ToString(); 

     // using Id to display data in crystal report viewer 
     // but only read the latest checkbox value 

     dtm = tam.GetDataBy_SearchId(Id); 

     // in my dataset (ds_utility), I wrote the query like this 
     // SELECT * FROM tbl_members WHERE (Id = ?) 
    } 
} 

어떻게 모든 값을 얻을 수있는 쿼리를 작성하기 : 여기

내 코드?

하는 ds_utility.tbl_membersDataTable의 목록을 작성하고 각 반복 한 후 목록에 테이블을 추가 : 내가 당신을 보는 것과

+0

. 따라서 각 반복 후에 현재 ID의 데이터가 포함됩니다. 당연히 마지막 반복 이후에는 마지막 ID의 데이터 만 포함합니다. – Vax

+0

@vaxlt 답장을 보내 주셔서 감사합니다 .. "dtm"을 어디에 두어야하는지, 또는 모든 값을 얻기 위해 올바른 쿼리를 작성하는 방법에 대해 더 설명해 주실 수 있습니까? – Zeera

답변

0

여러 옵션이 있습니다. 이것은 가장 간단한 방법이지만 여러 테이블을 먼저 반복해야하기 때문에 앞으로 문제가 발생할 수 있습니다. 그것은 다음과 같이 보일 것이다 : 귀하의 경우

string Id = ""; 

ds_utilityTableAdapters.tbl_membersTableAdapter tam = new ds_utilityTableAdapters.tbl_membersTableAdapter();  List<ds_utility.tbl_membersDataTable> dtm = new List<ds_utility.tbl_membersDataTable>(); 

foreach (DataGridViewRow row in dgv_members.Rows)  {   if (row.Cells[0].Value != null && (Boolean)row.Cells[0].Value == true) { 
    Id= row.Cells[1].Value.ToString(); 

    // using Id to display data in crystal report viewer 
    // but only read the latest checkbox value 

    dtm.Add(tam.GetDataBy_SearchId(Id)); 

    // in my dataset (ds_utility), I wrote the query like this 
    // SELECT * FROM tbl_members WHERE (Id = ?) } } 

그러나 가장 좋은 방법은 SQL 쿼리를 변경하는 대신 하나의 아이디를 전달하는 이드의의 연결된 목록을 전달하는 것입니다. 당신은 다음과 같이 당신의 SQL을 수정하여이 작업을 수행 할 수 있습니다 :

SELECT * FROM tbl_members WHERE Id in (?) 

그리고처럼 보이도록 코드를 변경 :

당신은 루프에서 "DTM"개체를 덮어 쓰는 것 같습니다
string Id = ""; 

ds_utilityTableAdapters.tbl_membersTableAdapter tam = new ds_utilityTableAdapters.tbl_membersTableAdapter();  
ds_utility.tbl_membersDataTable dtm = new ds_utility.tbl_membersDataTable(); 
List<string> idList = new List<string>(); 

foreach (DataGridViewRow row in dgv_members.Rows)  
{   
    if (row.Cells[0].Value != null && (Boolean)row.Cells[0].Value == true) 
    { 
     Id= row.Cells[1].Value.ToString(); 

     // using Id to display data in crystal report viewer 
     // but only read the latest checkbox value 

    idList.Add(Id); 
    } 
} 

dtm = tam.GetDataBy_SearchId(string.Join(",", idList)); 
+0

나는 시험해 보았지만 작동하지 않았다. – Zeera

관련 문제