简体   繁体   中英

How to add new Row in DevExpress Gridview from DataTable on Button Click

I am new to C# and trying to know more by trying to develop simple Windows application. In the application i used the DevExpress GridView. i am trying to add the content of textbox control to DevExpress Unbounded Gridview on button click like this. I tried like this but nothing show up in the GridView.

Private void btn_Add_to_List_Click(object sender, EventArgs e)
    {            
        gridView1.AddNewRow();
    }

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[0], cBox_ProcessingMaterial.Text.ToString());
        view.SetRowCellValue(e.RowHandle, view.Columns[1], txtBox_Qty_Used.Text.ToString());


    }

This is the form i used

The version of DevExpress i am using is 16.2.4. i have seen the documentation but the only thing i get is for bounded GridView only. please i need help. Thanks!!

When you call AddNewRow it will fire an event - InitNewRow. In there you can initialise the values of the new row.

This appears to be covered in their documentation: https://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBaseColumnView_InitNewRowtopic

It includes an example, which is:

private void gridView1_InitNewRow(object sender, InitNewRowEventArgs e) {
   DevExpress.XtraGrid.Views.Grid.GridView view = sender as Grid.GridView;
   view.SetRowCellValue(e.RowHandle, view.Columns["PurchaseDate"], DateTime.Today);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM