Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3216960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:21:21+00:00 2026-05-17T15:21:21+00:00

The dataGridview first row column name amount is return no value as there is

  • 0

The dataGridview first row column name amount is return no value as there is value inserted.

For our Peers’ Convenient I draw the table as it’s help to answer it better.

my table on dataGridview is as below:

   private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name",typeof(string));
        dt.Columns.Add("amount",typeof(decimal));
        dataGridView1.DataSource = dt;
        dataGridView1.Columns[1].DefaultCellStyle.Format = "f2";
    }

Now I prepared the table and my main code is below:

  private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        decimal sum = 0.00m;

        for (int i = 0; i < dataGridView1.Rows.Count-1; i++) 
        {
                sum = sum + Convert.ToDecimal(dataGridView1[1, i].Value);
                textBox1.Text = sum.ToString("f2");
        }
    }

The above code throw Error like “Object Cannot cast from DBNull to other types”. Even though there is value.

Now Look at to another Code:

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        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);
                textBox1.Text = sum.ToString("f2");

            }        

        }
    }

Now this code not throwing any Error but still the problem with it. The problem is when i enter the value on column of amount is look like it skip the value of first row’s column name amount.

e.g….

I enter the value on column name amount is below:

1st Row column amount values is = 12
2nd Row column amount values is = 12
3rd Row column amount values is = 12

Now the total showing in textbox1 is = 24, where as actual total is = 36.

There is going something wrong with coding it shows that dataGridview’s first row’s column value is not accepted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:21:21+00:00Added an answer on May 17, 2026 at 3:21 pm

    Best Solution is as below:

      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 dataGridView Event as above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm simply trying to delete a row from a DataGridView. Basically, the first column
I need to search a known column in my DataGridView and return the row
My DataGridView displays a 0 column with a delta in the first row and
I have DataGridView with two columns. The first column is TextBoxCol(DataGridViewTextBoxColumn) and the Second
hi i have a datagridview whose first column (index = 0) is a checkbox
I am trying to make the first row of the DataGridView to be bigger.
I have a datagridview in which the first column is a Foreign Key and
I currently have a 2-column wide DataGridView, the first column being a DataGridViewTextBoxColumn and
I've got a single-row, 40 column-long DataGridView that i need to print. Since i
how can i select full and first row of datagridview in vb.net on form_Load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.