?
Solved

databind to a DataSet

Posted on 2006-03-27
4
Medium Priority
?
895 Views
Last Modified: 2012-05-05
I'm trying to bind to a dataset, but am getting an exception that a field does not exist, which should.  My method to create the DataSet  is below (in its infancy).  It's VERY simple.  Here is the databinding:

DataSet result = this.getDataSet(fields);
dataGridView1.DataSource = result.Tables["main"];


cbc0 = new DataGridViewComboBoxColumn();
           
cbc0.DisplayIndex = 0;
cbc0.DataSource = result.Tables["createOptions"];
cbc0.DisplayMember = "text";  ///////////////////////////////PROBLEM - text field not found!
cbc0.ValueMember = "id";
dataGridView1.Columns.Add(cbc0);



DataSet creation code:  


        private DataSet getDataSet(ComboBox cb) {
            DataSet result = new DataSet();

            DataTable dt = new DataTable("main");
            DataColumn dc = new DataColumn("AttributeName", Type.GetType("System.String"));
            dt.Columns.Add(dc);
            dc = new DataColumn("dbField", Type.GetType("System.String"));
            dt.Columns.Add(dc);
            dc = new DataColumn("create", Type.GetType("System.Int32"));
            dt.Columns.Add(dc);

            DataRow row;
            for (int i = 0; i < cb.Items.Count; i++) {
                row = dt.NewRow();

                cb.SelectedIndex = i;
                row["AttributeName"] = cb.SelectedValue.ToString();
                row["dbField"] = cb.SelectedValue.ToString();
                row["create"] = 0;
                dt.Rows.Add(row);
            }

            result.Tables.Add(dt);

            dt = new DataTable("createOptions");
            dc = new DataColumn("id", Type.GetType("System.Int32"));
            dt.Columns.Add(dc);
            dc = new DataColumn("text", Type.GetType("System.String"));
            dt.Columns.Add(dc);

            row = dt.NewRow();
            row["id"] = 0;
            row["text"] = "Create";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["id"] = 1;
            row["text"] = "Don't Create";
            dt.Rows.Add(row);

            result.Tables.Add(dt);

            return result;
        }
0
Comment
Question by:ARACK04
  • 2
4 Comments
 

Assisted Solution

by:Harland Gomez
Harland Gomez earned 1000 total points
ID: 16307764
Have you tried renaming the column name? maybe the text is a reserved word.. just a thought..
0
 
LVL 6

Accepted Solution

by:
e1v earned 1000 total points
ID: 16309369
Hello, I tried to run your code using a databound combobox as the parameter in the DataSet result = this.getDataSet(fields);
and it compiled an ran without errors...So I'm not sure what the problem is...

I can give you one general tip, always set DisplayMember and ValueMember before you set DataSource
http://blogs.msdn.com/subhagpo/archive/2005/02/17/375112.aspx
0
 

Author Comment

by:ARACK04
ID: 16309431
I moved the datasource line after the valuemember and DisplayMember, and renamed text to text2, but it's still not finding it.  This is weird.
0
 

Author Comment

by:ARACK04
ID: 16309961
Thanks guys.  I went in a different directiosn, got it to work, then went backwards to what I had originally, and it still works; I am unable to re-create the error.  Well, thanks for the help!!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 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