사용자 지정 MyDataGridViewCheckBoxCell을 만들고 GetFormattedValue를 재정 의하여 컬렉션의 존재 셀 값에 따라 true \ false를 반환 할 수 있습니다. SetValue를 사용하여 컬렉션을 수정합니다. 아래 예제가 잘 작동하는지 확인하십시오. 그것은이 작업을 수행하는 가장 좋은 방법이지만 그이 도움이 확인 :
private static List<string> ItemsList = new List<string>();
...
// fill in collection list
ItemsList.AddRange(new string[] { "a", "c" });
// create columns
DataGridViewTextBoxColumn column0 = new DataGridViewTextBoxColumn()
{ HeaderText = "column1", DataPropertyName = "Column1"};
DataGridViewCheckBoxColumn column1 = new NewDataGridViewCheckBoxColumn()
{ HeaderText = "column2", DataPropertyName = "Column2"};
dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { column0, column1 });
// create data rows
dataSet1.Tables[0].Rows.Add(new object[] {"a", "a" });
dataSet1.Tables[0].Rows.Add(new object[] { "b", "b" });
dataSet1.Tables[0].Rows.Add(new object[] { "c", "c" });
...
// custom datagridview checkbox cell
public class MyDataGridViewCheckBoxCell : DataGridViewCheckBoxCell
{
public MyDataGridViewCheckBoxCell()
{
FalseValue = false;
TrueValue = true;
}
protected override Object GetFormattedValue(Object value,
int rowIndex,
ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
// check if value is string and it's in the list; return true if it is
object result = (value is string) ? Form1.ItemsList.IndexOf((string)value) > -1 : value;
return base.GetFormattedValue(result, rowIndex, ref cellStyle,
valueTypeConverter, formattedValueTypeConverter, context);
}
protected override bool SetValue(int rowIndex, Object value)
{
if (value!=null)
{
// change collection
if (value.Equals(true))
Form1.ItemsList.Add((string)Value);
else
Form1.ItemsList.Remove((string)Value);
// dump list into console
foreach (string item in Form1.ItemsList)
Console.Write("{0}\t", item);
Console.WriteLine();
}
return true;
}
}
// custom datagridview column
public class NewDataGridViewCheckBoxColumn : DataGridViewCheckBoxColumn
{
public NewDataGridViewCheckBoxColumn()
{
CellTemplate = new MyDataGridViewCheckBoxCell();
}
}
희망의 해키되어 있지 않은 경우 확실하지가
에 관하여