3

i have two DataGridViewComboBoxColumn that i add at run time i need the items of the first DataGridViewComboBoxColumn to stay the same in all the rows of the gridview but i want the items of the second DataGridViewComboBoxColumn to be different from row to the other depending on the selected item of the first DataGridViewComboBoxColumn

if we say the first DataGridViewComboBoxColumn represents the locations and the second DataGridViewComboBoxColumn to represent the sublocations. so i want the second DataGridViewComboBoxColumn items to be the sublocations of the selected location from the first DataGridViewComboBoxColumn

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • Sara, I would like to help - especially after giving you a hard time over the wording of this question :). However I'm not familiar with the DataGrid and will need to investigate it. I won't have a chance to do that until I get home tonight. P.S. You really should close the first question. continued – Binary Worrier Jan 15 '09 at 14:16
  • One of the moderators will close one of the questions as a duplicate, you can choose which one to close, they might close this one. – Binary Worrier Jan 15 '09 at 14:17
  • i want this to stay,but i didnt know how to delete the othere one!! –  Jan 15 '09 at 14:28
  • You should be able to see - on your own questions - three links at the bottom of the question "Edit - Close - Delete". I'd delete your old queston. – Binary Worrier Jan 15 '09 at 14:36
  • i couldnt find them!! :s –  Jan 15 '09 at 14:41
  • Maybe you need more rep to be about to edit your own questions. – Binary Worrier Jan 15 '09 at 14:51
  • Try again, now you've +50 points – Binary Worrier Jan 15 '09 at 14:52

3 Answers3

3

One option is to change the datasource at cell level for sublocations.

Supposing the grid is named grid and the two grid columns were named locationsColumn respectively subLocationsColumn:

private void Form1_Load(object sender, EventArgs e)
{
    locationsColumn.DataSource = new string[] { "Location A", "Location B" };
}

then, on grid's CellEndEdit event:

private void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if(locationsColumn.Index == e.ColumnIndex)
    {
        DataGridViewComboBoxCell subLocationCell = 
            (DataGridViewComboBoxCell)(grid.Rows[e.RowIndex].Cells["subLocationsColumn"]);

        string location = grid[e.ColumnIndex, e.RowIndex].Value as String;

        switch (location)
        {
            case "Location A":
                subLocationCell.DataSource = new string[] {
                    "A sublocation 1",
                    "A sublocation 2",
                    "A sublocation 3" 
                };
                break;
            case "Location B":
                subLocationCell.DataSource = new string[] { 
                    "B sublocation 1",
                    "B sublocation 2",
                    "B sublocation 3" 
                };
                break;
            default:
                subLocationCell.DataSource = null;
                return;
        }
    }
}

Some additional handling is necessary when the location changes for existing rows but this is the basic idea.

Sorin Comanescu
  • 4,829
  • 3
  • 29
  • 38
2

Check this out, I think it outlines what you need:

http://www.timvw.be/2007/01/17/exploring-datagridviewcomboboxcolumn-databinding/

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
1

One idea would be to use a secondary Binding Source for the "SubLocations" column. This BindingSource can be filtered by the LocationId selected in the "Locations" column. The key to do this is to use the EditingControlShowing and CellValueChanged events of the grid to set the proper filtering on the SubLocations column when the selected Location changes.

There is one example here.

Sergiu Damian
  • 1,420
  • 8
  • 10