locked
datagridview cell editing RRS feed

  • Question

  • I am programatically trying to edit some cells. I change the value using DG[index1,index2]=value. Then I execute EndEdit on the DG. But I notice that the currentcell is not the cell I edited. It seems to be causing problems. What is the best way to make sure changes are committed to the DataGridView once I make changes programatically.

     

    Thanks

    Nik

    Tuesday, August 1, 2006 2:54 PM

Answers

  • When you edit a cell using DataGridView(cellIndex, rowIndex) = value and then execute DataGridView.EndEdit(), it does not actually change CurrentCell property of a DataGridView. To change it programmatically use DataGridView.CurrentCell = DataGridView(cellIndex, rowIndex).
    Tuesday, August 1, 2006 3:25 PM

All replies

  • When you edit a cell using DataGridView(cellIndex, rowIndex) = value and then execute DataGridView.EndEdit(), it does not actually change CurrentCell property of a DataGridView. To change it programmatically use DataGridView.CurrentCell = DataGridView(cellIndex, rowIndex).
    Tuesday, August 1, 2006 3:25 PM
  • I guess I will start by telling what I am trying to do.

    ===================

    I have to edit a cell in a datagridview(in each row of the view). update these values and display the new updated values.

    I could change the underlying datatable values but I don't want to do that if there is a way around it.

    ===================

    back to my question:

    Is there a way to end the edit at the row level? The reason I am asking is because after I do an EndEdit() (which ends the edit on the current cell) but what I edited in the first place is not the current cell. Therefore I am wondering I that has something to do with my values not being saved properly in the gridview..

    So should the current cell be changed each time before endedit is called to make sure the current cell is the same as that being edited??

    Thanks

    Nik

    Tuesday, August 1, 2006 4:04 PM
  •  ChitownDotNet wrote:

    So should the current cell be changed each time before endedit is called to make sure the current cell is the same as that being edited??

     

    No it shouldn't. Moreover, you don't even have to call DataGridView.EndEdit(). Just step through each row and change cells values. After that you can save all changes to your database by executing BindingSource.EndEdit() and TableAdapter.Update().

    Tuesday, August 1, 2006 5:55 PM
  •  

    If you want to programatically edit a cell, you can try by

     

    DataGrid.Rows[0].Cells[1].ReadOnly = false;

    Saturday, November 24, 2007 11:15 AM
  • (I hope you guys are still listening) I'm completely newbie and I have been trying to solve a problem that is strongly related to what you guys had discussed here .

    I have a datagridview (datagridview1) on my form and it is not linked to any database. I'm only trying to programatically enter values into the rows of the grid by values that my program generates.

    For example if the value that my program generates is x, y, z then I would like to say something like

    DataGridView1(1, 1).Value = x


    DataGridView1(2, 1).Value = y
    DataGridView1(3, 1).Value = z

    When I do this I get the error

    "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

    I thought cells in the datagridview are dealt with like text boxes but apparently they are not

    I would really appreciate it if somone can point me in the right direction


    Marino


    Tuesday, October 20, 2009 8:17 PM
  • Marino - you probably have figured this out by now.. but you're dealing with a fundamental programming mistake. The rows and cells of a datagrid are on a zero-based index. Thus, the first column of cells would be addressed this way:

    DataGridView1(0,0).Value = x
    DataGridView1(1,0).Value = y
    DataGridView1(2,0).Value = z
    Friday, December 11, 2009 3:31 PM
  • I have a DataGridView that I am programmatically changing. All of the columns are unbound / it is not backed by any type of DataSource. Depending on user actions in one column, I may "activate" and allow tabbing into successive columns, or I may keep them greyed out. There is a lot of per-cell validation that I do, sometimes in the CellLeave events or the RowLeave / RowEnter events.

    I find during my validation functions, the initial value I want to work with is accessible via the cell's EditedFormattedValue property, and after I've massaged the user input into a standardized format, and want to apply my version of the final data to the cell, then I set the cell's Value property.

    After my data validation is completed, I then call the DataGridView's RefreshEdit() method in order to properly update what the user is seeing on their screen.

    None of what I describe above requires you to be editing values via the "CurrentCell" property or whatever.. it doesn't matter how you access the cell.. you can get a reference to a cell in any fashion you choose.
    • Proposed as answer by deeshubby Friday, December 11, 2009 5:15 PM
    • Edited by deeshubby Friday, December 11, 2009 5:18 PM misspelling
    Friday, December 11, 2009 5:15 PM
  • Thanks Deeshubby,

    I know it is a late thank you but I think I stoped looking at the forums when I thought that my question failed to show up in th forum. I will check this e-mail of mine regularly from now on and I want to thank you for being such a human in extending the helping hand to someone who needed it although you don't know him.

    Thanks again,

    Marino

    Friday, March 26, 2010 4:47 PM