• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 898
  • Last Modified:

databind to a DataSet

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
ARACK04
Asked:
ARACK04
  • 2
2 Solutions
 
Harland GomezSoftware Engineer IICommented:
Have you tried renaming the column name? maybe the text is a reserved word.. just a thought..
0
 
e1vCommented:
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
 
ARACK04Author Commented:
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
 
ARACK04Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now