I have the following code, when I run it, I can't add a new line to the gridView, thanks for your help:
for (int i = 0; i < dt.Rows.Count; i++)
{
try
{
// Add into gridView
gridView1.AddNewRow();
// Set rowHandle
int rowHandle = gridView1.GetRowHandle(gridView1.DataRowCount);
if (gridView1.IsNewItemRow(rowHandle))
{
gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ID"], i);
gridView1.SetRowCellValue(rowHandle, gridView1.Columns["Name"], "Peter");
gridView1.SetRowCellValue(rowHandle, gridView1.Columns["Age"], 35);
// Update gridView
gridView1.UpdateCurrentRow();
}
else
{
Debug.Print("Not row new!");
}
}
catch (Exception ex)
{
Debug.Print("Exception ex: " + ex);
}
}
Tuhin PaulPosted Mar 31, 2023, 2:41 AM
The IBindingList interface is a standard interface in .NET that allows collections to provide notifications when items are added, removed, or changed. It is typically used to bind collections to UI controls like the GridView. In practical terms, this means that if you are using a custom data source or a data source that does not implement the IBindingList interface, the AddNewRow method will not work with that data source. In such cases, you may need to implement the IBindingList interface on your data source or use a different method to add new rows to the grid. The IBindingList interface has additional requirements beyond just supporting the AddNewRow method. For example, a data source that implements IBindingList must also support sorting and filtering of the data.
Dong Lam TrienPosted Mar 31, 2023, 2:29 AM
I send you an example you debug.
https://www.mediafire.com/file/vczuleidtss0i7a/AddItemGridView.rar/file
Avijeet SinghPosted Mar 28, 2023, 8:34 AM
Based on the provided code, it seems like you are trying to add new rows to a gridView control in a loop, and populate those rows with values from a DataTable
dt. However, you mentioned that you cannot add new lines to the gridView, and it's not clear what the exact problem is. Here are a few suggestions that may help:Make sure the gridView control is properly bound to a data source. If the gridView is not bound to a data source, you may not be able to add new rows.
Check if there are any exceptions being thrown in the code. The
catchblock in the provided code only prints out the exception message to the Debug output. You may want to add a breakpoint in the catch block to see if any exceptions are being thrown, and if so, investigate the cause of the exception.Try adding the new rows outside of the loop. Depending on how the gridView is configured, it may be more efficient to add all the rows at once instead of adding them one by one in a loop.
Consider using a binding source instead of directly modifying the gridView. A binding source can simplify the process of adding new rows and populating them with data. For example, you can bind the gridView to a binding source, and then add rows to the binding source using the
Addmethod.I hope these suggestions help you resolve the issue you are facing with adding new rows to the gridView control.
Dong Lam TrienPosted Mar 27, 2023, 2:14 AM
Hello everyone !
If I use the assignment gridControl1.DataSource = dt; I have done it, I want to use my above code, can you make it work ?
Vishal JoshiPosted Mar 23, 2023, 5:09 AM
Hello
The GridView.AddNewRow method adds a new row only if a data source supports the IBindingList interface. Thus, if this method does nothing on your side, it means that your underlying data source does not support the IBindingList interface. Check this point
Thanks
Naimish MakwanaPosted Mar 23, 2023, 4:00 AM
Hello Dong,
Some potential reasons why you may not be able to add a new record to your DevExpress gridView:
Check if your grid control is set to allow new rows. To enable the new row, you should set the grid control's OptionsView.NewItemRow property to true.
Ensure that the grid control is bound to a data source. You can set the DataSource property of the grid control to the DataTable object containing your data.
Make sure that the grid control is in edit mode before trying to add a new row. You can call the GridView.FocusedColumn.View.FocusedEditor() method to activate the editor and put the grid control into edit mode.
Check if there are any validation errors preventing you from adding a new row. You can check the GridView.ValidateRow event to validate the current row before adding a new record.
Thanks
Naimish Makwana
Tuhin PaulPosted Mar 23, 2023, 3:04 AM
After binding the GridView to a data source, you can add new rows to the data source, and the GridView will automatically display them. You can modify your loop to add new rows to the data source.
This will add 10 new rows to the data source and display them in the GridView. Note that the column names in the code above ("ID", "Name", and "Age") should match the actual column names in the data source.
Tuhin PaulPosted Mar 23, 2023, 3:04 AM
I think the loop is trying to add new rows to a GridView control and set their values. However, the new rows are not being added to the GridView. One possible reason for this issue could be that the GridView control is not bound to a data source. To add new rows to a GridView, it should be bound to a data source such as a DataTable or a BindingList. If the GridView control is not bound to a data source, you can bind it to a DataTable.