开发者

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

just got a slight problem here with updating a MySql DataGridView from visual studio in C#. The code below contains the fillData(); method that populates the datagrid. The Exception error is thrown in the button code. The aim of the button is to replac开发者_Go百科e the Reviewed field from 0 to 1, meaning that the reviewed checkbox is then selected. This is probably a very basic problem so i may ask more questions, hope you can help.

public DataTable tb = new DataTable();
public MySqlDataAdapter a = new MySqlDataAdapter();

public void fillData()
{
    using (MySqlConnection c = new MySqlConnection("host="";user="";password=""; database="";"))
    {
        c.Open();                
        string strSQL = "SELECT DataID, Date, WhichMeal, HbA1C_Test, Carbohydrates, GlucoseReading, InsulinUsed, InsulinType, ReviewedBy, Reviewed From PatientData WHERE username = '" + uname.Text + "';";

        using (MySqlDataAdapter a = new MySqlDataAdapter(strSQL, c))
        {
            a.Fill(tb);
            dataGridView2.DataSource = tb;
        }


private void button5_Click(object sender, EventArgs e)
{
    using (MySqlConnection c = new MySqlConnection("host=;user=;password= ""; database=""))
    {
        try
        {
            string DataId =  dataGridView2.Rows[this.dataGridView2.SelectedRows[0].Index].Cells["DataID"].Value.ToString();
            string Update = string.Format("UPDATE PatientData SET Reviewed = 1 WHERE DataID = {0}", DataId);
            MySqlCommand command = new MySqlCommand(Update, c);

            c.Open();
            command.ExecuteNonQuery();
            c.Close();
            tb.Clear();
            fillData();
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}


You can find the problem, by checking the following things:

  • How many entries are there in this.dataGridView2.SelectedRows?
  • How many entries are there in this.dataGridView2.Rows?
  • What is the value of this.dataGridView2.SelectedRows[0].Index?

Index value is greater than the valid number of entries in one of those collecions.


I'm guessing that your problem is here:

.SelectedRows[0]

Can you verify that the .SelectedRows property has a Count of 1 or more at the time this code is run?

As a side note, the following expression:

dataGridView2.Rows[this.dataGridView2.SelectedRows[0].Index]

... appears to be unnecessarily complex. Can you replace it with this?

this.dataGridView2.SelectedRows[0]


Your problem lies in this line:

string DataId =  dataGridView2.Rows[this.dataGridView2.SelectedRows[0].Index].Cells["DataID"].Value.ToString();

You are trying to insert record where there is no record. The workaround to fix this is to try to insert some dummy data in row first then overwrite it with actual data.


I encountered this kind of problem in handling the button event inside the gridview.. and my solution to that is this:

if (e.ColumnIndex == 0 && e.RowIndex >=0)
{
  //condition here
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜