Go Premium for a chance to win a PS4. Enter to Win


Update values in datagrid

Posted on 2004-03-28
Medium Priority
Last Modified: 2012-06-22
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.

Question by:lakshmik
  • 4
  • 3

Expert Comment

ID: 10701551
>> 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

        '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

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

Expert Comment

ID: 10701597

'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 .

Author Comment

ID: 10701646
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??


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 10703018

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

 - Rajan

Author Comment

ID: 10703047
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.


Accepted Solution

pamboo earned 1440 total points
ID: 10705404
Sorry , I need more clarification

'Have u made datagrid allowsorting property to false to make column header unaccessable by any user like frozen header .

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  ?



Author Comment

ID: 10712659
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)
                                    dView.Sort = "id Asc," + colName + " desc";      
                                    dView.Sort = "id Asc," + colName + " asc";      
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Loops Section Overview

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question