Results 1 to 11 of 11

Thread: [RESOLVED] Make Cell's in a datagridview a combo box = string array

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] Make Cell's in a datagridview a combo box = string array

    Hi all

    I have a list that i display in a datagridview that has users on one side and Department on the other

    i was wondering if i could make the department a combo box so that the user can only input data that fit into a string array that i create earlier


    Thanks

    Robert

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Make Cell's in a datagridview a combo box = string array

    In the designer you add a column and change its type to combo box. You then set its DataSource, DisplayMember and ValueMember just like a regular ComboBox. The SelectedValue of the ComboBox in each cell is that cell's Value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Make Cell's in a datagridview a combo box = string array

    Is there a way to do this at runtime?

    here is what i get when i add a column manual then run my program



    I have the feeling im having a brain fart somewhere but i just cant figure this out

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Make Cell's in a datagridview a combo box = string array

    When you create the combo box column at design time you have to set its DataPropertyName property, otherwise it doesn't know that it's supposed to bind to a DataColumn and the grid will create a new column at run time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Make Cell's in a datagridview a combo box = string array

    Here is the code im working with ( it may look fimilar ) but im confused be cause at no point do i declare any other columns. However "departments" is part of the databindingsource but in not sure how to get the dgv to set something for something that doesnt exist yet





    VB Code:
    1. private void userListImport()
    2.         {
    3.             //worktodo
    4.  
    5.  
    6.             try
    7.             {
    8.                 //writecode to bring focus to tab
    9.                 OpenFileDialog OFD_user = new OpenFileDialog();
    10.                 OFD_user.InitialDirectory = StartingPath + @"\iPass\Billpass config";
    11.                 OFD_user.Filter = "Tab delmited (*.csv)|*.csv";
    12.                 OFD_user.FilterIndex = 1;
    13.                 OFD_user.FileName = "user.csv";
    14.  
    15.                 if (OFD_user.ShowDialog() == DialogResult.OK)
    16.                 {
    17.                     string
    18.                      filePath = OFD_user.FileName,
    19.                      folderPath = System.IO.Path.GetDirectoryName(filePath),
    20.                      fileName = System.IO.Path.GetFileName(filePath),
    21.                      adapter = "SELECT F1 AS Name, F2 AS Department FROM [" + fileName + "]";
    22.  
    23.                     dgv_users.DataSource = userbindingsource;
    24.  
    25.                     userbindingsource.DataSource = (ReadCSVFile(folderPath, "Users", adapter, "no"));
    26.                     dgv_users.DataSource = userbindingsource;
    27.                    
    28. /// this is the line that i just added
    29.                    CLM_department.DataSource = department;
    30.  
    31.  
    32.  
    33.                 }//ofd if
    34.  
    35.             }//try
    36.             catch (Exception ex)
    37.             {
    38.                 MessageBox.Show("Error with CSV backup" + Environment.NewLine + ex.Message);
    39.                 Console.WriteLine(ex.ToString());
    40.             }
    41.         }

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Make Cell's in a datagridview a combo box = string array

    Add two column to your DataGridView at design time. Set the DataPropertyName of the first column to "Name". Make the second column a combo box column and set its DataPropertyName to "Department". When you bind the BindingSource to the grid your grid columns will now bind to the proper columns of the source data.

    To populate the drop-down list of the combo box column you need to set its DataSource, DisplayMember and ValueMember properties just like you would a regular ComboBox. The DataSource will preumably be a list containing all the available departments. The DisplayMember and ValueMember will be the names of the properties that you want to display and assign to the grid cells.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Make Cell's in a datagridview a combo box = string array

    i'll give it a try

  8. #8

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Make Cell's in a datagridview a combo box = string array

    I got it but still have a little problem

    here is the code i adapted to work


    Code:
    DataGridViewComboBoxColumn col0 = new DataGridViewComboBoxColumn();
                        col0.DataPropertyName = "Department";
                        dgv_users.Columns.Add(col0);
                        col0.DataSource = department;
                        col0.SortMode = DataGridViewColumnSortMode.Programmatic;
                        //col0.HeaderCell.SortGlyphDirection = SortOrder.None;
                        col0.HeaderText = "Department";

    the problem is I cant make the original coloum go away


  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Make Cell's in a datagridview a combo box = string array

    Why do you have code to add a combo box column? Create the columns in the designer. Set their properties in the designer. The only thing you should have to do at run time is set the DataSource properties for the grid and the column. Everything else should be done at design time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Make Cell's in a datagridview a combo box = string array

    Perhapse this is where i am missing the point

    but at designe time if i right click on the datagridview and "add column" as a combobox

    then in the code i do soemthing like

    Col1.DataSource = department;
    Col1.DataPropertyName = "Department";

    i still get an extra column

    I think the problem is dont know where the hook up is that says dump all the data in this existing column

  11. #11

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Make Cell's in a datagridview a combo box = string array

    Quote Originally Posted by jmcilhinney
    DataPropertyName property.
    Duh- sorry it was a long day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width