DataGridView 사용자 정의 열, 셀 및 리 로드 인터페이스 함수
열의 요 구 는 모든 칸 에서 A 를 누 르 면 우 위 를 나타 내 고 B 는 양 을 나타 내 며 C 는 D 는 합격 을 나타 내 고 E 는 불합격 을 나타 내 며 모든 칸 의 값 은 A, B, C, D, E 이 고 표시 값 은 우, 량, 중, 합격, 불합격 이다.원래 DataGridViewCell 의 Value 와 Formatted Value 속성 은 이 작업 을 완성 할 수 있 습 니 다. 그러나 이 속성 은 즉시 변환 되 는 것 이 아 닙 니 다. 예 를 들 어 A 를 입력 하고 현재 편집 컨트롤 을 떠 날 때 Value 값 은 DataGridViewCell 에 전달 되 어야 Formatted Value 가 좋 습 니 다 (이것 은 실현 과정 입 니 다). 사용자 가 본 것 은 A 를 누 르 고 컨트롤 을 편집 한 것 입 니까? A 를 누 른 것 입 니까?다른 셀 로 바 뀔 때 까지 아까 의 셀 이 우 로 나 타 났 습 니 다. 이런 화면 은 우호 적 이지 않 아서 열 을 다시 정의 하 겠 다 는 생각 이 들 었 습 니 다.
사용자 정의 열의 실현 과정.
첫 번 째 단계: 열 을 사용자 정의 하여 사용자 정의 셀 을 이 열 에 노출 합 니 다.
두 번 째 단계: 사용자 정의 셀, 사용자 정의 편집 컨트롤 을 이 셀 에 노출 합 니 다.(설명, DataGridView 에서 BeginEdit () 를 실행 하기 전에 이 셀 이 표 시 됩 니 다. 하지만 이 때 는 편집 상태 에 들 어가 지 않 았 습 니 다)
세 번 째 단계: 편집 컨트롤 을 사용자 정의 하고 인터페이스 함 수 를 실현 합 니 다.(설명, 이것 이 바로 BeginEdit ()그 다음 에 들 어 가 는 셀 은 DataGridViewColumn 에서 한 열 에 표 시 된 셀 은 하나의 컨트롤 입 니 다. 하 얀 점 은 편집 상태 에 들 어간 후에 Cell 의 값 을 하나의 컨트롤 에 전달 한 다음 에 이 컨트롤 이 값 을 편집 하고 완성 한 후에 값 을 Cell 에 되 돌려 주 는 것 입 니 다. 즉, 이 열 은 하나의 컨트롤 입 니 다. 아무리 셀 이 있 더 라 도 실 용적 인 컨트롤 입 니 다.만약 에 여러 열 이 같은 열 이 라면 이 여러 열 은 같은 컨트롤 입 니 다. 그리고 하 얀 점, 예 를 들 어 제 모든 열 이 DataGridView TextBox Column 입 니 다. 그러면 제 첫 번 째 줄 의 세 번 째 열 은 편집 상태 에 들 어간 후에 두 번 째 줄 의 첫 번 째 열 과 편집 상태 에 들 어간 후에 열 린 것 은 같은 컨트롤 입 니 다. 컨트롤 에 전달 하 는 값 이 다 를 뿐 입 니 다.)
네 번 째 단계: 시스템 이 자체 적 으로 가지 고 있 는 TextBox 컨트롤 이 제 요 구 를 만족 시 키 지 못 하기 때문에 지난 단계 에 편집 한 컨트롤 은 사용자 정의 TextBox 컨트롤 을 계승 하 는 것 입 니 다.
다음은 소스 코드 입 니 다. 어떤 과정 은 제 가 있 는 이곳 의 특별한 요구 입 니 다. 필요 한 것 이 아 닙 니 다. 주의 하 세 요.
1. DataGridView LevelTextBoxColumn 은 사용자 정의 열 로 DataGridView TextBoxColumn 에서 계승 합 니 다.
2. DataGridView LevelTextBoxCell 은 사용자 정의 셀 로 DataGridView TextBoxCell 에서 계승 합 니 다.여기 서 설명해 야 할 것 은 제 가 이 칸 의 Value 를 ABCDE 로 요구 하기 때문에 우, 량, 중, 합격, 불합격 으로 나 타 났 습 니 다. 그래서 저 는 SetLevelValue (), GetValue (), GetFormatted Value () 방법 을 다시 썼 습 니 다. 이 세 가지 방법 은 개인의 수요 에 따라 나 온 것 입 니 다. 그 밖 에 필요 한 방법 이 있 습 니 다. 즉 Initialized Editing Control 방법 입 니 다.사용자 정의 편집 컨트롤 을 초기 화 합 니 다. 이 컨트롤 에 자신의 Value 를 전달 해 야 합 니 다. 저 는 TextBox 컨트롤 을 사용 하기 때문에 값 은 Text 속성 입 니 다. 컨트롤 을 사용 하면 속성 을 주어 야 합 니 다.
3. LevelTextBox DataGridView EditingControl 은 사용자 정의 편집 컨트롤 입 니 다. 하나의 컨트롤 을 계승 하고 IDataGridView EditingControl 인 터 페 이 스 를 실현 해 야 합 니 다. 시스템 자체 의 TextBox 컨트롤 이 제 요 구 를 만족 시 키 지 못 하기 때문에 제 가 사용자 정의 LevelTextBox 컨트롤 을 계승 합 니 다. IDataGridView EditingControl 인터페이스 의 대부분 은 수정 할 필요 가 없습니다.주의해 야 할 점 은 OnTextChanged 이 벤트 를 다시 불 러 오 는 것 입 니 다. 이 컨트롤 에서 수정 한 값 이 this. Editing Control DataGridView 가 없 으 면 Notify Current CellDirty (true) 입 니 다.이렇게 하면 DataGridViewCell 에 값 을 전달 하지 않 습 니 다. 즉, 당신 이 무엇 을 수정 하 든 DataGridViewCell 의 값 은 변 하지 않 습 니 다. DataGridView 에 알려 야 합 니 다. "여보세요, DataGridView, 제 값 이 바 뀌 었 으 니 당신 도 고 쳐 야 합 니 다." OnTextChanged 이 벤트 를 왜 다시 불 러 오 는 지 에 대해 서 는...물론 제 가 LevelTextBox 컨트롤 을 물 려 받 았 고 TextBox 컨트롤 을 물 려 받 았 기 때 문 입 니 다. 다른 컨트롤 이 라면 OnValue Changed 일 수도 있 습 니 다.
4. LevelTextBox 는 제 가 사용자 정의 한 컨트롤 입 니 다. TextBox 컨트롤 을 계승 합 니 다. 여 기 는 OnKeyDown 과 OnKeyPress 두 개의 이벤트 만 다시 불 러 옵 니 다. 이것 은 제 가 컨트롤 에 A 를 누 르 면 우 위 를 표시 하 라 고 요구 하기 때문에 OnKeyDown 이 벤트 를 다시 불 러 옵 니 다. 왜 OnKeyPress 이 벤트 를 다시 불 러 옵 니까? 여러분 은 한 가지 만 했 습 니 다. 바로 e. Handled = true 입 니 다.버튼 과정 이 끝 났 음 을 표시 합 니 다. 다음 버튼 이 벤트 를 실행 하지 마 십시오.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WinEasV2.Controls
{
public class DataGridViewLevelTextBoxColumn : DataGridViewTextBoxColumn
{
public DataGridViewLevelTextBoxColumn()
{
this.CellTemplate = new DataGridViewLevelTextBoxCell();
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
DataGridViewLevelTextBoxCell cell = value as DataGridViewLevelTextBoxCell;
if (value != null && cell == null)
{
throw new InvalidCastException("Value provided for CellTemplate must be of type TEditNumDataGridViewCell or derive from it.");
}
base.CellTemplate = value;
}
}
}
public class DataGridViewLevelTextBoxCell : DataGridViewTextBoxCell
{
public DataGridViewLevelTextBoxCell()
{
}
private static Type defaultEditType = typeof(LevelTextBoxDataGridViewEditingControl);
private static Type defaultValueType = typeof(System.String);
public override Type EditType
{
get { return defaultEditType; }
}
public override Type ValueType
{
get
{
Type valueType = base.ValueType;
if (valueType != null)
{
return valueType;
}
return defaultValueType;
}
}
/// <summary>
/// 。
/// </summary>
/// <param name="rowIndex"></param>
/// <param name="initialFormattedValue"></param>
/// <param name="dataGridViewCellStyle"></param>
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
LevelTextBoxDataGridViewEditingControl ctl =
DataGridView.EditingControl as LevelTextBoxDataGridViewEditingControl;
ctl.Text =(string) SetLevelValue();
}
protected object SetLevelValue()
{
switch (this.Value.ToString())
{
case "A":
return " ";
case "B":
return " ";
case "C":
return " ";
case "D":
return " ";
case "E":
return " ";
default:
return "";
}
}
protected override object GetValue(int rowIndex)
{
switch (base.GetValue(rowIndex).ToString())
{
case " ":
return "A";
case " ":
return "B";
case " ":
return "C";
case " ":
return "D";
case " ":
return "E";
default:
return "";
}
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
switch (base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context).ToString())
{
case "A":
return " ";
case "B":
return " ";
case "C":
return " ";
case "D":
return " ";
case "E":
return " ";
default:
return "";
}
}
}
public class LevelTextBox : TextBox
{
public LevelTextBox() : base() { }
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
switch (e.KeyCode)
{
case Keys.A:
{
this.Text = " ";
e.Handled = true;
break;
}
case Keys.B:
{
this.Text = " ";
e.Handled = true;
break;
}
case Keys.C:
{
this.Text = " ";
e.Handled = true;
break;
}
case Keys.D:
{
this.Text = " ";
e.Handled = true;
break;
}
case Keys.E:
{
this.Text = " ";
e.Handled = true;
break;
}
default:
{
this.Text = "";
e.Handled = true;
break;
}
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
e.Handled = true;
}
}
public class LevelTextBoxDataGridViewEditingControl : LevelTextBox, IDataGridViewEditingControl
{
private DataGridView dataGridView; // grid owning this editing control
private bool valueChanged = false; // editing control's value has changed or not
private int rowIndex; // row index in which the editing control resides
#region IDataGridViewEditingControl
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.TextAlign = HorizontalAlignment.Center;
}
public DataGridView EditingControlDataGridView
{
get
{
return this.dataGridView;
}
set
{
this.dataGridView = value;
}
}
public object EditingControlFormattedValue
{
get
{
return GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting);
}
set
{
this.Text = (string)value;
}
}
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
public bool EditingControlValueChanged
{
get { return this.valueChanged; }
set { this.valueChanged = value; }
}
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Right:
case Keys.Left:
case Keys.Down:
case Keys.Up:
case Keys.Home:
case Keys.End:
case Keys.Delete:
return true;
}
return !dataGridViewWantsInputKey;
}
public Cursor EditingPanelCursor
{
get { return Cursors.Default; }
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.Text;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
protected override void OnTextChanged(EventArgs e)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(e);
}
#endregion
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.