데이터gridview에 디지털 형식 열을 추가합니다
6875 단어 datagridview
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace winclient
{
class numEdit:NumericUpDown,IDataGridViewEditingControl
{
private DataGridView grid;
private bool valuechanged = false;
private int rowindex;
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public DataGridView EditingControlDataGridView
{
get
{
return grid;
}
set
{
grid = value;
}
}
public object EditingControlFormattedValue
{
get
{
return this.Value;
}
set
{
if (value != null)
this.Value = Convert.ToDecimal(value);
}
}
public int EditingControlRowIndex
{
get
{
return rowindex;
}
set
{
rowindex = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valuechanged;
}
set
{
valuechanged = value;
}
}
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
protected override void OnValueChanged(EventArgs eventargs)
{
valuechanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
}
2. 셀 정의: (전체 코드)
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace winclient
{
class numcell:DataGridViewTextBoxCell
{
private numEdit ne;
public numcell() : base() { }
private int pointnum;
public int Pointnum
{
get { return pointnum; }
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(" 0 ");
}
if (this.pointnum != value)
{
pointnum = value;
}
}
}
private bool thousands=false;
public bool Thousands
{
get { return thousands; }
set { thousands = value; }
}
private decimal max=10000000;
public decimal Max
{
get { return max; }
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(" 0 ");
}
if (this.max != value)
{
max = value;
}
}
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
ne = DataGridView.EditingControl as numEdit;
ne.Value = Convert.ToDecimal(this.Value);
ne.DecimalPlaces = pointnum;
if (ne != null)
{
ne.Size = this.Size;
ne.DecimalPlaces = pointnum;
ne.Maximum = max;
ne.ThousandsSeparator = thousands;
}
}
public override Type EditType
{
get
{
return typeof(numEdit);
}
}
public override Type ValueType
{
get
{
Type valueType = base.ValueType;
if (valueType != null)
{
return valueType;
}
return ValueType;
}
}
protected override object GetValue(int rowIndex)
{
return base.GetValue(rowIndex);
}
public override object DefaultNewRowValue
{
get
{
return decimal.One;
}
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.ComponentModel.TypeConverter valueTypeConverter)
{
return base.ParseFormattedValue(formattedValue.ToString(), cellStyle, formattedValueTypeConverter, valueTypeConverter);
}
}
}
3. 열 정의: (전체 코드)
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace winclient
{
class numcolumn:DataGridViewColumn
{
public numcolumn() : base(new numcell()) { }
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(numcell)))
throw new InvalidCastException(" ");
base.CellTemplate = value;
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#DataGridView의 데이터를 Excel로 내보내기텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.