databind to a DataSet

Posted on 2006-03-27
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";

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"));
            dc = new DataColumn("dbField", Type.GetType("System.String"));
            dc = new DataColumn("create", Type.GetType("System.Int32"));

            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 = new DataTable("createOptions");
            dc = new DataColumn("id", Type.GetType("System.Int32"));
            dc = new DataColumn("text", Type.GetType("System.String"));

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

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


            return result;
Question by:ARACK04

    Assisted Solution

    by:Harland Gomez
    Have you tried renaming the column name? maybe the text is a reserved word.. just a thought..
    LVL 6

    Accepted Solution

    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

    Author Comment

    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.

    Author Comment

    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!!!!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now