gridcontrol 添加行删除行

时间:2024-09-29 22:05:32

一、添加行

注意,必须先绑定数据源(可以是一个空的Datatable);然后再执行添加行的动作,否则不会成功;

gridcontrol 添加行删除行

gridcontrol 添加行删除行

    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"], ""+DateTime.Now.ToString("HHmmss"));
view.SetRowCellValue(e.RowHandle, view.Columns["GCol2"], "" + 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();
}
}