PRMiller2
Technical User
- Jul 30, 2010
- 123
I'm very new to C#, so hoping someone can help me out. I've been banging my head against the wall on this one. I'm receiving the following error: "Index was out of range. Must be non-negative and less than the size of the collection."
In Main.cs, when a user has a customer selected in a combo box, they can select a command that will open Clients.cs. In Clients.cs, a DataGridView is populated with all Customer records. The program will select the row representing the customer chosen in Main.cs. Here's the code from Main.cs:
Here's the Load event in Clients.cs:
This works for all records except the last one in the Customer table. When that one is selected, I receive the error listed above. If the database table contains three tables, how can I make the index less than the size of the collection? In my mind, that means that the user will never be able to select the last record in a database.
Of course, that's a foolish interpretation on my part, but it does leave me scratching my head. Any recommendations?
Thanks,
Paul
In Main.cs, when a user has a customer selected in a combo box, they can select a command that will open Clients.cs. In Clients.cs, a DataGridView is populated with all Customer records. The program will select the row representing the customer chosen in Main.cs. Here's the code from Main.cs:
Code:
private void editClient()
{
Clients clientForm = new Clients();
if (clientComboBox.SelectedValue != null)
{
// int clientVal = (int)clientComboBox.SelectedValue;
int clientVal = Convert.ToInt32(clientComboBox.SelectedValue);
clientForm.ClientID = clientVal;
}
else
{
clientForm.ClientID = 0;
}
DialogResult result = clientForm.ShowDialog();
clientForm = null;
}
Here's the Load event in Clients.cs:
Code:
private void Clients_Load(object sender, EventArgs e)
{
this.customerTableAdapter.Fill(this.cs_dbDataSet.Customer);
if (clientID == 0)
{
addsaveButton.Text = "Add";
}
else
{
addsaveButton.Text = "Save";
int rowCount = clientsDataGridView.Rows.Count;
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < rowCount; i++)
{
row = clientsDataGridView.Rows[i];
if (row.Cells["lngCustomerID_pk"].Value.Equals(clientID))
{
clientsDataGridView.FirstDisplayedScrollingRowIndex = i;
clientsDataGridView.CurrentCell = row.Cells[clientID];
row.Selected = true;
clientabbbrevTextBox.Text = row.Cells["clientabbrevDGVTextBoxColumn"].Value.ToString();
clientnameTextBox.Text = row.Cells["clientnameDGVTextBoxColumn"].Value.ToString();
break;
}
row = null;
}
}
}
This works for all records except the last one in the Customer table. When that one is selected, I receive the error listed above. If the database table contains three tables, how can I make the index less than the size of the collection? In my mind, that means that the user will never be able to select the last record in a database.
Of course, that's a foolish interpretation on my part, but it does leave me scratching my head. Any recommendations?
Thanks,
Paul