문제

여러 열과 행이있는 DataGridView가 있습니다. 첫 번째 열에는 확인란이 포함되어 있습니다. 사용자가 여러 확인란을 선택한 다음 작업을 수행 할 수 있기를 원합니다. 예를 들어 행 1과 2에서 확인란을 선택하면 행 1과 2의 다른 열의 데이터를 선택하여 메시지 상자로 전달할 수 있습니다.

이를 위해 CheckBox_Changed 이벤트를 사용해야한다는 것을 알고 있습니다. 그러나 여러 행 에서이 작업을 수행하는 방법을 해결하는 데 어려움이 있습니까?

도움이 되었습니까?

해결책

켜짐 버튼 클릭 이벤트 :

static int SelectColumnIndex = 0;
PerformAction_Click(object sender, System.EventArgs e)
{
    string data = string.Empty;
    foreach(DataGridViewRow row in MyDataGridView.Rows)
    {
      if(row.Cells[SelectColumnIndex].Value!=null &&
             Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
      {
        foreach(DataGridViewCell cell in row.Cells)
        {
          if(cell.OwningColumn.Index!= SelectColumnIndex)
          {
            data+= (cell.Value + " ") ; // do some thing
          }
        }
        data+="\n";
      }
   }
   MessageBox.Show(data, "Data");
}

다른 팁

사용자가 작업을 수행하기 위해 버튼을 클릭하려면 조치를 수행하는 경우 버튼이 변경된 이벤트가 아닌 버튼의 클릭 이벤트입니다. 버튼을 클릭하면 모든 행을 이동합니다. DataGridView 및 확인 된 확인란으로 행에 동작을 수행하십시오.

static int SelectColumnIndex = 0;

PerformAction_Click(object sender, System.EventArgs e)
{
    string data = string.Empty;    

    foreach(DataGridViewRow row in MyDataGridView.Rows)     
    {
        if(row.Cells[SelectColumnIndex].Value != null 
            &&
         Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)       
        {
            foreach(DataGridViewCell cell in row.Cells)        
            {
                if (cell.OwningColumn.Index != SelectColumnIndex)                               
                {
                    data+= (cell.Value + " ") ; // do some thing           
                 }
            }

            data+="\n";
        }
    }

    MessageBox.Show(data, "Data");
}

이 코드 로이 문제를 해결 한 이벤트가 필요하지 않다고 생각합니다.

//In Fill DataGridViewEvent :
        DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxColumn();
        ChCol.Name = "CheckedRow";
        ChCol.HeaderText = "انتخاب";
        ChCol.Width = 50;
        ChCol.TrueValue = "1";
        ChCol.FalseValue = "0";
        dg.Columns.Insert(0, ChCol);

// In Button Event put these codes:

            try
            {
                MessageBox.Show(row.Cells[2].Value.ToString() + " --> " +
                row.Cells["CheckedRow"].Value.ToString());
            }
            catch
            {
                // Nothing Act
            }


enter code here

이 매개 변수에 의한 출력 : itemid-> 0 // 확인되지 않은 값의 경우 itemid-> 1 // 확인 된 값의 경우

이제 당신은 당신의 행동에 대한 값으로 1을 반환하는 행을 필터링 할 수 있습니다. 나는이 코드를 테스트했으며 그것은 나를 위해 작동합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top