gridcontrol 添加行删除行
一、添加行
注意,必须先绑定数据源(可以是一个空的Datatable);然后再执行添加行的动作,否则不会成功;
public partial class Form2 : Form { public Form2() { InitializeComponent(); this.gridView1.InitNewRow += gridView1_InitNewRow; } private void Form2_Load(object sender, EventArgs e) { //设置数据结构 DataTable dt = new DataTable(); dt.Columns.Add("GCol1", Type.GetType("System.String")); dt.Columns.Add("GCol2", Type.GetType("System.String")); //必须有此行代码,绑定数据源,否则gridView1添加不了数据行 this.gridControl1.DataSource = dt; } /// <summary> /// 添加行 执行动态绑定数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView; view.SetRowCellValue(e.RowHandle, view.Columns["GCol1"], "123"+DateTime.Now.ToString("HHmmss")); view.SetRowCellValue(e.RowHandle, view.Columns["GCol2"], "456" + DateTime.Now.ToString("HHmmss")); } /// <summary> /// 事件:添加按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { //添加一行的命令 this.gridView1.AddNewRow(); } }