Solved

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

Posted on 2015-01-31
4
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
Kamal Khaleefa earned 500 total points
ID: 40582304
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
ID: 40582609
Hope you are doing in item_bound event of data grid.
0
 
LVL 8

Expert Comment

by:Naitik Gamit
ID: 40585618
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
ID: 40587144
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
array not updating 8 42
c#, datatable, aspx 4 52
Cannot interpret token '"' at position 10. 3 25
What is GIS method of Geometry data type? 6 36
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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