DataGridView Combobox Column

I Have a simple datagridview bound to a database. One of the columns is a comboxcolumn.

When i try to drop down the combobox in any row i get the following error.

"Getting the Size property of a cell in a shared row is not allowed."

Any idea what causes this error  Any solution yet

I am using Beta 1 (Feb 2005 CTP).


Answer this question

DataGridView Combobox Column

  • davidt1234

    Awesome! I'll try the fix.

    I got another one for ya.

    I have a datagridview with 2 columns. This datagridview is bound to a datatable.

    The first columns DataGridViewCellStyle.Format property is set to C2 (Currency).
    The first columns DataGridViewCellStyle.Format property is set to P2 (Percentage).

    When I edit the values in the datagridview for the currency column it works and the changes are made to the datatable normally.

    When I edit the values for the percentage colums I get the following error.

    "Input string was not in the correct format."

    It seems there is a problem converting the cells string to a decimal value for storage in the datatable.

    I have tried to handle the DataGridView.CellParsing event and removing the "%" from the string but that does not work.

    The only work around I have found for this bug is to do the following.

    When the user clicks edit I remove the format from the column.
    MyGrid.Columns["DiscountRate"].DefaultCellStyle.Format = "";

    When the user saves the information I put the format back.
    MyGrid.Columns["DiscountRate"].DefaultCellStyle.Format = "P2";

    But this sucks. Any ideas on this one

  • jfarias

    Thanks Kapil. This bug maps to the following bug (I think opened by you also): http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx feedbackid=52ed3728-8408-4e53-9cb5-2e8b13283fd6

    The good new: This is fixed in post Beta2 builds. The bad news: We aren't able to get this fix in the Beta2 build. The workaround is easy. You need to handle the CellMouseDown and get the cell's reference:

    Dim c as DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)

    This will keep the exception from occurring.

    Thanks,
    Mark
    .NET Client Program Manager
    Microsoft
    This post is provided "as-is"


  • Tom Norman

    sure.

    where can i e mail the project to

    thanks,
    kapil

  • Max MUCCI

    Use the following address (remove the "online." part though): Markri@online.microsoft.com
  • tom_gee

    The Percentage format style (P2) unfortunately doesn't round-trip, so you are correct that handling CellParsing is the way to go. I'm curious why that still didn't work. Can you post your code for the CellParsing

    -mark
    .NET Client Program Manager 
    Microsoft 
    This post is provided "as-is" 

  • vpsivam

    Awesome!

    You're 2 for 2 Mark.

    Thanks.

  • Green Beret

    Can you send me your project  I have not seen this error on Beta1 builds.

    thanks,
    -mark 
    .NET Client Program Manager 
    Microsoft 
    This post is provided "as-is" 

  • Ma?o

    Mark, thank you VERY VERY MUCH for workaround on the CellMouseDown bug!!! It works! Me.happy=True :)
  • BrainWasher

    When databinding, the grid asks the datasource for the column's type. In this case, the UnitsInStock is of type Int16. When you perform parsing you have to parse the value to match the columns type. The column has the ValueType property that you can use to determine what the type is. 

    So, when parsing the Percentage format (P2) you need to end up with an Int16 type. In addition, when you handle this event and set the value you need to set the ParsingApplied = true. Here is what my CellParsing event looks like:
     
        Private Sub DataGridView1_CellParsing(...) Handles DataGridView1.CellParsing
            If DataGridView1.Columns(e.ColumnIndex).Name <> "UnitsInStock" Then Return

            e.Value = Int16.Parse(e.Value.ToString().Replace("%", "").Trim())
            e.ParsingApplied = True
        End Sub

    Hope this helps!
    -mark
    .NET Client Program Manager 
    Microsoft 
    This post is provided "as-is"

  • Perky

    Here is the code.

    void grdTETypes_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
    {
    if (e.DesiredType != typeof(decimal)) 
                 return;
    e.Value = e.Value.ToString().Replace("%", "").Trim();
    }

  • DataGridView Combobox Column