Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-31
4
Medium Priority
?
155 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 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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.

722 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