Associating a DataRow with a TextBox

I want to bind a text field to a single DataRow object (passed to the dialog form for editing). Here is my code:

DataRow row = myDataTable.NewRow();
EditForm form = new EditForm(row);

//in EditForm constructor
nameTextBox.DataBindings.Add("Text", row, "name");

and I get an error: cannot bind to a property or column in a DataSource. Do you know what I am missing or maybe some workarounds?

[Added]

My DataTable surely contains a DataColumn with ColumnName = "name". Here is my code for creating a DataTable

    public DataTable SelectReturnDataTable(string tableName, string sql, params SQLiteParameter[] parameters)
    {
        using (SQLiteConnection conn = new SQLiteConnection(_connectionString))
        {
            using (SQLiteCommand cmd = new SQLiteCommand(conn))
            {
                cmd.CommandText = sql;
                foreach (SQLiteParameter p in parameters)
                    cmd.Parameters.Add(p);

                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                DataTable dt = new DataTable(tableName);                                        

                conn.Open();                    
                da.Fill(dt);

                return dt;
            }
        }
    }
+5
source share
3 answers

Try the following:

DataView dv = myDataTable.DefaultView;

dv.RowFilter = "id=1";

nameTextBox.DataBindings.Add("Text",dv,"NAME");
+2
source

I tried to reproduce this (with VS 2008 SP1), and I get an InvalidCastException if the row has zero data in the name column, but the program continues and works.

, , . @Henk , DataTable.

0

This is where overloading occurs to create a DataBinding that takes an object to use if the source object is DBNull. You can try this.

0
source

All Articles