WinForm中中DataGridView添加添加,删除删除,修改操作具体方法修改操作具体方法
1.添加操作,代码如下:
代码如下:
IList<SelfRun> selfRunConfigs = new List<SelfRun>();
private void btnNewConfig_Click(object sender, EventArgs e)
{
try
{
string _lampNo = UpDownSelfLampNo.Value.ToString();
int _ctrlGpNo = Convert.ToInt16(UpDownCtrlGpCnt.Value);
string _opWay = string.Format(“{0}”, rbConfig.Checked == true ? 1 : 0);
string _opCtuch = GetSelectedCtuCh();
if (CheckNewConfigIsLega(_ctrlGpNo, _opCtuch))
{
string _opType = rbCgOpen.Checked == true ? “01” : rbCgClose.Checked == true ? “00” : “02”;
selfRunConfigs.Add(new SelfRun(_opCtuch, _opType, Convert.ToInt32(UpDownTime.Value)));
}
BindGridViewForIList<SelfRun>(gcConfigShow, selfRunConfigs);
}
catch (Exception ex)
{
MessageBox.Show(string.Format(“新增配置失败,原因:{0}”, ex.Message.Trim()));
}
}
private void BindGridViewForIList<T>(DataGridView gv, IList<T> datasource)
{
BindingList<T> _bindinglist = new BindingList<T>(datasource);
BindingSource _source = new BindingSource(_bindinglist, null);
gv.DataSource = _source;
}
SelfRun实体类代码如下:
代码如下:
public struct SelfRun
{
public SelfRun(string _opCtuCh, string _opWay, int _opTime)
: this()
{
OpCtuCh = _opCtuCh;
OpWay = _opWay;
OpTime = _opTime;
}
public string OpCtuCh
{
get;
set;
}
public string OpWay { get; set; }
public int OpTime { get; set; }
}
界面绑定,如图: