Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / November 2007

Tip: Looking for answers? Try searching our database.

Select value in ComboBo in DataGridView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul S - 12 Nov 2007 14:56 GMT
Based on a change of value in a column - c1 - I want the set the value of
another column - c2 - hosting a combobox with valid values. When I set the
value in c2 in my code it does not get displayed in the grid. I believe I
need to select the proper value in the combo box in c2, but I don't know how
to get it.

Thanks
Paul S
Linda Liu[MSFT] - 13 Nov 2007 07:07 GMT
Hi Paul,

If you bind the c2 with a data source, i.e. set the DataSource property of
the c2 to a data source and set the ValueMember property of the c2 to a
property in the data source, you could find the valid values for the cells
under the c2 from the property specified as the value member in the data
source.

For example, we have a business class named Person, which has two
properties--ID and Name. We create a collection of type List<Person> and
add some objects of type Person into this collection. Then we set the
DataSource property of a DataGridViewComboBoxColumn to this collection and
the ValueMember property to "ID" and DisplayMember property to "Name".

Now we can find all the valid values for cells under the
DataGridViewComboBoxColumn from the ID property of the objects contained in
the above collection.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Paul S - 13 Nov 2007 08:23 GMT
I seems that I was a bit unclear in my question.
Say the valid values for C2 was 1, 2, and 3 then I set it like this

grid[columnIndex, rowIndex].Value = "2";
(this does not show the value in the cell)

or like this using the combo box:
combobox.SelectedIndex = 1;
(I don't know how to get the combobox from the cell - the closest I get is
in the
eventhandler for the EditingControlShowing event - ComboBox c = e.Control as
ComboBox)

Signature

Sincerely
Paul S

> Hi Paul,
>
[quoted text clipped - 40 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu[MSFT] - 13 Nov 2007 10:11 GMT
Hi Paul,

Thank you for your prompt reply!

>Say the valid values for C2 was 1, 2, and 3 then I set it like this
>grid[columnIndex, rowIndex].Value = "2";
>(this does not show the value in the cell)

Could you tell me how you bind the DataGridViewComboBoxColumn?

If you bind the DataGridViewComboBoxColumn to a data source and set the
ValueMember and DisplayMember properties, the DataGriedViewComboBoxCell
should show the text of the corresponding DisplayMember, not value of the
ValueMember.

I will illustrate this with an example. Create a WinForm application
project and add a DataGridView and a Button on the form. Add a
DataGridViewComboBoxColumn into the DataGridView control. In the Form load
event handler, create a DataTable with two columns named ID and Name and
add some rows into this DataTable and bind the DataGridViewComboBoxColumn
in the DataGridView to the DataTable.

In the Button Click event handler, set the current cell's value to a
specified value.

The following is the sample code:

private void Form1_Load(object sender, EventArgs e)
{
           this.dataGridView1.RowCount = 3;
                     
           DataTable lookup = new DataTable();
           DataColumn col = new DataColumn("ID", typeof(Int32));
           lookup.Columns.Add(col);
           col = new DataColumn("Name", typeof(string));
           lookup.Columns.Add(col);
           DataRow row = lookup.NewRow();
           row[0] = 1;
           row[1] = "aa";
           lookup.Rows.Add(row);
           row = lookup.NewRow();
           row[0] = 2;
           row[1] = "bb";
           lookup.Rows.Add(row);                      
       
           // bind the DataGridViewComboBoxColumn to the DataTable
           (this.dataGridView1.Columns[0] as
DataGridViewComboBoxColumn).DataSource = lookup;
           (this.dataGridView1.Columns[0] as
DataGridViewComboBoxColumn).ValueMember = "ID";
           (this.dataGridView1.Columns[0] as
DataGridViewComboBoxColumn).DisplayMember = "Name";
}

private void button1_Click(object sender, EventArgs e)
{
           this.dataGridView1.CurrentCell.Value = 2; // the current cell's
value becomes 2 and the text displayed in the current cell becomes "bb"
}

> I don't know how to get the combobox from the cell - the closest I get is
in the eventhandler for the EditingControlShowing event - ComboBox c =
e.Control as ComboBox

To get the hosted ComboBox in the DataGridViewComboBoxCell, we usually
handle the EditingControlShowing event of the DataGridView. But I don't
think you need to get the hosted ComboBox in order to get the valid values
for the DataGridViewComboBoxCell.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 13 Nov 2007 12:42 GMT
Hi Linda
We're getting closer.

I've added the c2 column to your sample and handled the EndEnit event

       private void dataGridView1_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
       {
           if (e.ColumnIndex == 1)
           {
               if (this.dataGridView1.CurrentCell.Value == "1")
                   this.dataGridView1[0, e.RowIndex].Value = 1;
               if (this.dataGridView1.CurrentCell.Value == "2")
                   this.dataGridView1[0, e.RowIndex].Value = 2;
           }
       }

This illustrates my problem not being able to set the value in the first
column containing the Combo box.

Signature

Sincerely
Paul S

Linda Liu[MSFT] - 14 Nov 2007 10:09 GMT
Hi Paul,

Thank you for your quick response!

The CellEndEdit event of a DataGridView occurs when edit mode stops for the
current selected cell.

For example, after you type "1" in the first row and second column within
the DataGridView, you should press the Enter key or select another cell to
make the CellEndEdit event raised. Then you should see the text displayed
in the first row and first column is "aa".

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 14 Nov 2007 11:28 GMT
Hi Linda
I agree on everything you say. When I leave the cell in the second column
the eventhandler for the CellEndEdit gets executed and there I set the value
for the first column containing the combo box.

This is not happening:
Then you should see the text displayed
in the first row and first column is "aa"  

Signature

Sincerely
Paul S

Linda Liu[MSFT] - 15 Nov 2007 04:10 GMT
Hi Paul,

Could you please tell me what version of the Visual Studio you're using?
I'm using VS05.

Since I didn't see the same result as you, it would be better for you to
create a simple project that could just reproduce the problem and send it
to me.

To get my actual email address, remove 'online' from my displayed email
address.

Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 15 Nov 2007 07:25 GMT
Hi Linda

In the progress of creating a simple sample I saw that what you're saying is
right and what's causing my problems is how I adress the cells

       private void dataGridView1_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
       {
           if (e.ColumnIndex == 1)
           {
               string val = this.dataGridView1.CurrentCell.Value.ToString();
               DataGridViewCell cell = this.dataGridView1[e.ColumnIndex,
e.RowIndex];

               if (string.Equals(val, "1"))
                   cell.OwningRow.Cells[e.ColumnIndex].Value = 1;
               if (string.Equals(val, "2"))
                   this.dataGridView1[0, e.RowIndex].Value = 2;
           }
       }

2) works, but 1 doesn't. I thought that what I did in 1) was just another
way to do the same as in 2).

Signature

Paul S

> Hi Paul,
>
[quoted text clipped - 11 lines]
> Linda Liu
> Microsoft Online Community Support
Linda Liu[MSFT] - 15 Nov 2007 09:55 GMT
Hi Paul,

Thank you for your prompt reply!

The statement 'cell.OwningRow.Cells[e.ColumnIndex]' refers to the cell
itself.

Modify the line of code
cell.OwningRow.Cells[e.ColumnIndex].Value = 1;
to
cell.OwningRow.Cells[0].Value =1;

should solve the problem.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 23 Nov 2007 11:50 GMT
Thanks Linda

You're absolytely right - silly mistake on my side - sorry

Signature

Sincerely
Paul S

> Hi Paul,
>
[quoted text clipped - 15 lines]
> Linda Liu
> Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.