Results 1 to 4 of 4

Thread: [RESOLVED] How to selected value of a dropbox column of a DataGridView programmatically?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Resolved [RESOLVED] How to selected value of a dropbox column of a DataGridView programmatically?

    I'm a C# user. I did some search in Internet but I couldn't find any proper solution for my problem.
    I have a DataGridView which adds rows and fill their value by pressing a button. The values' input is achieved from some Textboxes. My last Column is a Dropbox which has 3 defined items. I want to select first Item as a default value when the button is pressed. User can change it manually later.
    How can I do this programmatically?

    I tried this code for the button (some part of the code):
    Code:
    Int32 LastRow = dataGridView1.Rows.Count - 1;
    dataGridView1.Rows[LastRow].Cells[9].Value = new DataGridViewComboBoxCell {  };
    Last edited by fa2020; Nov 13th, 2020 at 05:33 AM.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to selected value of a dropbox column of a DataGridView programmatically?

    why are you setting the value to a new DataGridViewComboBoxCell? Did you do that for the other values? My guess is no, you did not. So why should it be any different? Set the value to the value you want it to be. If it's in the combobox it'll be selected.
    That said, is your DGV bound to anything? If so, that's what you should be adding to, not the grid. And setting the values in that object. The grid should simply be a reflction of the data, not the datastore directly.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Re: How to selected value of a dropbox column of a DataGridView programmatically?

    No, DGV doesn't bounty to external data. I store information directly. The point is that I don't put information directly to the DGV, rather I use text boxes an input from for filling DGV. A button collects all text boxes information and put them into DGV. When I run my app, DGV has no rows at first. It is the add button which fills the datagridview. Finally I solved my problem using this code:
    Code:
    dataGridView1.Rows[LastRow].Cells[9].Value = new DataGridViewComboBoxCell [0];

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: [RESOLVED] How to selected value of a dropbox column of a DataGridView programmat

    Although you have a solution, a better way is shown in the following code sample which takes more code yet is a better solution and only touches the DataGridView with DefaultValuesNeeded event.

    You can use Git to download the project as the entire repo is large (or simply view online)

    Create a .bat file, place it in a folder and run it.

    Code:
    mkdir code
    cd code
    git init
    git remote add -f origin https://github.com/karenpayneoregon/visual-basic-getting-started
    git sparse-checkout init --cone 
    git sparse-checkout add DataGridViewComboBoxRaw
    git pull origin master
    
    del .gitattributes
    del .gitignore
    del *.md
    del *.sln
    Note the same code done in VB.NET will work in C# (I posted VB solution as this is a VB forum, C# should be posted in the C# forum)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width