DevExpress GridView执行AddRow函数时新增2条明细行问题求助
Hey there, let's break down this duplicate detail row issue you're hitting with DevExpress GridView—this is a super common quirk I’ve troubleshooted a few times before. Here are the most likely causes and fixes to check out:
1. Accidental Double Event Binding
First, check if your "Create Detail" button’s click event is being bound multiple times. This happens a lot in WinForms/WebForms when you bind events in a Load method without guards, leading to the click logic firing twice.
Example of the mistake (WinForms):
private void Form1_Load(object sender, EventArgs e) { // Binds the event EVERY time the form loads, not just once btnCreateDetail.Click += btnCreateDetail_Click; }
Fix:
Move the event binding to your form’s constructor (only runs once) or add a guard clause:
public Form1() { InitializeComponent(); // Binds once during initialization btnCreateDetail.Click += btnCreateDetail_Click; }
For WebForms, wrap the binding in an !IsPostBack check:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { btnCreateDetail.Click += btnCreateDetail_Click; } }
2. NewItemRowPosition Conflict
This is the #1 culprit for duplicate detail rows. If your detail GridView has OptionsView.NewItemRowPosition set to Top or Bottom, DevExpress automatically renders an empty new row for user input. When you manually call AddNewRow() on top of that, you get two rows total.
Check & Fix:
- Open your detail GridView’s properties, find
OptionsView > NewItemRowPosition, and set it toNoneif you want to handle new rows manually. - Or, use the existing NewItemRow instead of calling
AddNewRow():private void btnCreateDetail_Click(object sender, EventArgs e) { var selectedMasterRow = gridViewMaster.GetFocusedRow() as YourMasterModel; if (selectedMasterRow != null) { // Focus the auto-generated NewItemRow gridViewDetail.FocusedRowHandle = GridControl.NewItemRowHandle; // Populate data directly into this row gridViewDetail.SetRowCellValue(GridControl.NewItemRowHandle, "MasterId", selectedMasterRow.Id); // Fill other fields as needed } }
3. Unintended Double Calls to AddNewRow()
Double-check your button click logic—you might be calling gridViewDetail.AddNewRow() twice by accident. It’s easy to miss if you have nested conditionals or copied code.
Example of the mistake:
private void btnCreateDetail_Click(object sender, EventArgs e) { var masterRow = gridViewMaster.GetFocusedRow() as YourMasterModel; if (masterRow != null) { // Oops, two calls here! gridViewDetail.AddNewRow(); gridViewDetail.AddNewRow(); // Populate row data... } }
4. Indirect Event Triggers
Sometimes other events (like master row selection changes, or data source refreshes) can indirectly trigger AddNewRow(). For example:
- If you have logic in
gridViewMaster.SelectionChangedthat runs when you click the button (since the master row is already selected, but the event might fire again on postback). - Your detail data source (like a
BindingSource) is being refreshed twice, causing rows to duplicate.
Troubleshoot:
Add breakpoints to all places where AddNewRow() is called, and check the call stack to see if unexpected events are triggering the method.
Start with checking the NewItemRowPosition setting—that’s almost always the fix for this exact issue. If that doesn’t work, walk through the other points step by step.
内容的提问来源于stack exchange,提问作者user3127554

