Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I am to sort the bounded datagridview as I change a cell value. The problem comes when I try to edit the checkbox control in datagrid view. I have found the source of error is the code in region sorting Feature. when I remove it It works perfect. Please help me if there is a way out of this problem. It has killed a lot of my time. Please help.
C#
private void dgDrugs_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    dgDrugs.CellValueChanged -= new DataGridViewCellEventHandler(dgDrugs_CellValueChanged);
    try
    {
        if (dgDrugs.Rows.Count > 0)
        {
            #region Sorting Feature
            this.dgDrugs.Sort(drugsDataGridViewTextBoxColumn, ListSortDirection.Ascending);
            #endregion
            ////updateColors();
         //   updateStopped();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    dgDrugs.CellValueChanged += new DataGridViewCellEventHandler(dgDrugs_CellValueChanged);

}

The error message appears on following main function... error place is quoted,

C#
namespace Patient_hospital_bracelet
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            "Application.Run(new frmBracelet());" 
        }
    }
}

The error message is,
Specified argument was out of the range of valid values.
Parameter name: rowIndex


Thanks in advance
Posted
Updated 25-Jan-11 11:02am
v2
Comments
Henry Minute 25-Jan-11 17:08pm    
Do you only want the sorting to happen when the drugsDataGridViewTextBoxColumn column changes?

That's a top level error, it just means you need to add some try catches or look at the call stack to see the line with the actual error. Why do you set the event handler twice, and set it in your cell value changed method ? that means every time a cell is changed, that method will be called two more times when that event fires. I wonder if having it call a bajillion times is the root cause of your error ?
 
Share this answer
 
Comments
J imran 25-Jan-11 17:04pm    
Please see the line
<pre lang="midl">dgDrugs.CellValueChanged -= new DataGridViewCellEventHandler(dgDrugs_CellValueChanged);</pre>
I have disabled the event handler while the function is called. this avoid the recursive calles.
Henry Minute 25-Jan-11 17:05pm    
He's not setting it twice, Christian. He's unsetting it at the start then setting it at the end, I suspect to avoid it being called during the sort operation.
J imran 25-Jan-11 17:08pm    
yes henry.. you are right.. Please guide me out of it. not sure how to fix this. Please please please. Getting out of time.
J imran 25-Jan-11 17:06pm    
It seams to me that It is a top level error but I have no place left without try catch. still confusing for me where the error comes actually. But when I delete the sorting feature the error don't appear.
GenJerDan 25-Jan-11 17:23pm    
Is it just me, or is "rowIndex" not even in that block? Seems like the error is cropping up somewhere else....like in the form's initialization/creation.
If you only want the sorting to happen when the value of the drugsDataGridViewTextBoxColumn changes, alter your code something like this:

C#
private void dgDrugs_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // dgDrugs.CellValueChanged -= new DataGridViewCellEventHandler(dgDrugs_CellValueChanged);
    try
    {
        if (dgDrugs.Rows.Count > 0)
        {
            #region Sorting Feature
            if (e.ColumnIndex == this.dataGridView1.Columns["drugsDataGridViewTextBoxColumn"].Index)
            {
                 this.dataGridView1.Sort(this.dataGridView1.Columns[e.ColumnIndex], ListSortDirection.Ascending);
            }
            #endregion
            ////updateColors();
         //   updateStopped();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    // dgDrugs.CellValueChanged += new DataGridViewCellEventHandler(dgDrugs_CellValueChanged);
}


I have commented out your unsubscribing from the event and then subscribing again lines because I have just tried this and it works fine on my system.

If you get problems, my tests suggest that they are not caused by this code.

Of course, I could be wrong. :)
 
Share this answer
 
Comments
J imran 26-Jan-11 10:55am    
Thank you boss. I will try that. Thank you again for the favour

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900