Update values in datagrid

Posted on 2004-03-28
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??

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


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 360 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 92
move one pixel 4 69
Tembedded WB animatid gifs not animated on some pcs 2 80
C# DataGridView_RowsAdded event not firing 1 76
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

831 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