Click here to Skip to main content
15,887,937 members
Home / Discussions / C#
   

C#

 
QuestionGet cell value from GridView Pin
treuveni13-Aug-10 12:45
treuveni13-Aug-10 12:45 
AnswerRe: Get cell value from GridView Pin
PIEBALDconsult13-Aug-10 17:55
mvePIEBALDconsult13-Aug-10 17:55 
GeneralRe: Get cell value from GridView Pin
treuveni13-Aug-10 21:03
treuveni13-Aug-10 21:03 
AnswerRe: Get cell value from GridView Pin
maddahi.m.p14-Aug-10 0:29
maddahi.m.p14-Aug-10 0:29 
QuestionMessage Removed Pin
13-Aug-10 11:53
Tichaona J13-Aug-10 11:53 
AnswerRe: Playing a list of files on after the other. Pin
AspDotNetDev13-Aug-10 12:22
protectorAspDotNetDev13-Aug-10 12:22 
QuestionHow to properly show text for URL link using RichTextBox to output into RTF format? Pin
jboyd11113-Aug-10 10:53
jboyd11113-Aug-10 10:53 
QuestiondataGridView1_CellContentClick Error Index Out of Range Pin
MumbleB13-Aug-10 10:27
MumbleB13-Aug-10 10:27 
Hi Guys. I have been trying to resolve an error I keep getting. I have an app with a number of forms. Most of the forms has a datagridview with edit and delete buttons. Most of them work fine but on two of the most simpler forms it just for some reason doesn't seem to work. Whenever I click on any of the column I get the following error:
Index out of range. must be non-negative and less than the size of the collection. Parameter name : index.

The same code I am using here works on other forms but not on this one.

Below is the code I am using.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int currentRow = int.Parse(e.RowIndex.ToString());
    int rowint = 0;
    dt.Clear();
    try
    {
        conn.Open();
        string taskstring = dataGridView1[0, currentRow].Value.ToString();
        rowint = int.Parse(taskstring);
        if (dataGridView1.Columns[e.ColumnIndex] == deleteButton && currentRow >= 0)
            //if(dataGridView1.Rows[e.RowIndex].Cells = 1)
        {

            string queryDeleteString = "DELETE FROM tblTasks where TaskCode = " + rowint + "";
            OleDbCommand sqlDelete = new OleDbCommand();
            sqlDelete.CommandText = queryDeleteString;
            sqlDelete.Connection = conn;
            sqlDelete.ExecuteNonQuery();
        }
        else if (dataGridView1.Columns[e.ColumnIndex] == editButton && currentRow >= 0)
        {
            string updatestring = "Select TaskCode, TaskName, TaskDescription FROM tblTasks WHERE TaskCode = " + rowint + "";
            string taskname = dataGridView1[dataGridView1.Columns["TaskName"].Index, currentRow].Value.ToString();
            string description = dataGridView1[dataGridView1.Columns["TaskDescription"].Index, currentRow].Value.ToString();
            OleDbDataAdapter da = new OleDbDataAdapter(updatestring, conn);
            OleDbCommandBuilder cmdb = new OleDbCommandBuilder(da);
            da.Update(dt);
            string update = "UPDATE tblTasks SET TaskName = '" + taskname + "', TaskDescription = '" + description + "' WHERE TaskCode = " + rowint + "";
            da.UpdateCommand = conn.CreateCommand();
            da.UpdateCommand.CommandText = update;
            da.UpdateCommand.ExecuteNonQuery();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
    finally
    {
        conn.Close();
        getData();

    }
}


Can anybody see anything here that would cause the problem? As mentioned, the same code works on other forms that have much more columns and data on it. Only two fo the forms don't seem to work with this code. Confused | :confused:
Excellence is doing ordinary things extraordinarily well.

AnswerRe: dataGridView1_CellContentClick Error Index Out of Range Pin
OriginalGriff13-Aug-10 10:39
mveOriginalGriff13-Aug-10 10:39 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
MumbleB13-Aug-10 11:02
MumbleB13-Aug-10 11:02 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
MumbleB13-Aug-10 11:18
MumbleB13-Aug-10 11:18 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
OriginalGriff13-Aug-10 22:59
mveOriginalGriff13-Aug-10 22:59 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
MumbleB14-Aug-10 3:41
MumbleB14-Aug-10 3:41 
AnswerRe: dataGridView1_CellContentClick Error Index Out of Range Pin
Luc Pattyn13-Aug-10 14:00
sitebuilderLuc Pattyn13-Aug-10 14:00 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
MumbleB13-Aug-10 21:50
MumbleB13-Aug-10 21:50 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
Luc Pattyn13-Aug-10 22:34
sitebuilderLuc Pattyn13-Aug-10 22:34 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
OriginalGriff13-Aug-10 23:03
mveOriginalGriff13-Aug-10 23:03 
AnswerRe: dataGridView1_CellContentClick Error Index Out of Range Pin
Luc Pattyn13-Aug-10 23:13
sitebuilderLuc Pattyn13-Aug-10 23:13 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
OriginalGriff13-Aug-10 23:22
mveOriginalGriff13-Aug-10 23:22 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
MumbleB14-Aug-10 4:18
MumbleB14-Aug-10 4:18 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
OriginalGriff14-Aug-10 6:06
mveOriginalGriff14-Aug-10 6:06 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
MumbleB14-Aug-10 10:00
MumbleB14-Aug-10 10:00 
GeneralRe: dataGridView1_CellContentClick Error Index Out of Range Pin
OriginalGriff14-Aug-10 23:50
mveOriginalGriff14-Aug-10 23:50 
QuestionReturn a specified item from a list box.. Pin
Tichaona J13-Aug-10 8:57
Tichaona J13-Aug-10 8:57 
AnswerRe: Return a specified item from a list box.. Pin
OriginalGriff13-Aug-10 9:06
mveOriginalGriff13-Aug-10 9:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.