DataGridView 아이콘 이 있 는 셀 구현 코드
7940 단어 DataGridView아이콘단원 격
C\#WinForm 이 자체 적 으로 가지 고 있 는 표 컨트롤 을 확장 하여 데이터 의 상하 경계 값 을 자동 으로 판단 하고 넘 침 을 표시 합 니 다.
여기 서 사용 하 는 방법 은 표 의 열 대상 을 확장 하 는 것 입 니 다:DataGridViewColumn.
1.클래스 생 성:DecimalCheckCell
/// <summary>
///
/// </summary>
public class DecimalCheckCell : DataGridViewTextBoxCell
{
private bool checkMaxValue = false;
private bool checkMinValue = false;
private decimal maxValue = 0;
private decimal minValue = 0;
public decimal MaxValue
{
get { return maxValue; }
internal set { maxValue = value; }
}
public decimal MinValue
{
get { return minValue; }
internal set { minValue = value; }
}
public bool CheckMaxValue
{
get { return checkMaxValue; }
internal set { checkMaxValue = value; }
}
public bool CheckMinValue
{
get { return checkMinValue; }
internal set
{
checkMinValue = value;
}
}
public override object Clone()
{
DecimalCheckCell c = base.Clone() as DecimalCheckCell;
c.checkMaxValue = this.checkMaxValue;
c.checkMinValue = this.checkMinValue;
c.maxValue = this.maxValue;
c.minValue = this.minValue;
return c;
}
protected override void Paint(Graphics graphics, Rectangle clipBounds,
Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// Paint the base content
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
value, formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
//
if (this.RowIndex < 0 || this.OwningRow.IsNewRow) // -1, ( )
return;
if (value == null) return;
decimal vCurValue = Convert.ToDecimal(value);
bool overValue = false;
Image img = null;
if (checkMaxValue)
{
overValue = vCurValue > maxValue;
img = VsTest.Properties.Resources.Undo; //
}
if (checkMinValue && !overValue)
{
overValue = vCurValue < minValue;
img = VsTest.Properties.Resources.Redo; //
}
//
if (overValue && img != null)
{
var vSize = graphics.MeasureString(vCurValue.ToString(), cellStyle.Font);
System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
graphics.SetClip(cellBounds);
graphics.DrawImageUnscaled(img, new Point(cellBounds.Location.X + (int)vSize.Width, cellBounds.Location.Y));
graphics.EndContainer(container);
}
}
protected override bool SetValue(int rowIndex, object value)
{
if (rowIndex >= 0)
{
try
{
decimal vdeci = Convert.ToDecimal(value); //
base.ErrorText = string.Empty;
}
catch (Exception ex)
{
base.ErrorText = " " + ex.Message;
return false;
}
}
return base.SetValue(rowIndex, value);
}
}
2.클래스 생 성:DecimalCheckColumn
/// <summary>
///
/// </summary>
public class DecimalCheckColumn : DataGridViewColumn
{
private bool checkMaxValue = false;
private bool checkMinValue = false;
private decimal maxValue = 0;
private decimal minValue = 0;
public decimal MaxValue
{
get { return maxValue; }
set
{
maxValue = value;
(base.CellTemplate as DecimalCheckCell).MaxValue = value;
}
}
public decimal MinValue
{
get { return minValue; }
set
{
minValue = value;
(base.CellTemplate as DecimalCheckCell).MinValue = value;
}
}
/// <summary>
/// , MaxValue
/// </summary>
public bool CheckMaxValue
{
get { return checkMaxValue; }
set
{
checkMaxValue = value;
(base.CellTemplate as DecimalCheckCell).CheckMaxValue = value;
}
}
/// <summary>
/// , MinValue
/// </summary>
public bool CheckMinValue
{
get { return checkMinValue; }
set
{
checkMinValue = value;
(base.CellTemplate as DecimalCheckCell).CheckMinValue = value;
}
}
public DecimalCheckColumn()
: base(new DecimalCheckCell())
{
}
public override object Clone()
{
DecimalCheckColumn c = base.Clone() as DecimalCheckColumn;
c.checkMaxValue = this.checkMaxValue;
c.checkMinValue = this.checkMinValue;
c.maxValue = this.maxValue;
c.minValue = this.minValue;
return c;
}
}
3.이제 사용 할 수 있 습 니 다.창 에 dataGridView 컨트롤 을 끌 고 다음 코드 를 추가 합 니 다.
private void TestForm_Load(object sender, EventArgs e)
{
InitControlsProperties(); //
//
DataTable dTabel = new DataTable();
dTabel.Columns.Add("ID",typeof(int));
dTabel.Columns.Add("TestValue",typeof(decimal));
Random rnd = new Random();
for (int i = 0; i < 10; i++) // 10
{
var vdr = dTabel.NewRow();
vdr[0] = i + 1;
vdr[1] = rnd.Next(50);
dTabel.Rows.Add(vdr);
}
this.dataGridView1.DataSource = dTabel;
}
private void InitControlsProperties()
{
DecimalCheckColumn ColumnRoleID = new DecimalCheckColumn();
ColumnRoleID.DataPropertyName = "ID";
ColumnRoleID.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
ColumnRoleID.Name = "ID";
ColumnRoleID.HeaderText = " ";
ColumnRoleID.Width = 50;
this.dataGridView1.Columns.Add(ColumnRoleID);
DecimalCheckColumn ColumnRoleName = new DecimalCheckColumn();
ColumnRoleName.DataPropertyName = "TestValue";
ColumnRoleName.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
ColumnRoleName.Name = "TestValue";
ColumnRoleName.HeaderText = " ";
ColumnRoleName.Width = 100;
ColumnRoleName.CheckMaxValue = true; //
ColumnRoleName.MaxValue = 41;
ColumnRoleName.CheckMinValue = true; //
ColumnRoleName.MinValue = 7;
this.dataGridView1.Columns.Add(ColumnRoleName);
//this.dataGridView1.AllowUserToAddRows = false;
//this.dataGridView1.AllowUserToDeleteRows = false;
//this.dataGridView1.ReadOnly = true;
this.dataGridView1.AutoGenerateColumns = false;
}
운행 효 과 는 아래 그림 왼쪽 과 같다.그럼 오른쪽 그림 은 뭐야?
아직 해결 되 지 않 은 문제 가 하나 더 있 습 니 다.기본적으로 처음 불 러 온 데 이 터 는 한계 초과 여 부 를 완전히 판단 할 수 없습니다.때로는 한두 개 로 판단 할 수 있 고,때로는 전혀 판단 할 수 없 지만,마우스 로 각 칸 을 클릭 하면 자동 으로 식별 할 수 있 습 니 다.당분간 문제 의 원인 을 발견 하지 못 했다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
DataGridView 아이콘 이 있 는 셀 구현 코드목적: C\#WinForm 이 자체 적 으로 가지 고 있 는 표 컨트롤 을 확장 하여 데이터 의 상하 경계 값 을 자동 으로 판단 하고 넘 침 을 표시 합 니 다. 여기 서 사용 하 는 방법 은 표 의 열 대상 을 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.