When I change a value in a cell the record is updated via a SQL Insert statement which works fine. When I don't change a value in a cell and just tab to the next cell, it removes all the current rows from the datagridview and renders a new row.
Loading

Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 8, 2024, 7:07 AM EST
It seems like you’re experiencing an unusual behavior with your DataGridView. This could be due to a variety of reasons, and without more specific details about your implementation, it’s hard to pinpoint the exact cause. However, here are a few general suggestions that might help:
Data Binding: Ensure that your DataGridView is properly bound to a data source. Adding rows one by one can lead to performance issues1. Instead, consider using a
BindingSourceor creating aDataTableand passing the data reader to itsLoadmethod1.Event Handling: Check your event handlers, especially those related to cell or row changes. An event might be triggering the removal and addition of rows unintentionally.
Debugging: Use debugging tools to step through your code and see where the issue might be occurring. This can help you understand if there’s a particular method or event causing the problem1.
Data Updates: Make sure that updates to your data source are being correctly reflected in your DataGridView. If you’re using a SQL Insert statement to update records, ensure that this operation is successful and that your DataGridView is refreshed afterwards to reflect these changes.
Here's a general example in C# using WinForms to demonstrate updating instead of inserting:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { // Check if the value has changed if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (cell.Value != null && !cell.Value.Equals(GetOriginalValue(e.RowIndex, e.ColumnIndex))) { // Update the record in the database using an Update statement UpdateRecord(e.RowIndex, e.ColumnIndex, cell.Value.ToString()); } } } private void UpdateRecord(int rowIndex, int columnIndex, string newValue) { // Implement your logic to update the record in the database // Use the rowIndex, columnIndex, and newValue to construct the SQL Update statement // Execute the SQL Update statement }Thanks
Pushpendra ShuklaRank #552 · 4.4K reputationPosted Jan 8, 2024, 6:57 AM EST
use update panel for showing updating records. your records are removed due to page refresh that why recodrs are distory.
you should be use repeater or datagrid both under the UpdatePanel .
// your data grid
UpdatePanel>
Sachin SinghRank #66 · 26.2K reputationPosted Jan 8, 2024, 10:50 AM EST
Test if this works
private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Tab) { e.Handled = true; // Prevent default Tab behavior SendKeys.Send("{Right}"); // Move focus to the next cell } }