Datagridview First Row Skip

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshwag
    New Member
    • Aug 2010
    • 71

    Datagridview First Row Skip

    software use: c#, VS-2005

    I am trying to calculation on datagridview as below

    Code:
    private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
       double dd = 1.768901;
       dd = Convert.ToDouble(dd.ToString("f2"));
       DataGridViewCellStyle fixedstyle = 
       new DataGridViewCellStyle();
       fixedstyle.Format = "f2";
       dataGridView1.Columns[1].DefaultCellStyle =fixedstyle;
    
       try
       {
        decimal sum = 0.00m;
    
        for (int i = 0; i < dataGridView1.Rows.Count - 1;i++)
        if (dataGridView1[1, i].Value != DBNull.Value)
        {
        sum=sum Convert.ToDecimal(dataGridView1[1,i].Value);
        label12.Text = sum.ToString("f2");
         }
    
       }
    
        catch (Exception ex)
        {
           MessageBox.Show(ex.Message);
        }
       }
    The Problem is Total count from 2nd row not from begining rows.
    e.g. :-

    1st rows column value=12
    2nd rows column value=12
    3rd rows column value=12

    The total shows 24 where actual totals is 36.

    I think the datagridview skip first rows. How can i solve it?.

    Note:- I set ColumnHeaderVis ible property set to false.
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    why u have i < dataGridView1.R ows.Count - 1.either make it i <= dataGridView1.R ows.Count - 1 or make it i < dataGridView1.R ows.Count

    Comment

    • maheshwag
      New Member
      • Aug 2010
      • 71

      #3
      1. With i<= dataGridView1.R ows.Count-1 result is 0.00
      e.g. if 1st row column value i enter 12.00 the total count result is 0.00

      2. With i<dataGridView1 .Rows.Count Result is Error like Index was out of range. Index Must be non negative and less than size of collection parameter name:index

      Comment

      • maheshwag
        New Member
        • Aug 2010
        • 71

        #4
        I wants to Share a knowledge with solution of above problem

        The solution is:

        Code:
            private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
                {
                    decimal sum = 0.00m;
        
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (dataGridView1[1, i].Value != DBNull.Value)
                        {
                            sum = sum + Convert.ToDecimal(dataGridView1[1, i].Value);
                            textBox1.Text = sum.ToString("f2");
        
                        }
        
                    }
                }
        Just change the event to dataGridView_Ce llValidated.

        Comment

        Working...