2011-10-10 7 views
5

Winforms datagridview 셀을 CellValidating으로 유효성을 검사하고 싶습니다. 사용자가 값을 올바르게 설정하지 않은 경우 ErrorText을 설정하고 e.Cancel을 사용하여 커서가 셀에 남아 있도록합니다. 이제 오류 기호 (및 오류 텍스트)이 셀에 표시되지 않는 문제가 있습니다. e.Cancel을 삭제하면 셀이 초점을 잃고 오류 기호가 표시됩니다. 셀을 편집 모드로 유지하고 오류 기호도 표시되도록하려면 어떻게해야합니까?Winforms : DataGridview의 셀 유효성 검사 문제

if (...) 
{ 
    this.datagridviewX.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Errortext"; 
    e.Cancel = true; 
} 
else 
{ 
    this.datagridviewX.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = ""; 
} 
+0

내가 말했듯이 당신이 묘사하는 것은 상자 밖의 행동이 아니기 때문에 당신은 이상한 일을해야합니다. 몇 가지 코드를 제공해 주시겠습니까? –

답변

9

표시되는 동작은 실제로 페인팅 문제로 인한 것이며 오류 아이콘이 표시되지 않아서가 아닙니다. 셀의 오류 텍스트를 설정하면 아이콘이 표시되지만 편집 모드에서 셀의 텍스트 상자가 아이콘 위에 그려 지므로 아이콘이 표시되지 않습니다.

this.datagridviewX.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Errortext"; 
e.Cancel = true; 

당신은이 :

this.datagridviewX.Rows[e.RowIndex].ErrorText = "Errortext"; 
e.Cancel = true; 

다른 옵션은 셀을 변경하는 일이 단순히 너무 대신 행의 오류 텍스트를 사용하는 것입니다 -

는이를 고정하기위한 두 가지 옵션이 있습니다 셀을 채우고 (편집 컨트롤을 움직여서) 아이콘을 그립니다.

실제로이 문제를 해결하기 위해이 기술을 발견했습니다. here 및 재생산 아래의 코드 (C#에서는 VB.Net이 아님). 아이콘을 제외하고는 아이콘이 편집 컨트롤을 이동하지 볼 수있는

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    if (string.IsNullOrEmpty(e.FormattedValue.ToString())) 
    { 
     DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; 

     cell.ErrorText = 
      "Company Name must not be empty"; 

     if (cell.Tag == null) 
     { 
      cell.Tag = cell.Style.Padding; 
      cell.Style.Padding = new Padding(0, 0, 18, 0); 
     } 
     e.Cancel = true; 

    } 
    else 
    { 
     dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty; 
    } 
} 

너무 이동 :

먼저 당신은 당신이 셀 패딩을 변경하는 몇 가지 코드를 추가하여 세포 검증하는 이벤트가있다! 그래서 우리는 또한 새로운 아이콘을 칠할 필요가 있습니다. 여기에 몇 가지입니다 -

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    if (!string.IsNullOrEmpty(dataGridView1[e.ColumnIndex, e.RowIndex].ErrorText)) 
    { 
     DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; 
     cell.ErrorText = string.Empty; 
     cell.Style.Padding = (Padding)cell.Tag; 
     cell.Tag = null; 
    } 
} 
나는이 새로운 그린 아이콘을 마우스를 통해 설정 무시 발견 된 게시물

: 당신이 셀을 편집 끝날 때

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (dataGridView1.IsCurrentCellDirty) 
    { 
     if (!string.IsNullOrEmpty(e.ErrorText)) 
     { 
      GraphicsContainer container = e.Graphics.BeginContainer(); 
      e.Graphics.TranslateTransform(18,0); 
      e.Paint(this.ClientRectangle, DataGridViewPaintParts.ErrorIcon); 
      e.Graphics.EndContainer(container); 
      e.Handled = true; 
     } 
    } 
} 

그런 다음 당신은 패딩을 재설정해야 그것을 다루는 개략적 인 코드는, 실제로 작동하도록 할 시간이 없기 때문에 고친다 고 생각되는 약간의 연한 캔디가 있습니다 - 제가 나중에 1 분이면 그것을 정리할 것입니다.

DataGridView.ShowCellToolTips = true로 설정하고 부울 inError를 도입하여 현재 편집 오류가 있는지 추적합니다. 그런 다음 MouseHover 이벤트를 처리합니다.

void dataGridView1_MouseHover(object sender, EventArgs e) 
{ 
    if (inError) 
    {     
     Point pos = this.PointToClient(Cursor.Position);    

     if (r.Contains(pos.X - 20, pos.Y - 5)) 
     {     
      t.Show("There was an error", dataGridView1.EditingControl, 3000); 
     } 
    } 
} 

해당 코드의 t는 폼 수준의 도구 설명 컨트롤이고 r은 사각형입니다. 내가 채울

셀 그림 처리기에서 다음과 같이 r에 :

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (dataGridView1.IsCurrentCellDirty) 
    { 
     if (!string.IsNullOrEmpty(e.ErrorText)) 
     {    
      GraphicsContainer container = e.Graphics.BeginContainer(); 

      r = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); 
      e.Graphics.TranslateTransform(18, 0); 
      e.Paint(this.ClientRectangle, DataGridViewPaintParts.ErrorIcon); 
      e.Graphics.EndContainer(container);    

      e.Handled = true; 
     } 
    } 
} 

나는 위치 지점에 마이너스 (20)와 마이너스 5에 대한 행복하지 않다 - 그게 내가 있었다면 내가 고쳐 줄 것입니다 조금 더.

+0

나는 dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex] .ErrorText =로 이것을 정확히 시도했다. ... – Kottan

+0

@Kottan은 약간의 코드를 제공하고 (아마도 너무 크지 않은 경우 전체 양식에 가장 좋음) 우리는 보기. –

+0

@Kottan - 오케이, 지금 당장있어. 귀하의 질문은 셀에 오류를 설정하고 행을 설정하지 않았 음을 분명히하지 않았습니다. 코드를 제공하는 것은 항상 좋은 일입니다. –

관련 문제