|
-
Jan 23rd, 2007, 05:23 PM
#1
Thread Starter
Fanatic Member
[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
-
Jan 24th, 2007, 05:03 PM
#2
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.
-
Jan 25th, 2007, 04:01 PM
#3
Thread Starter
Fanatic Member
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
-
Jan 25th, 2007, 05:35 PM
#4
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.
-
Jan 25th, 2007, 07:34 PM
#5
Thread Starter
Fanatic Member
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:
private void userListImport()
{
//worktodo
try
{
//writecode to bring focus to tab
OpenFileDialog OFD_user = new OpenFileDialog();
OFD_user.InitialDirectory = StartingPath + @"\iPass\Billpass config";
OFD_user.Filter = "Tab delmited (*.csv)|*.csv";
OFD_user.FilterIndex = 1;
OFD_user.FileName = "user.csv";
if (OFD_user.ShowDialog() == DialogResult.OK)
{
string
filePath = OFD_user.FileName,
folderPath = System.IO.Path.GetDirectoryName(filePath),
fileName = System.IO.Path.GetFileName(filePath),
adapter = "SELECT F1 AS Name, F2 AS Department FROM [" + fileName + "]";
dgv_users.DataSource = userbindingsource;
userbindingsource.DataSource = (ReadCSVFile(folderPath, "Users", adapter, "no"));
dgv_users.DataSource = userbindingsource;
/// this is the line that i just added
CLM_department.DataSource = department;
}//ofd if
}//try
catch (Exception ex)
{
MessageBox.Show("Error with CSV backup" + Environment.NewLine + ex.Message);
Console.WriteLine(ex.ToString());
}
}
-
Jan 25th, 2007, 07:41 PM
#6
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.
-
Jan 25th, 2007, 07:55 PM
#7
Thread Starter
Fanatic Member
Re: Make Cell's in a datagridview a combo box = string array
-
Jan 25th, 2007, 08:23 PM
#8
Thread Starter
Fanatic Member
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
-
Jan 25th, 2007, 09:18 PM
#9
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.
-
Jan 25th, 2007, 11:31 PM
#10
Thread Starter
Fanatic Member
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
-
Jan 26th, 2007, 12:07 AM
#11
Thread Starter
Fanatic Member
Re: Make Cell's in a datagridview a combo box = string array
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|