The cell values in DataGridView


  • How to get the value of cells in the adding row of DataGridView ? In ASP.Net I can get the value from e.NewValues and e.OldValue, but how about VB DataGridView ? thank's...

    Friday, November 30, 2007 2:03 PM

All replies

  • Hi Sixon,


    Could you clarify your scenario about how to populate the datagridview control? I assume that you use the datatable object as the datasource of the datagridview control.


    Because the DataGridViewCell Members haven't the property to save the original value, I recommend that you use the datatable as the datasource of the datagridview control. ADO.NET manages rows in tables using row states and versions. A row state indicates the status of a row; row versions maintain the values stored in a row as it is modified, including current, original, and default values. You can try to use the Row States and Row Versions to get the values which corresponds to the different version. It should satisfy your requirement. For more information about the Row States and Row Versions 
    , visit:
    http://msdn2.microsoft.com/en-us/library/ww3k31w0(VS.80).aspx. If you have any further questions, please tell me.


    Best regards,


    Tuesday, December 04, 2007 3:24 AM
  • Sixon,


    as far as i know the new row's cell's value is in e.FormattedValue

    the existing row's values is in DataGridView.Rows(e.RowIndex).Cell(e.ColumnIndex).Value



    Tuesday, December 04, 2007 4:55 AM
  • i believe that the new row should not be any different than the others

    it is still a row


    you should be able to access it by     currentrow


    Me.DataGridView.CurrentRow.Cells( index  or "column name").Value()


    and you can check if the row is a new row also


    If Me.CustomerDataGridView.CurrentRow.IsNewRow Then

    'do something

    End If

    Tuesday, December 04, 2007 2:41 PM
  • Actually, my table of Sql is like below :


    Code     Description     Level    Type

    001       Cash              1          AS

    002       Bank              1          AS

    003       Expenses       2          EX



    (Note : All columns are not allow Null value.)


    then I create two adapter, adapter1 is "Select Code, Description, Level, Type Where Level=1.

    Adapter2 is "Select Code, Description, Level, Type Where Level=2


    Then I create two DataGridView : GridView1 bind to BindingSource1 which get data from adapter1, GridView2 bind to BindingSource2 which get data from Adapter2.


    But My DataGridView is like below :


    GridView1 :

      Code     Description    

      001       Cash            

      002       Bank          

    *                                     <---- New row of DataGridView1


    GridView2 :

      Code     Description    

      003       Expenses            

    *                                     <---- New row of DataGridView2


    The "Level" & "Type" column not appears in the GridView (I removed it after draging the datasource from the DataSource Explorer to the form).

    When I type the Code "004" and Decription "Capital", in the new row of GridView2 then click the save icon on BindingNavigater, I want it save or update the new row. So I put the codes in the DataGridView2_RowUpdating events like below :


    Me.DataSet.Chart.LevelColumn.DefaultValue = 2
    Me.DataSet.Chart.TypeColumn.DefaultValue = "AS"

    And in the BindingNavigaterSaveItem_Click events I put the codes below :




    I think that with these codes, the data will save when I click the save icon, because I have put the values to the hidden columns (Level & Type) before it updates. But it still appear error "Level Column not allow Null value" It's like the codes in the GirdView RowUpdating event is not efect. But when I type once again , the row is update and works properly. For the second new row, it works properly too. So, only for the first time input the row, it is error. I feel strange. How to solve this problem ? (Or because of viewing two GridView at the same time will crash ?)  thanks....... 





    Wednesday, December 05, 2007 12:54 PM
  • I have seen other threads about similar issues

    It seems to be a problem with the very first row

    I had a similar situation with the auto generated number - primary key field

    the first row would generate a 1 but it threw an error saying that 1 was already used

    Obviously it had not been used because that was the only row that had ever been created.


    There is something wrong with the very first row being handled by the bindingsource

    i have yet to see the same happen with any other row.


    In the other thread i saw i believe what was being suggested was to tell the user on the first one that they needed to save twice.  Seems like you shouldn't have to do such a thing.   The other thing might be to have a check and on the first one catch it with try catch and in the catch save again.  Just seems like you shouldn't have to do these types of things but apparently it's a problem.


    I have inserting records records directly into my database tables with code and have yet to have any problems like this.  Later on when i need to insert and update bulk records from a datagridview i plan on using a row state column that i will handle myself and run a loop to insert or update the records myself with sql statements.  That way i know that i don't have to rely on the bindingsource.  I have already found a few small ones. It makes me wonder what other issues there are with it. 

    Wednesday, December 05, 2007 2:13 PM
  • I'm using DataGridView_CellEndEdit event rather than on row updating event, because when I the input  wrong text, all input for the  row was cleared (it's not new row) ,...for example,  My data is like below :


    Column1   Column2    Column3    Column4

      001           aaa           bbb            ccc

      002           ddd           eee            fff   -----> edit Column1 to xxx, Column2 yyy, Column3 zzz

      003           ggg           hhh            iii

    When I'm editing in row 2, Column1 is xxx then type Tab and go to Column2 and change it to yyy and then Column3 is zzz and Column4 I change it to 123, if error, the text in Column1 return to ddd, Column2 eee and Column3 fff.

    So I must input from column1 again. So I put the updating in CellEndEdit event for updating everytime I change the cell value, so that if I input wrong value in Column4, the value of column1 is still xxx, etc.


    The codes is like below :



    if Me.DataGridView1.CurrentRow.IsNewRow = False Then








      Catch Exception

          MessageBox.Show("Type wrongly, or must input all columns")


      End Try


    End If


    The Codes runs and work properly for the rows that's not new row. Now My problem is how to handle the new row? When I input Column1 in new row (Column2, 3, 4 still blank/null), and then go to previous row, the error message was appeared, but it's error handle message, not my messagebox......


    Friday, December 14, 2007 6:48 PM
  • I handle all my record editing with a textbox form.  I let the user select a record from the datagridview and then they choose from a menu option to edit it.  Then it brings up the record in the edit form with the textboxes.  I handle the text input with the key press events and do checks before i update the database.


    I don't know how the errors are handled when you are editing a record in the datagridview so i don't know how to work around them.


    It might be better if you were to start a new thread just for this problem

    Friday, December 14, 2007 7:32 PM
  • hi guys,

    am also working on a similar project.
    but here i have three textboxes and a datagridview that has three columns.
    i used the DataGridView.Rows(e.RowIndex).Cell(e.ColumnIndex).Value to assign the first textbox, now how do i assign the value of the second cell to the second textbox and same to the third.

    Friday, January 02, 2009 2:37 PM
  • Public  MyRant

    Everyone who keeps insisting on data binding to controls, needs to stop.  You add an uncontrollable element to your code, causing you grief.  Learn ADO.NET, which is the foundation for these data controls in your toolbox.  Seriously!  Programming (to me anyways) is all about 2 steps forward, and one step back.  Take your step back, and learn ADO.NET. 

    When developing software, and you encounter a major roadblock, you need to stop for a moment, and think about possible alternatives.  You wont know the alternatives available unless you have a grasp of the foundation of the environment your working in, e.g. vb.net, and ado.net.

    Don't get stuck, and just post a question in a forum and wait.  Search the internet, most likely there are many others that have had a similar problem, or question, and a resolution has already been made available.  In your searching, you will come across code samples, suggestions, and answers to questions you haven't even thought of yet.  You may not know what this means at that moment, but down the road when you get stuck again, you will remember something you saw while searching for something else, and it will give you an idea of what you need to search for now.
    Just my opinion!

    Compensating what I dont know yet, with what I do know now.
    Sunday, January 04, 2009 3:53 AM
  • I'm starting to agree.....binding seems more trouble than its worth as does the DataGridView with all it's unexpected behaviors.

    Working with individual controls that react logically seems to be a much more efficient approach.

    If anyone can point to a source that clearly and simply explains the DataGridView, it would be much appreciated.

    Saturday, September 03, 2011 6:22 PM