Solved

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

Posted on 2015-01-31
4
142 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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