0
votes

I have a windows forms application with a DataGridView (DGV) on part of it. I used the Visual Studio UI to add columns to the DGV and then programmatically added rows with some default values. My problem is once the rows are added, any cell selected automatically empties whatever value was in it - even read-only cells!

I am only handling the "CellValueChanged" event for the DGV, and all that does is validate the rows to make sure at least one valid combination of values exists and then enables a button.

To add to the confusion, the selected-cell-clearing issue does not always happen. After rows are added, until you make a change or perform an action that reloads the DGV with rows, all the cells will clear. After that either some or none of the cells will clear on their own.

Has anyone experienced this or know of a way to solve the problem? All I got from google was "how to make cells clear" and things like that.

Thanks,

  • Matt

* UPDATE *

Code for adding new rows to the DGV:

        metaMapGrd.Rows.Clear();
        ((DataGridViewComboBoxColumn)metaMapGrd.Columns[1]).DataSource = _excel.ListHeaders(worksheetListCmb.SelectedItem.ToString());
        foreach (Field f in _sharePoint.CurrentFieldList)
        {
            // skip the key field matching portion.  These should be for comparison only.
            if (f.Title == spListKeyCmb.SelectedItem.ToString())
            {
                continue;
            }

            DataGridViewRow row = new DataGridViewRow();

            metaMapGrd.Rows.Add(row);

            DataGridViewTextBoxCell spcell = new DataGridViewTextBoxCell();
            spcell.Value = f.Title;
            spcell.ToolTipText = f.TypeAsString;
            row.Cells["spListColumn"] = spcell;

            DataGridViewCheckBoxCell fcell = new DataGridViewCheckBoxCell();
            fcell.Value = true;
            row.Cells["useThisColumn"] = fcell;
        }
        metaMapGrd.CurrentCell = null;
1
Are you also data-binding the datagridview ?Luc Morin
no, just adding rows. does it need to also be databound?Matt
Depends on your use case... so you basically want to handle the content of the grid in your code ?Luc Morin
Also, could you provide some sample code to explain how you set the values in the cells ?Luc Morin
I want to add a tooltip for each row of a particular column. Otherwise I have no objection to binding the darned thing to a ListMatt

1 Answers

0
votes

As per our chat, I suggest not trying to manually handle the DatagridView, but rather use an underlying List of DataObjects, and then databind the DGV to that list.

Use a hidden column for the information about the SharePoint ColumnType.

Hoipe this helps.