유효성 검사 오류 (CellValidating 이벤트를 사용하여 셀의 ErrorText 속성을 사용하여 설정)가있는 경우 사용자가 DataGridView의 변경 내용을 저장하지 못하게하고 싶습니다.DataGridView HasErrors?
myDataGridView.HasErrors()
과 같은 방법을 찾고 있습니다 (볼 수는 없습니다)? 는 모습을 가지고있는 DataGridView를 확인하는 방법에 대한 예제가
유효성 검사 오류 (CellValidating 이벤트를 사용하여 셀의 ErrorText 속성을 사용하여 설정)가있는 경우 사용자가 DataGridView의 변경 내용을 저장하지 못하게하고 싶습니다.DataGridView HasErrors?
myDataGridView.HasErrors()
과 같은 방법을 찾고 있습니다 (볼 수는 없습니다)? 는 모습을 가지고있는 DataGridView를 확인하는 방법에 대한 예제가
도움 희망 행. 나는 문제가 이제 해결되었다고 생각하지만 여러 필드 검증 대처에 대한 동일한 문제에 직면 다른 사람들을 돕기 위해 내 솔루션을 추가 할 것입니다 게시 된 MSDN 예제 아리 ...
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
int newInteger;
if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
if (!int.TryParse(e.FormattedValue.ToString(),
out newInteger) || newInteger < 0)
{
e.Cancel = true;
dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
//If it's simple, do something like this here.
this.SubmitButton.Enabled = false;
//If not, set a private boolean variable scoped to your class that you can use elsewhere.
this.PassedValidation = false;
}
}
그러나 여러 열을 처리하지 못합니다. 그렇습니까? 예를 들어 첫 번째 셀의 유효성 검사에 실패하면 제출 버튼이 비활성화됩니다. 그러면 다음 셀이 유효성 검사를 통과합니다. 이전 셀이 실패하더라도 제출 버튼이 활성화됩니다. – Rezzie
제출 버튼이 명시 적으로 그렇게하지 않고 다시 활성화되는 이유는 무엇입니까? 위의 코드는 사용하는 정확한 코드가 아닙니다. 그것은 하나의 요점을 설명하기위한 것이 었습니다 (실패가 발생하면 원하는대로 무효화 할 수 있습니다). – Ocelot20
게시 한 코드로 셀이 유효성 검사에 실패 할 때마다 버튼을 비활성화하는 데 충분하지만 어떻게 다시 활성화할까요? – Rezzie
사용 :
이어서public bool validation1_valid = true;
public bool validation2_valid = true;
상기 데이터 그리드의 _CellValueChanged
방법 :
private void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
string cellHeader = gridView.Columns[e.ColumnIndex].HeaderText.ToString();
//if cell is one that needs validating
if (cellHeader == "Something" || cellHeader == "Something Else")
{
//if it isn't null/empty
if (gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null
&& gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != "")
{
//check if it's valid (here using REGEX to check if it's a number)
if (System.Text.RegularExpressions.Regex.IsMatch(gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), @"^(?:^\d+$|)$"))
{
//set the relevant boolean if the value to true if the cell it passes validation
switch (cellHeader)
{
case "Something":
validation1_valid= true;
break;
case "Something Else":
validation2_valid= true;
break;
}
//Add an error text
gridView.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Black;
gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = String.Empty;
gridView.Rows[e.RowIndex].ErrorText = String.Empty;
//DON'T disable the button here - it will get changed every time the validation is called
}
else //doesn't pass validation
{
//set boolean to false
switch (cellHeader)
{
case "Something":
validation1_valid = false;
break;
case "Something Else":
validation2_valid = false;
break;
}
gridView.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Red;
gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Value must be numeric.";
}
}
else //null or empty - I'm allowing these, so set error to "" and booleans to true
{
switch (cellHeader)
{
case "Something":
validation1_valid = true;
break;
case "Something Else":
validation2_valid = true;
break;
}
gridView.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Black;
gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = String.Empty;
gridView.Rows[e.RowIndex].ErrorText = String.Empty;
}
}
//if there is an invalid field somewhere
if (validation1_valid == false || validation2_valid == false)
{
//set the button to false, and add an error to the row
btnSave.Enabled = false;
gridView.Rows[e.RowIndex].ErrorText = "There are validation errors.";
}
//else if they're all vaild
else if (validation1_valid == true && validation2_valid == true)
{
//set the button to true, and remove the error from the row
btnSave.Enabled = true;
gridView.Rows[e.RowIndex].ErrorText = String.Empty;
}
}
입력이 유효한지 여부 whetehr
제가 저장하는 어떤 변수를 생성 pubilc
죄송합니다. 원래 질문에서 명확하지 않을 수 있습니다. 나는 벌써 셀을 검증하고, 적절한 곳에 ErrorText를 설정하고있다. 나는 단지 어떤 셀이 유효성 검사에 실패하여 사용자가 "저장"버튼을 클릭하는 것을 막을 수 있는지 알고 싶습니다. – Rezzie