change type of the selected cell on datagridview when cell is clicked.

Posted on 2008-11-11
Last Modified: 2012-05-05
I am populating a datagridview with two text columns from the output of a sql stored procedure. I want the user to be able to click on any cell in the grid and see a drop down list of values populated from another database table, one per each column, which they can select from to override the cell content from the initial datasource. i.e I want to change the type of the selected cell from datagridviewtextboxcolumn to datagridviewcomboboxcolumn when the cell is clicked.  Is there any way of doing this?

Question by:AlHal2

    Author Comment

    What about setting each cell in the grid to a combobox.
    The text property of the combobox are the contents of the data table.
    At the moment each cell of the grid is like a textbox and is populated by the contents of the data table.
    LVL 18

    Expert Comment

    Yuu cannot change type of a datagridview cell, this is not possible. You would need to add comboboxcolumn (have it visible = false, copy data from textboxcell to new comboboxcell, remove textboxcell, and make comboboxcolumn visible.

    Author Comment

    Do you have any sample code for this?
    LVL 18

    Accepted Solution

    No I dont, but I dont believe it is hard, you know the best the logic of your application. I will give some guidance links

    how to create DataGridViewComboBoxColumn, insert it at desired location, and populate it

    then you need to loop through each row and set the value from textbox to ComboBox cell. Note that value from textboxcell MUST exists in a comboboxcell, otherwise you will have to add this entry manually in a comboboxcolumn list.

    example c# code

    foreach (DataGridViewRow dr in dataGridView1.Rows)
        dr.Cells[1].Value = dr.Cells[0].Value; // index 0 is the index of textbox column, and index 1 is the index of combobox column

    and in the end remove the textbox column

    dataGridView1.Rows.RemoveAt(0); // index of textbox column is 0

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now