-
Nov 13th, 2020, 05:08 AM
#1
Thread Starter
Member
[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.
-
Nov 13th, 2020, 12:26 PM
#2
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
-
Nov 13th, 2020, 10:47 PM
#3
Thread Starter
Member
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];
-
Nov 15th, 2020, 08:34 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|