• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1774
  • Last Modified:

Update values in datagrid

Hi,
I'm working on Windows Forms Datagrid. I've a datagrid with a Footer row which shows me calculations of the fields I retrieve from database. I want this footer row to be updated whenever I change a value in one of the cells. Can anyone tell me which event i need to override to make this happen. I tried cellChanged() event, but this gets fired even when focus goes into a cell. I need an event to be called only when I "change" something in a cell and swich focus to next cell. Or is there anyway I can call the "Edit" and "Update" functionalities of each row as we do in Web Forms Datagrid (with update and edit button for each row).

Secondly I would like to know how to attach an image icon to the Header text of Column Headers in a WinForm Datagrid.

Thanks
Lakshmi
0
lakshmik
Asked:
lakshmik
  • 4
  • 3
1 Solution
 
pambooCommented:
>> I need an event to be called only when I "change" something in a cell and swich focus to next cell.

Private CellValueChanged as Boolean =False ' by default or at first there is no change in any cell  , so set to false
Private _CurrentDGCellLocation as DataGridCell
Private WithEvents _dtDatagridDataSourceTable As DataTable

'Get the DataTable assigned to the DataGrid to a DataTable object declared with events
dtDatagridDataSourceTable  = Me.DataGrid1.DataSource

'Now on the column change of that DataTable set some flag to check if it changed

Private Sub _dtDatagridDataSourceTable _ColumnChanged(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles _dtDatagridDataSourceTable.ColumnChanged

  CellValueChanged  =True

  'Also to get the current cell below code will help u  which can used on when it is changed to other cell to get the
  ' last cell location which is been updated or changed.

        Dim dt As DataTable = CType(sender, DataTable)
        Dim r As DataRow
        Dim c As DataColumn
        Dim row, col As Integer
        row = 0
        col = 0

        'If it is a new row going to be added
        _CurrentDGCellLocation.RowNumber = dt.Rows.Count

        'Find the row location
        For Each r In dt.Rows
            If r Is e.Row Then
                _CurrentDGCellLocation.RowNumber = row
                Exit For
            End If
            row += 1
        Next

        'Find the col location
        For Each c In dt.Columns
            If c Is e.Column Then
                _CurrentDGCellLocation.ColumnNumber = col
                Exit For
            End If
            col += 1
        Next

End Sub

'Now on the cell changed event of DataGrid
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As EventArgs) handles DataGrid1.CurrentCellChanged
 
 if CellValueChanged  =true then
  'update ur footer value
  'if u want the last cell which is changed , u can get from the variable "_CurrentDGCellLocation"

 end if

  'set CellValueChanged  to false to initialise for the next event
  CellValueChanged  =false

End Sub

So , the key point is u have to use both the events DataTable coulmnchange event + DataGrid cell change .

Hope this helps u .

- Rajan
0
 
pambooCommented:
>>correction

'Get the DataTable assigned to the DataGrid to a DataTable object declared with events
_dtDatagridDataSourceTable  = Me.DataGrid1.DataSource


--------------------------------------------------------------------------------------------------------------------------------------

U no need to the get the DataTable from the DataGrid . U will be having it in some DataSet or DataTable Variable already .
(which u would have assigned to the Datagrid)

If u have it as datatable already just include WithEvents Modifier in the declaration of the datatable and make its scope across the form class (as a datamember of the form class and not local to a method/function).

If u have it has only in DataSet , get the DataTable assigned to the Datagrid to a DataTable Vaiable declared as above mentioned and assign the table that u have assigned to Datagrid . ( eg.,_dtDatagridDataSourceTable  = DataSet1.Tables(0))
(or) even u can get it from the datagrid directly  (_dtDatagridDataSourceTable  = Me.DataGrid1.DataSource)


-------------------------------------------------------------------------------------------------------------------------------------

I feel I am explaining it more . Sorry if so ...

Tell if this solution helped .

- Rajan .
0
 
lakshmikAuthor Commented:
Hi Rajan,
Thanks a lot for ur help. I'm trying it out now in c#... wil get back once I get it through.
Any idea on attachin ColumnHeader text with an icon for custom sort??

Thanks
Lakshmi
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
pambooCommented:

 u mean to change the
 (^) ascending .
 (V) decending
 icons indicating in the sort column header of a datagrid .

 - Rajan
0
 
lakshmikAuthor Commented:
hi Rajan,
I'm implementing custom sorting so that my footer does not get sorted with data everytime. This makes the default icons vanish. So i want to know if there is any way where i can show the user the sorting functionality.

Thanks
Lakshmi
0
 
pambooCommented:
Sorry , I need more clarification

'Have u made datagrid allowsorting property to false to make column header unaccessable by any user like frozen header .
Me.DataGrid1.AllowSorting=False

Then u might be given some button or menu which when user click, it will sort by
the mentioned column of button or menu control.

Now , when after that sorting is made , u need to show in the header of the grid
that this column is sorted by indicating it with normal sort icon in that custom sorted column.

Is this u want ? please let me know . If so then I will check it here and try.

Okay additionaly , to do sorting of a column programatically u can use this below command,

_dtDatagridDataSourceTable.DefaultView.Sort = "[desc] sortcolumnname" ' sort column by decending order
_dtDatagridDataSourceTable.DefaultView.Sort = "sortcolumnname"  'sorting in the other order of the prev

Is the first solution of updated datagrid cell changed helped  ?

Regards,
Rajan

0
 
lakshmikAuthor Commented:
hi,
Thank you for the datagrid cell changed event. It works:).
Abt the column header, I'm first disabling the default feature of sorting for datagrid.
Then i add a bool column(call it "id") with the footer's value alone as false. I then programmatically enable
sorting by handling the mousedown event of datagrid with the following code.
DataGrid.HitTestInfo hti;
                  hti =this.HitTest(e.X, e.Y);
                  string colName;
                  if(hti.Type == DataGrid.HitTestType.ColumnHeader)  
                  {  
                        int colNum=hti.Column;
                        if(colNum != -1)
                        {
                              colName=dTable.Columns[colNum].ColumnName;
                              if(boolAsc==true)
                              {
                                    dView.Sort = "id Asc," + colName + " desc";      
                                    boolAsc=false;
                              }
                              else
                              {
                                    dView.Sort = "id Asc," + colName + " asc";      
                                    boolAsc=true;
                              }
                        }
                  }
So the sorting is done by cliking the column header but there is no indication if its ascending or descending.
So I want to add a sort icon beside the name of the colm so that i can show if its sorted in ascending or descending.
Thanks
Lakshmi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now