my program loads a table from sqlite3 and displays the content in a datagridview form with c#. I am sorting the table entries according to name and some values which gives out of order ID's to display in the datagridview. The program works fine if the ID column is visible. But when i use RangesDatagrid.Columns[0].Visible = false. This gives a null exception on the second time of deleting the first entry in the datagridview. When looking into the database, the entry is deleted dispite of the message. If i click on other entries in the gridview it works fine. Any ideas? This is the code for that part.
private void DeleteRangeBtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(ComplexNameTxt.Text.Trim()) || !string.IsNullOrWhiteSpace(RangeNameTxt.Text.Trim()))
{
DialogResult result;
if (RangesDatagrid.Rows.Count != 0)
{
string msg = "Do You Want To Permanently Delete The Selected Record?";
string title = "Artemis: " + "Deletion Confirmation";
result = MessageBox.Show(msg, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes)
{
return;
}
int rowValue = Convert.ToInt32(RangesDatagrid.Rows[RangesDatagrid.CurrentRow.Index].Cells[0].Value);
try
{
bool isGood = GlobalConfig.Connection.DeleteRange(rowValue);
if (!isGood)
{
throw new ArgumentOutOfRangeException(nameof(sender), "Returned false by the DeleteRange method.");
}
else
{
ClearFormData();
RefreshDataGridView();
DeleteCompleted();
}
}
catch (Exception ex)
{
string msg2 = $"The Record Has NOT Been Deleted \nThe application has the following Error: {ex.Message}";
string title2 = "Artemis: " + "Range Elevation";
MessageBox.Show(msg2, title2, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
##############
public bool DeleteRange(int Id)
{
bool outcome;
//Connect to database as per connection string in App.Config.
using (IDbConnection connection = new SqliteConnection(GlobalConfig.CnnString(db)))
{
int affectedrows = connection.Execute("DELETE FROM RANGES WHERE RangeID = @RangeID", new { RangeID = Id });
if (affectedrows > 0)
{
outcome = true;
}
else
{
outcome = false;
}
}
return outcome;
}
#######################
private void RefreshDataGridView()
{
if (AddNewRangeBtn.Text == "Cancel")
{
string msg = "Press the Cancel Button first before using Refresh!";
string title = "Artemis: " + "Refresh";
MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
List<RangeModel> rangeModels = GlobalConfig.Connection.GetRanges();
availableRanges = rangeModels;
RangesDatagrid.Columns.Clear();
RangesDatagrid.AutoSize = true;
RangesDatagrid.DataSource = availableRanges;
DisplayDataGridRow(); //Pass the first row to the method and populate the textbox's.
//Set the column headings.
RangesDatagrid.Columns[0].HeaderText = "ID";
RangesDatagrid.Columns[1].HeaderText = "Complex";
RangesDatagrid.Columns[2].HeaderText = "Range Name";
RangesDatagrid.Columns[3].HeaderText = "Distance";
RangesDatagrid.Columns[4].HeaderText = "Elevation";
RangesDatagrid.Columns[5].HeaderText = "Latitude";
RangesDatagrid.Columns[6].HeaderText = "Direction";
RangesDatagrid.Columns[7].HeaderText = "Notes";
//RangesDatagrid.Columns[0].Visible = false; //################################
RangesDatagrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
hope this is sufficent!!!