Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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