Solved

How do I assign a value to a DataGridViewComboBoxColumn in C#?

Posted on 2015-01-31
4
128 Views
Last Modified: 2015-02-03
I have a DataGridView childDataGridView with a DataGridViewTextBoxColumn cmdSpnIndCol and a DataGridViewComboBoxColumn commandSponsoredIndicator.  

commandSponsoredIndicator has three hard coded Items values: "Select One", "Yes", and "No".  Each row of childDataGridView has the following possible values for cmdSpnIndCol: "Y", "N", or null.

I am trying to iterate through each row of childDataGridView to set commandSponsoredIndicator as follows:

if  cmdSpnIndCol is "Y", then set to "Yes".
if cmdSpnIndCol is "N", then set to "No".
otherwise, set to "Select One".

My code is below.  All is working except the assignment part.  What am I doing wrong?

foreach (DataGridViewRow row in childDataGridView.Rows)
                {
                    if (row.Cells["cmdSponIndCol"].Value.ToString() == "Y")
                    {
                        // not working
                        row.Cells["commandSponsoredIndicator"].Value = "Yes";
                    }
                    else if (row.Cells["cmdSponIndCol"].Value.ToString() == "N")
                    {
                        // not working
                        row.Cells["commandSponsoredIndicator"].Value = "No";
                    }
                    else
                    {
                        // not working
                        row.Cells["commandSponsoredIndicator"].Value = "Select One";
                    }
                 }
0
Comment
Question by:spazjr01
4 Comments
 
LVL 16

Accepted Solution

by:
Kamal Khaleefa earned 500 total points
Comment Utility
Hi

you need to use FindControl to find the control and assign your value

try something like this

row.findcontrol

or why you don't do it before binding to the grid view

datatable dt=Get my date from DB;

then make a for loop and change the vale and assign it to the datatable
then bind it.
0
 
LVL 25

Expert Comment

by:apeter
Comment Utility
Hope you are doing in item_bound event of data grid.
0
 
LVL 8

Expert Comment

by:Naitik Gamit
Comment Utility
use like this:

foreach (DataGridViewRow row in childDataGridView.Rows)
                {
                    if (row.Cells.FindControl("cmdSponIndCol") as TextBox).Text.ToString() == "Y")
                    {
                          //Assign Value
                    }
                }
0
 

Author Closing Comment

by:spazjr01
Comment Utility
I ended up creating a new DataTable yesNoTable (with two columns) and binding those DataColumns to the the DataGridViewComboBoxColumn's DisplayedMember and ValueMember properties.  This appears to have solved my problem.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

12 Experts available now in Live!

Get 1:1 Help Now