How to refresh or update DataGrid Cell Value ...
-
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 !
-
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 !
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
-
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 !
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'sEndEdit
method. Inserting the following line before you start reading values from the grid should fix the problem.dgv.EndEdit();
Alan.
-
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'sEndEdit
method. Inserting the following line before you start reading values from the grid should fix the problem.dgv.EndEdit();
Alan.
thanks , it solve the problem ! ;)