C# – Binding DataRow to TextBox

cdata-binding

I want to bind textbox to single DataRow object (passed to 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'm gettinh an error: Cannot bind to property or column in DataSource. Do you know what I'm missing or any workarounds maybe?

[Added]

My DataTable for sure contains DataColumn with ColumnName="name". Here is my code for creating 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;
            }
        }
    }

Best Answer

Try this:

DataView dv = myDataTable.DefaultView;

dv.RowFilter = "id=1";

nameTextBox.DataBindings.Add("Text",dv,"NAME");
Related Topic