Solved

Update values in datagrid

Posted on 2004-03-28
9
1,709 Views
Last Modified: 2012-06-22
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
Comment
Question by:lakshmik
  • 4
  • 3
9 Comments
 
LVL 3

Expert Comment

by:pamboo
Comment Utility
>> 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
 
LVL 3

Expert Comment

by:pamboo
Comment Utility
>>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
 

Author Comment

by:lakshmik
Comment Utility
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:pamboo
Comment Utility

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

 - Rajan
0
 

Author Comment

by:lakshmik
Comment Utility
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
 
LVL 3

Accepted Solution

by:
pamboo earned 360 total points
Comment Utility
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
 

Author Comment

by:lakshmik
Comment Utility
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now