Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to refresh or update DataGrid Cell Value ...

How to refresh or update DataGrid Cell Value ...

Scheduled Pinned Locked Moved C#
comhelptutorialannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nassimnastaran
    wrote on last edited by
    #1

    Hi all ! I want to save the DataGrid Cell Value into an excel file :

    ...
    try
    {
    mySheetInputData = (Microsoft.Office.Interop.Excel.Worksheet)ExWorkBookInputData.Sheets["Sheet1"];

    mySheetInputData = (Microsoft.Office.Interop.Excel.Worksheet)ExWorkBookInputData.ActiveSheet;

    mySheetInputData.Name = "mySheet";
    ...
    for (int i = 0; i < RowCount; i++)
    {
    for (int j = 0; j < ColumnCount; j++)
    {
    mySheet.Cells[i + 2, j + 2] = dgv.Rows[i].Cells[j].Value;
    if (dgv.Rows[i].Cells[j].Value == null || dgvRows[i].Cells[j].Value.Equals(""))
    mySheet.Cells[i + 2, j + 2] = 0.0;
    }
    }

    dgv.update();

    ....

    when I save this to an excel file , last cell i fill , show the value of "0.0" , but when I resize or minimize the application or change the cursor in the next cell that fill before , there is no problem to show all value i put to DataGrid Cell. thanks a lot !

    OriginalGriffO A 2 Replies Last reply
    0
    • N nassimnastaran

      Hi all ! I want to save the DataGrid Cell Value into an excel file :

      ...
      try
      {
      mySheetInputData = (Microsoft.Office.Interop.Excel.Worksheet)ExWorkBookInputData.Sheets["Sheet1"];

      mySheetInputData = (Microsoft.Office.Interop.Excel.Worksheet)ExWorkBookInputData.ActiveSheet;

      mySheetInputData.Name = "mySheet";
      ...
      for (int i = 0; i < RowCount; i++)
      {
      for (int j = 0; j < ColumnCount; j++)
      {
      mySheet.Cells[i + 2, j + 2] = dgv.Rows[i].Cells[j].Value;
      if (dgv.Rows[i].Cells[j].Value == null || dgvRows[i].Cells[j].Value.Equals(""))
      mySheet.Cells[i + 2, j + 2] = 0.0;
      }
      }

      dgv.update();

      ....

      when I save this to an excel file , last cell i fill , show the value of "0.0" , but when I resize or minimize the application or change the cursor in the next cell that fill before , there is no problem to show all value i put to DataGrid Cell. thanks a lot !

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind. Edit your question and provide better information - perhaps a simple example of what you have before, what you expect, and what you get would help.

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • N nassimnastaran

        Hi all ! I want to save the DataGrid Cell Value into an excel file :

        ...
        try
        {
        mySheetInputData = (Microsoft.Office.Interop.Excel.Worksheet)ExWorkBookInputData.Sheets["Sheet1"];

        mySheetInputData = (Microsoft.Office.Interop.Excel.Worksheet)ExWorkBookInputData.ActiveSheet;

        mySheetInputData.Name = "mySheet";
        ...
        for (int i = 0; i < RowCount; i++)
        {
        for (int j = 0; j < ColumnCount; j++)
        {
        mySheet.Cells[i + 2, j + 2] = dgv.Rows[i].Cells[j].Value;
        if (dgv.Rows[i].Cells[j].Value == null || dgvRows[i].Cells[j].Value.Equals(""))
        mySheet.Cells[i + 2, j + 2] = 0.0;
        }
        }

        dgv.update();

        ....

        when I save this to an excel file , last cell i fill , show the value of "0.0" , but when I resize or minimize the application or change the cursor in the next cell that fill before , there is no problem to show all value i put to DataGrid Cell. thanks a lot !

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        This is happening because a cell is still in edit mode when it's Value property is read. The new value does not become available until it has been committed and edit mode ended. As you found out this can be done by moving the focus to another cell or control, but it can also be done programmatically by calling the DataGridView's EndEdit method. Inserting the following line before you start reading values from the grid should fix the problem.

        dgv.EndEdit();

        Alan.

        N 1 Reply Last reply
        0
        • A Alan N

          This is happening because a cell is still in edit mode when it's Value property is read. The new value does not become available until it has been committed and edit mode ended. As you found out this can be done by moving the focus to another cell or control, but it can also be done programmatically by calling the DataGridView's EndEdit method. Inserting the following line before you start reading values from the grid should fix the problem.

          dgv.EndEdit();

          Alan.

          N Offline
          N Offline
          nassimnastaran
          wrote on last edited by
          #4

          thanks , it solve the problem ! ;)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups