?
Solved

how to add a combo column to a data bound datagridview control

Posted on 2010-04-02
11
Medium Priority
?
554 Views
Last Modified: 2013-12-17
hi -- i'm using .net 3.5 and vs2008 and a windows forms control.  I want to contrain my users choices with a combo box on the datagridview control for a given column. the problem is that i'm populating from a dataset that is databound to the control. it appears that the 'automatic' poplulation of the grid from the dataset is with a textbox column.
it's not obvious how to override this.
Jim
0
Comment
Question by:jdcoburn
  • 6
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 29798021
I am not sure that I completely understand your problem.  Are you saying that you want to filter the drop-down list for one column?
0
 

Author Comment

by:jdcoburn
ID: 29805750
no. I'm using the dataset designer to create the columns (as fields in the dataset). I open a xml file using ds.ReadXML(filename), create a datatable by referencing a table in the dataset and then bind the datagridview using  scriptDisplay_DGV.DataSource = dt. Because i'm using the dataset designer, i can only use text based columns. is there a way to use a combobox column using the dataset designer, or should i scrap the designer and manually create columns?
0
 

Author Comment

by:jdcoburn
ID: 29807387
I've played with this some more. i've eliminated the dataset designer by just creating a dataset, then binding the datagridview to a table in teh dataset. but my question remains the same -- how do i create a combo box column unless there's someting in the xml that tells the DGV to create a combo box columns for that field?
Jim
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 29808180
You should be able to follow these steps to set a DataSource for the DataGridViewComboBoxColumn:

1) Set the DataSource for the column

2) Set the DisplayMember to a valid column name to set the display text.

3) Set the ValueMember to a valid column name to set the value returned when an option is selected.


Screenshot.png
0
 

Author Closing Comment

by:jdcoburn
ID: 31710425
I bound the whole control from the initial edit columns dialog box. this then generated the column, and i was able to modify the column type from that point. if i used your approach, i couldn't get it to recognize the data source.
thanks for the help.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 29841131
Where and how is the data source defined?  Is it a typed DataSet that is defined on the form?
0
 

Author Comment

by:jdcoburn
ID: 29846574
hi -- this is a typed dataset. i suspect some of the confusion exists because i've partially implemented this in code and the rest using the dataset designer tool. I'll try and recap how i got to where i am:
i first built the dataset and datatable using the dataset design tool. i had also created a dataset using the control from the toolbox. the designer also created a binding source. so, the dataset designer tool created the schema necessary to import the xml file (which, from where i had started, had not been defined.) then i bound the datagridviedw to the datatable created as part of the dataset. this all worked fine, until i wanted to add a combobox column to the datagridview so that it constrained the choices for one of the columns.the designer itself will not allow a combobox column.

the code i run when i open the xml file is:

//get file name and display the file name using FileInfo
strFileName = openFileDialog.FileName;
shortName = openFileDialog.SafeFileName;

FileInfo fi = new FileInfo(strFileName);
ds.ReadXml(strFileName, XmlReadMode.InferTypedSchema);

dt = ds.Tables["ScriptTool_DT"];
scriptDisplay_DGV.DataSource = dt;

this.Text = "Script Tool Editor: " + shortName.Remove(shortName.Length - 5);

saveToolStripMenuItem.Enabled = true;
saveAsToolStripMenuItem.Enabled = true;
contentOpen = true;

Ultimately the way around this was to bind the datagridview on the first dialog screen (attached):
that generated the columns based on the xsd schema. then i went into the column editor and changed the type of column from text to combo. individual column binding didn't work.
Jim

first-image.jpg
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 29848767
1) You are using VS.NET 2010

2) There are mucho better ways to approach this problem, like using LINQ.

3) Doing the job in code gives you much more control over the process.
0
 

Author Comment

by:jdcoburn
ID: 29850117
1. yes, i upgraded part way through all ths thinking that vs2010 had a better solution. it didn't seem to matter.
2. I have no experience with LINQ but no reason to disagree. i don't normally deal with db apps.
3. I agree -- if i was going to start over, i would do it from scratch with code. but i thought as long as MS had this designer that might help documentation, i would try it.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 29851793
If you want to use the designer, you need to use a specific sequence of events, and all the data sources need to be defined on the form, so that the DataGridView designer can find them.
0
 

Author Comment

by:jdcoburn
ID: 29852716
i realize that now. it appears that the designer presupposes a schema from an existing data source (eg a sql db.) I wanted to use the designer to create the schema for a data source -- an xml file. i have no experience writing a xml file from scratch, so i was hoping the schema would allow me to generate the file. it appears i started at the wrong place with the wrong tool . i eventually figured out how to create the file by using XMLspy.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

601 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