Link to home
Start Free TrialLog in
Avatar of jdcoburn
jdcoburn

asked on

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

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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?
Avatar of jdcoburn
jdcoburn

ASKER

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?
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Where and how is the data source defined?  Is it a typed DataSet that is defined on the form?
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
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.
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.
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.
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.